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

85 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

const _ = require('lodash')
const { createApolloFetch } = require('apollo-fetch')
/* global WIKI */
module.exports = async () => {
WIKI.logger.info('Syncing locales with Graph endpoint...')
try {
2018-05-28 18:46:55 +00:00
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 {
availability
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.cache.set('locales', locales)
2018-05-06 20:19:37 +00:00
// -> Download locale strings
2018-06-17 15:12:11 +00:00
if (WIKI.config.lang.autoUpdate) {
const activeLocales = WIKI.config.lang.namespacing ? WIKI.config.lang.namespaces : [WIKI.config.lang.code]
for (const currentLocale of activeLocales) {
const localeInfo = _.find(locales, ['code', currentLocale])
const respStrings = await apollo({
query: `query ($code: String!) {
localization {
strings(code: $code) {
key
value
}
2018-05-06 20:19:37 +00:00
}
}`,
variables: {
code: currentLocale
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 }
if (_.isEmpty(row.value)) {
row.value = row.key
}
_.set(lcObj, row.key.replace(':', '.'), row.value)
})
await WIKI.models.locales.query().update({
code: currentLocale,
strings: lcObj,
isRTL: localeInfo.isRTL,
name: localeInfo.name,
nativeName: localeInfo.nativeName,
availability: localeInfo.availability
}).where('code', currentLocale)
2018-05-06 20:19:37 +00:00
WIKI.logger.info(`Pulled latest locale updates for ${localeInfo.name} from Graph endpoint: [ COMPLETED ]`)
}
2018-05-06 20:19:37 +00:00
}
await WIKI.lang.refreshNamespaces()
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)
}
}