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.

84 lines
2.4 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. availability
  16. code
  17. name
  18. nativeName
  19. isRTL
  20. createdAt
  21. updatedAt
  22. }
  23. }
  24. }`
  25. })
  26. const locales = _.sortBy(_.get(respList, 'data.localization.locales', []), 'name').map(lc => ({...lc, isInstalled: (lc.code === 'en')}))
  27. WIKI.cache.set('locales', locales)
  28. // -> Download locale strings
  29. if (WIKI.config.lang.autoUpdate) {
  30. const activeLocales = WIKI.config.lang.namespacing ? WIKI.config.lang.namespaces : [WIKI.config.lang.code]
  31. for (const currentLocale of activeLocales) {
  32. const localeInfo = _.find(locales, ['code', currentLocale])
  33. const respStrings = await apollo({
  34. query: `query ($code: String!) {
  35. localization {
  36. strings(code: $code) {
  37. key
  38. value
  39. }
  40. }
  41. }`,
  42. variables: {
  43. code: currentLocale
  44. }
  45. })
  46. const strings = _.get(respStrings, 'data.localization.strings', [])
  47. let lcObj = {}
  48. _.forEach(strings, row => {
  49. if (_.includes(row.key, '::')) { return }
  50. if (_.isEmpty(row.value)) {
  51. row.value = row.key
  52. }
  53. _.set(lcObj, row.key.replace(':', '.'), row.value)
  54. })
  55. await WIKI.models.locales.query().update({
  56. code: currentLocale,
  57. strings: lcObj,
  58. isRTL: localeInfo.isRTL,
  59. name: localeInfo.name,
  60. nativeName: localeInfo.nativeName,
  61. availability: localeInfo.availability
  62. }).where('code', currentLocale)
  63. WIKI.logger.info(`Pulled latest locale updates for ${localeInfo.name} from Graph endpoint: [ COMPLETED ]`)
  64. }
  65. }
  66. await WIKI.lang.refreshNamespaces()
  67. WIKI.logger.info('Syncing locales with Graph endpoint: [ COMPLETED ]')
  68. } catch (err) {
  69. WIKI.logger.error('Syncing locales with Graph endpoint: [ FAILED ]')
  70. WIKI.logger.error(err.message)
  71. }
  72. }