You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.8 KiB

  1. require('../core/worker')
  2. const _ = require('lodash')
  3. const { createApolloFetch } = require('apollo-fetch')
  4. /* global WIKI */
  5. WIKI.redis = require('../core/redis').init()
  6. WIKI.models = require('../core/db').init()
  7. module.exports = async (job) => {
  8. WIKI.logger.info(`Fetching locale ${job.data.locale} from Graph endpoint...`)
  9. try {
  10. await WIKI.configSvc.loadFromDb()
  11. const apollo = createApolloFetch({
  12. uri: WIKI.config.graphEndpoint
  13. })
  14. const respStrings = await apollo({
  15. query: `query ($code: String!) {
  16. localization {
  17. strings(code: $code) {
  18. key
  19. value
  20. }
  21. }
  22. }`,
  23. variables: {
  24. code: job.data.locale
  25. }
  26. })
  27. const strings = _.get(respStrings, 'data.localization.strings', [])
  28. let lcObj = {}
  29. _.forEach(strings, row => {
  30. if (_.includes(row.key, '::')) { return }
  31. _.set(lcObj, row.key.replace(':', '.'), row.value)
  32. })
  33. const locales = await WIKI.redis.get('locales')
  34. if (locales) {
  35. const currentLocale = _.find(JSON.parse(locales), ['code', job.data.locale]) || {}
  36. await WIKI.models.locales.query().delete().where('code', job.data.locale)
  37. await WIKI.models.locales.query().insert({
  38. code: job.data.locale,
  39. strings: lcObj,
  40. isRTL: currentLocale.isRTL,
  41. name: currentLocale.name,
  42. nativeName: currentLocale.nativeName
  43. })
  44. } else {
  45. throw new Error('Failed to fetch cached locales list! Restart server to resolve this issue.')
  46. }
  47. WIKI.logger.info(`Fetching locale ${job.data.locale} from Graph endpoint: [ COMPLETED ]`)
  48. } catch (err) {
  49. WIKI.logger.error(`Fetching locale ${job.data.locale} from Graph endpoint: [ FAILED ]`)
  50. WIKI.logger.error(err.message)
  51. }
  52. }