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.

76 lines
2.1 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('Syncing locales with Graph endpoint...')
  9. try {
  10. await WIKI.configSvc.loadFromDb()
  11. const apollo = createApolloFetch({
  12. uri: WIKI.config.graphEndpoint
  13. })
  14. // -> Fetch locales list
  15. const respList = await apollo({
  16. query: `{
  17. localization {
  18. locales {
  19. code
  20. name
  21. nativeName
  22. isRTL
  23. createdAt
  24. updatedAt
  25. }
  26. }
  27. }`
  28. })
  29. const locales = _.sortBy(_.get(respList, 'data.localization.locales', []), 'name').map(lc => ({...lc, isInstalled: (lc.code === 'en')}))
  30. WIKI.redis.set('locales', JSON.stringify(locales))
  31. const currentLocale = _.find(locales, ['code', WIKI.config.lang.code])
  32. // -> Download locale strings
  33. if (WIKI.config.lang.autoUpdate) {
  34. const respStrings = await apollo({
  35. query: `query ($code: String!) {
  36. localization {
  37. strings(code: $code) {
  38. key
  39. value
  40. }
  41. }
  42. }`,
  43. variables: {
  44. code: WIKI.config.lang.code
  45. }
  46. })
  47. const strings = _.get(respStrings, 'data.localization.strings', [])
  48. let lcObj = {}
  49. _.forEach(strings, row => {
  50. if (_.includes(row.key, '::')) { return }
  51. _.set(lcObj, row.key.replace(':', '.'), row.value)
  52. })
  53. await WIKI.models.locales.query().update({
  54. code: WIKI.config.lang.code,
  55. strings: lcObj,
  56. isRTL: currentLocale.isRTL,
  57. name: currentLocale.name,
  58. nativeName: currentLocale.nativeName
  59. }).where('code', WIKI.config.lang.code)
  60. }
  61. WIKI.logger.info('Syncing locales with Graph endpoint: [ COMPLETED ]')
  62. } catch (err) {
  63. WIKI.logger.error('Syncing locales with Graph endpoint: [ FAILED ]')
  64. WIKI.logger.error(err.message)
  65. }
  66. }