2018-04-29 21:55:36 +00:00
|
|
|
const _ = require('lodash')
|
|
|
|
const { createApolloFetch } = require('apollo-fetch')
|
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
2019-02-13 22:20:46 +00:00
|
|
|
module.exports = async () => {
|
2018-04-29 21:55:36 +00:00
|
|
|
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({
|
2018-04-29 21:55:36 +00:00
|
|
|
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')}))
|
2019-02-13 22:20:46 +00:00
|
|
|
WIKI.cache.set('locales', locales)
|
2018-05-28 23:36:35 +00:00
|
|
|
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({
|
2018-05-07 03:21:48 +00:00
|
|
|
query: `query ($code: String!) {
|
2018-05-06 20:19:37 +00:00
|
|
|
localization {
|
2018-05-07 03:21:48 +00:00
|
|
|
strings(code: $code) {
|
2018-05-06 20:19:37 +00:00
|
|
|
key
|
|
|
|
value
|
|
|
|
}
|
|
|
|
}
|
2018-05-07 03:21:48 +00:00
|
|
|
}`,
|
|
|
|
variables: {
|
2018-05-28 23:36:35 +00:00
|
|
|
code: WIKI.config.lang.code
|
2018-05-07 03:21:48 +00:00
|
|
|
}
|
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)
|
|
|
|
})
|
|
|
|
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.locales.query().update({
|
2018-05-28 23:36:35 +00:00
|
|
|
code: WIKI.config.lang.code,
|
2018-05-06 20:19:37 +00:00
|
|
|
strings: lcObj,
|
|
|
|
isRTL: currentLocale.isRTL,
|
|
|
|
name: currentLocale.name,
|
|
|
|
nativeName: currentLocale.nativeName
|
2018-05-28 23:36:35 +00:00
|
|
|
}).where('code', WIKI.config.lang.code)
|
2018-05-06 20:19:37 +00:00
|
|
|
}
|
2018-04-29 21:55:36 +00:00
|
|
|
|
2019-02-13 22:20:46 +00:00
|
|
|
await WIKI.lang.refreshNamespaces()
|
2018-11-18 21:38:00 +00:00
|
|
|
|
2018-04-29 21:55:36 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|