2018-05-07 03:21:48 +00:00
|
|
|
const _ = require('lodash')
|
|
|
|
const { createApolloFetch } = require('apollo-fetch')
|
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
2019-02-13 22:20:46 +00:00
|
|
|
module.exports = async (localeCode) => {
|
|
|
|
WIKI.logger.info(`Fetching locale ${localeCode} from Graph endpoint...`)
|
2018-05-07 03:21:48 +00:00
|
|
|
|
|
|
|
try {
|
2018-05-28 18:46:55 +00:00
|
|
|
const apollo = createApolloFetch({
|
|
|
|
uri: WIKI.config.graphEndpoint
|
|
|
|
})
|
|
|
|
|
2018-05-07 03:21:48 +00:00
|
|
|
const respStrings = await apollo({
|
|
|
|
query: `query ($code: String!) {
|
|
|
|
localization {
|
|
|
|
strings(code: $code) {
|
|
|
|
key
|
|
|
|
value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
variables: {
|
2019-02-13 22:20:46 +00:00
|
|
|
code: localeCode
|
2018-05-07 03:21:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
const strings = _.get(respStrings, 'data.localization.strings', [])
|
|
|
|
let lcObj = {}
|
|
|
|
_.forEach(strings, row => {
|
|
|
|
if (_.includes(row.key, '::')) { return }
|
2019-07-13 03:56:44 +00:00
|
|
|
if (_.isEmpty(row.value)) {
|
|
|
|
row.value = row.key
|
|
|
|
}
|
2018-05-07 03:21:48 +00:00
|
|
|
_.set(lcObj, row.key.replace(':', '.'), row.value)
|
|
|
|
})
|
|
|
|
|
2019-02-13 22:20:46 +00:00
|
|
|
const locales = await WIKI.cache.get('locales')
|
2018-05-07 03:21:48 +00:00
|
|
|
if (locales) {
|
2019-02-13 22:20:46 +00:00
|
|
|
const currentLocale = _.find(locales, ['code', localeCode]) || {}
|
2019-07-13 03:56:44 +00:00
|
|
|
const existingLocale = await WIKI.models.locales.query().where('code', localeCode)
|
|
|
|
if (existingLocale) {
|
|
|
|
await WIKI.models.locales.query().patch({
|
|
|
|
strings: lcObj
|
|
|
|
}).where('code', localeCode)
|
|
|
|
} else {
|
|
|
|
await WIKI.models.locales.query().insert({
|
|
|
|
code: localeCode,
|
|
|
|
strings: lcObj,
|
|
|
|
isRTL: currentLocale.isRTL,
|
|
|
|
name: currentLocale.name,
|
|
|
|
nativeName: currentLocale.nativeName,
|
|
|
|
availability: currentLocale.availability
|
|
|
|
})
|
|
|
|
}
|
2018-05-07 03:21:48 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('Failed to fetch cached locales list! Restart server to resolve this issue.')
|
|
|
|
}
|
|
|
|
|
2019-02-13 22:20:46 +00:00
|
|
|
await WIKI.lang.refreshNamespaces()
|
2018-11-18 21:38:00 +00:00
|
|
|
|
2019-02-13 22:20:46 +00:00
|
|
|
WIKI.logger.info(`Fetching locale ${localeCode} from Graph endpoint: [ COMPLETED ]`)
|
2018-05-07 03:21:48 +00:00
|
|
|
} catch (err) {
|
2019-02-13 22:20:46 +00:00
|
|
|
WIKI.logger.error(`Fetching locale ${localeCode} from Graph endpoint: [ FAILED ]`)
|
2018-05-07 03:21:48 +00:00
|
|
|
WIKI.logger.error(err.message)
|
|
|
|
}
|
|
|
|
}
|