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.

73 lines
1.9 KiB

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