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.

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