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.

56 lines
1.6 KiB

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