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.

57 lines
1.7 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. if (_.isEmpty(row.value)) { row.value = row.key }
  28. _.set(lcObj, row.key.replace(':', '.'), row.value)
  29. })
  30. const locales = await WIKI.cache.get('locales')
  31. if (locales) {
  32. const currentLocale = _.find(locales, ['code', localeCode]) || {}
  33. await WIKI.models.locales.query().delete().where('code', localeCode)
  34. await WIKI.models.locales.query().insert({
  35. code: localeCode,
  36. strings: lcObj,
  37. isRTL: currentLocale.isRTL,
  38. name: currentLocale.name,
  39. nativeName: currentLocale.nativeName
  40. })
  41. } else {
  42. throw new Error('Failed to fetch cached locales list! Restart server to resolve this issue.')
  43. }
  44. await WIKI.lang.refreshNamespaces()
  45. WIKI.logger.info(`Fetching locale ${localeCode} from Graph endpoint: [ COMPLETED ]`)
  46. } catch (err) {
  47. WIKI.logger.error(`Fetching locale ${localeCode} from Graph endpoint: [ FAILED ]`)
  48. WIKI.logger.error(err.message)
  49. }
  50. }