wikijs-fork/server/jobs/sync-graph-locales.js

79 lines
2.1 KiB
JavaScript
Raw Normal View History

require('../core/worker')
const _ = require('lodash')
const { createApolloFetch } = require('apollo-fetch')
/* global WIKI */
WIKI.redis = require('../core/redis').init()
WIKI.models = require('../core/db').init()
2018-05-06 20:19:37 +00:00
module.exports = async (job) => {
WIKI.logger.info('Syncing locales with Graph endpoint...')
try {
2018-05-28 18:46:55 +00:00
await WIKI.configSvc.loadFromDb()
const apollo = createApolloFetch({
uri: WIKI.config.graphEndpoint
})
2018-05-06 20:19:37 +00:00
// -> Fetch locales list
const respList = await apollo({
query: `{
localization {
locales {
code
name
nativeName
isRTL
createdAt
updatedAt
}
}
}`
})
2018-05-06 20:19:37 +00:00
const locales = _.sortBy(_.get(respList, 'data.localization.locales', []), 'name').map(lc => ({...lc, isInstalled: (lc.code === 'en')}))
WIKI.redis.set('locales', JSON.stringify(locales))
const currentLocale = _.find(locales, ['code', WIKI.config.lang.code])
2018-05-06 20:19:37 +00:00
// -> Download locale strings
2018-06-17 15:12:11 +00:00
if (WIKI.config.lang.autoUpdate) {
2018-05-06 20:19:37 +00:00
const respStrings = await apollo({
query: `query ($code: String!) {
2018-05-06 20:19:37 +00:00
localization {
strings(code: $code) {
2018-05-06 20:19:37 +00:00
key
value
}
}
}`,
variables: {
code: WIKI.config.lang.code
}
2018-05-06 20:19:37 +00:00
})
const strings = _.get(respStrings, 'data.localization.strings', [])
let lcObj = {}
_.forEach(strings, row => {
if (_.includes(row.key, '::')) { return }
_.set(lcObj, row.key.replace(':', '.'), row.value)
})
await WIKI.models.locales.query().update({
code: WIKI.config.lang.code,
2018-05-06 20:19:37 +00:00
strings: lcObj,
isRTL: currentLocale.isRTL,
name: currentLocale.name,
nativeName: currentLocale.nativeName
}).where('code', WIKI.config.lang.code)
2018-05-06 20:19:37 +00:00
}
await WIKI.redis.publish('localization', 'reload')
WIKI.logger.info('Syncing locales with Graph endpoint: [ COMPLETED ]')
} catch (err) {
WIKI.logger.error('Syncing locales with Graph endpoint: [ FAILED ]')
WIKI.logger.error(err.message)
}
}