feat: admin - download locale strings from graph
This commit is contained in:
57
server/jobs/fetch-graph-locale.js
Normal file
57
server/jobs/fetch-graph-locale.js
Normal file
@@ -0,0 +1,57 @@
|
||||
require('../core/worker')
|
||||
const _ = require('lodash')
|
||||
const { createApolloFetch } = require('apollo-fetch')
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
WIKI.redis = require('../core/redis').init()
|
||||
WIKI.db = require('../core/db').init()
|
||||
|
||||
const apollo = createApolloFetch({
|
||||
uri: 'https://graph.requarks.io'
|
||||
})
|
||||
|
||||
module.exports = async (job) => {
|
||||
WIKI.logger.info(`Fetching locale ${job.data.locale} from Graph endpoint...`)
|
||||
|
||||
try {
|
||||
const respStrings = await apollo({
|
||||
query: `query ($code: String!) {
|
||||
localization {
|
||||
strings(code: $code) {
|
||||
key
|
||||
value
|
||||
}
|
||||
}
|
||||
}`,
|
||||
variables: {
|
||||
code: job.data.locale
|
||||
}
|
||||
})
|
||||
const strings = _.get(respStrings, 'data.localization.strings', [])
|
||||
let lcObj = {}
|
||||
_.forEach(strings, row => {
|
||||
if (_.includes(row.key, '::')) { return }
|
||||
_.set(lcObj, row.key.replace(':', '.'), row.value)
|
||||
})
|
||||
|
||||
const locales = await WIKI.redis.get('locales')
|
||||
if (locales) {
|
||||
const currentLocale = _.find(JSON.parse(locales), ['code', job.data.locale]) || {}
|
||||
await WIKI.db.Locale.upsert({
|
||||
code: job.data.locale,
|
||||
strings: lcObj,
|
||||
isRTL: currentLocale.isRTL,
|
||||
name: currentLocale.name,
|
||||
nativeName: currentLocale.nativeName
|
||||
})
|
||||
} else {
|
||||
throw new Error('Failed to fetch cached locales list! Restart server to resolve this issue.')
|
||||
}
|
||||
|
||||
WIKI.logger.info(`Fetching locale ${job.data.locale} from Graph endpoint: [ COMPLETED ]`)
|
||||
} catch (err) {
|
||||
WIKI.logger.error(`Fetching locale ${job.data.locale} from Graph endpoint: [ FAILED ]`)
|
||||
WIKI.logger.error(err.message)
|
||||
}
|
||||
}
|
@@ -41,14 +41,17 @@ module.exports = async (job) => {
|
||||
|
||||
if (WIKI.config.site.langAutoUpdate) {
|
||||
const respStrings = await apollo({
|
||||
query: `{
|
||||
query: `query ($code: String!) {
|
||||
localization {
|
||||
strings(code: "${WIKI.config.site.lang}") {
|
||||
strings(code: $code) {
|
||||
key
|
||||
value
|
||||
}
|
||||
}
|
||||
}`
|
||||
}`,
|
||||
variables: {
|
||||
code: WIKI.config.site.lang
|
||||
}
|
||||
})
|
||||
const strings = _.get(respStrings, 'data.localization.strings', [])
|
||||
let lcObj = {}
|
||||
@@ -57,7 +60,7 @@ module.exports = async (job) => {
|
||||
_.set(lcObj, row.key.replace(':', '.'), row.value)
|
||||
})
|
||||
|
||||
WIKI.db.Locale.upsert({
|
||||
await WIKI.db.Locale.upsert({
|
||||
code: WIKI.config.site.lang,
|
||||
strings: lcObj,
|
||||
isRTL: currentLocale.isRTL,
|
||||
|
Reference in New Issue
Block a user