localization.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const graphHelper = require('../../helpers/graph')
  2. const _ = require('lodash')
  3. /* global WIKI */
  4. module.exports = {
  5. Query: {
  6. async locales(obj, args, context, info) {
  7. let remoteLocales = await WIKI.cache.get('locales')
  8. let localLocales = await WIKI.models.locales.query().select('code', 'isRTL', 'name', 'nativeName', 'createdAt', 'updatedAt', 'availability')
  9. remoteLocales = remoteLocales || localLocales
  10. return _.map(remoteLocales, rl => {
  11. let isInstalled = _.some(localLocales, ['code', rl.code])
  12. return {
  13. ...rl,
  14. isInstalled,
  15. installDate: isInstalled ? _.find(localLocales, ['code', rl.code]).updatedAt : null
  16. }
  17. })
  18. },
  19. translations (obj, args, context, info) {
  20. return WIKI.lang.getByNamespace(args.locale, args.namespace)
  21. }
  22. },
  23. Mutation: {
  24. async downloadLocale(obj, args, context) {
  25. try {
  26. const job = await WIKI.scheduler.registerJob({
  27. name: 'fetch-graph-locale',
  28. immediate: true
  29. }, args.locale)
  30. await job.finished
  31. return {
  32. responseResult: graphHelper.generateSuccess('Locale downloaded successfully')
  33. }
  34. } catch (err) {
  35. return graphHelper.generateError(err)
  36. }
  37. },
  38. async updateLocale(obj, args, context) {
  39. try {
  40. WIKI.config.lang.code = args.locale
  41. WIKI.config.lang.autoUpdate = args.autoUpdate
  42. WIKI.config.lang.namespacing = args.namespacing
  43. WIKI.config.lang.namespaces = _.union(args.namespaces, [args.locale])
  44. const newLocale = await WIKI.models.locales.query().select('isRTL').where('code', args.locale).first()
  45. WIKI.config.lang.rtl = newLocale.isRTL
  46. await WIKI.configSvc.saveToDb(['lang'])
  47. await WIKI.lang.setCurrentLocale(args.locale)
  48. await WIKI.lang.refreshNamespaces()
  49. await WIKI.cache.del('nav:locales')
  50. return {
  51. responseResult: graphHelper.generateSuccess('Locale config updated')
  52. }
  53. } catch (err) {
  54. return graphHelper.generateError(err)
  55. }
  56. }
  57. }
  58. }