feat: sync locales strings

This commit is contained in:
NGPixel 2018-05-06 16:19:37 -04:00
parent 7786f9042f
commit b143aa2f8c
5 changed files with 80 additions and 7 deletions

View File

@ -17,10 +17,22 @@ module.exports = {
ns: this.namespaces, ns: this.namespaces,
defaultNS: 'common', defaultNS: 'common',
saveMissing: false, saveMissing: false,
preload: [WIKI.config.site.lang],
lng: WIKI.config.site.lang, lng: WIKI.config.site.lang,
fallbackLng: 'en' fallbackLng: 'en'
}) })
// Load fallback defaults
const enFallback = require('../locales/default.json')
if (_.isPlainObject(enFallback)) {
_.forOwn(enFallback, (data, ns) => {
this.namespaces.push(ns)
this.engine.addResourceBundle('en', ns, data)
})
}
// Load current language
this.loadLocale(WIKI.config.site.lang, { silent: true })
return this return this
}, },
attachMiddleware (app) { attachMiddleware (app) {
@ -39,10 +51,22 @@ module.exports = {
throw new Error('Invalid locale or namespace') throw new Error('Invalid locale or namespace')
} }
}, },
async loadLocale(locale) { async loadLocale(locale, opts = { silent: false }) {
return Promise.fromCallback(cb => { const res = await WIKI.db.Locale.findOne({
return this.engine.loadLanguages(locale, cb) where: {
code: locale
}
}) })
if (res) {
if (_.isPlainObject(res.strings)) {
_.forOwn(res.strings, (data, ns) => {
this.namespaces.push(ns)
this.engine.addResourceBundle(locale, ns, data, true, true)
})
}
} else if (!opts.silent) {
throw new Error('No such locale in local store.')
}
}, },
async setCurrentLocale(locale) { async setCurrentLocale(locale) {
return Promise.fromCallback(cb => { return Promise.fromCallback(cb => {

View File

@ -28,6 +28,12 @@ module.exports = {
installDate: isInstalled ? _.find(localLocales, ['code', rl.code]).updatedAt : null installDate: isInstalled ? _.find(localLocales, ['code', rl.code]).updatedAt : null
} }
}) })
},
async config(obj, args, context, info) {
return {
locale: WIKI.config.site.lang,
autoUpdate: WIKI.config.site.langAutoUpdate
}
} }
}, },
LocalizationMutation: { LocalizationMutation: {

View File

@ -16,6 +16,7 @@ extend type Mutation {
type LocalizationQuery { type LocalizationQuery {
locales: [LocalizationLocale] locales: [LocalizationLocale]
config: LocalizationConfig
} }
# ----------------------------------------------- # -----------------------------------------------
@ -43,3 +44,8 @@ type LocalizationLocale {
nativeName: String! nativeName: String!
updatedAt: Date! updatedAt: Date!
} }
type LocalizationConfig {
locale: String!
autoUpdate: Boolean!
}

View File

@ -5,6 +5,8 @@ const { createApolloFetch } = require('apollo-fetch')
/* global WIKI */ /* global WIKI */
WIKI.redis = require('../core/redis').init() WIKI.redis = require('../core/redis').init()
WIKI.db = require('../core/db').init()
const apollo = createApolloFetch({ const apollo = createApolloFetch({
uri: 'https://graph.requarks.io' uri: 'https://graph.requarks.io'
}) })
@ -13,7 +15,11 @@ module.exports = async (job) => {
WIKI.logger.info('Syncing locales with Graph endpoint...') WIKI.logger.info('Syncing locales with Graph endpoint...')
try { try {
const resp = await apollo({ await WIKI.configSvc.loadFromDb(['site'])
// -> Fetch locales list
const respList = await apollo({
query: `{ query: `{
localization { localization {
locales { locales {
@ -27,8 +33,38 @@ module.exports = async (job) => {
} }
}` }`
}) })
const locales = _.sortBy(_.get(resp, 'data.localization.locales', []), 'name').map(lc => ({...lc, isInstalled: (lc.code === 'en')})) const locales = _.sortBy(_.get(respList, 'data.localization.locales', []), 'name').map(lc => ({...lc, isInstalled: (lc.code === 'en')}))
WIKI.redis.set('locales', JSON.stringify(locales)) WIKI.redis.set('locales', JSON.stringify(locales))
const currentLocale = _.find(locales, ['code', WIKI.config.site.lang])
// -> Download locale strings
if (WIKI.config.site.langAutoUpdate) {
const respStrings = await apollo({
query: `{
localization {
strings(code: "${WIKI.config.site.lang}") {
key
value
}
}
}`
})
const strings = _.get(respStrings, 'data.localization.strings', [])
let lcObj = {}
_.forEach(strings, row => {
if (_.includes(row.key, '::')) { return }
_.set(lcObj, row.key.replace(':', '.'), row.value)
})
WIKI.db.Locale.upsert({
code: WIKI.config.site.lang,
strings: lcObj,
isRTL: currentLocale.isRTL,
name: currentLocale.name,
nativeName: currentLocale.nativeName
})
}
WIKI.logger.info('Syncing locales with Graph endpoint: [ COMPLETED ]') WIKI.logger.info('Syncing locales with Graph endpoint: [ COMPLETED ]')
} catch (err) { } catch (err) {

View File

@ -284,7 +284,8 @@ module.exports = () => {
// Site namespace // Site namespace
_.set(WIKI.config.site, 'title', req.body.title) _.set(WIKI.config.site, 'title', req.body.title)
_.set(WIKI.config.site, 'lang', 'en') _.set(WIKI.config.site, 'lang', 'en')
_.set(WIKI.config.site, 'rtl', _.includes(WIKI.data.rtlLangs, req.body.lang)) _.set(WIKI.config.site, 'langAutoUpdate', true)
_.set(WIKI.config.site, 'rtl', false)
_.set(WIKI.config.site, 'sessionSecret', (await crypto.randomBytesAsync(32)).toString('hex')) _.set(WIKI.config.site, 'sessionSecret', (await crypto.randomBytesAsync(32)).toString('hex'))
// Auth namespace // Auth namespace