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

75 lines
2.0 KiB
JavaScript
Raw 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 {
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)
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 }
2019-06-09 01:00:12 +00:00
if (_.isEmpty(row.value)) { row.value = row.key }
2018-05-06 20:19:37 +00:00
_.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.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)
}
}