localization.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const graphHelper = require('../../helpers/graph')
  2. const _ = require('lodash')
  3. /* global WIKI */
  4. module.exports = {
  5. Query: {
  6. async localization() { return {} }
  7. },
  8. Mutation: {
  9. async localization() { return {} }
  10. },
  11. LocalizationQuery: {
  12. async locales(obj, args, context, info) {
  13. let remoteLocales = await WIKI.redis.get('locales')
  14. let localLocales = await WIKI.db.locales.query().select('id', 'code', 'isRTL', 'name', 'nativeName', 'createdAt', 'updatedAt')
  15. remoteLocales = (remoteLocales) ? JSON.parse(remoteLocales) : localLocales
  16. return _.map(remoteLocales, rl => {
  17. let isInstalled = _.some(localLocales, ['code', rl.code])
  18. return {
  19. ...rl,
  20. isInstalled,
  21. installDate: isInstalled ? _.find(localLocales, ['code', rl.code]).updatedAt : null
  22. }
  23. })
  24. },
  25. async config(obj, args, context, info) {
  26. return {
  27. locale: WIKI.config.site.lang,
  28. autoUpdate: WIKI.config.site.langAutoUpdate
  29. }
  30. }
  31. },
  32. LocalizationMutation: {
  33. async downloadLocale(obj, args, context) {
  34. try {
  35. const job = await WIKI.queue.job.fetchGraphLocale.add({
  36. locale: args.locale
  37. }, {
  38. timeout: 30000
  39. })
  40. await job.finished()
  41. return {
  42. responseResult: graphHelper.generateSuccess('Locale downloaded successfully')
  43. }
  44. } catch (err) {
  45. return graphHelper.generateError(err)
  46. }
  47. },
  48. async updateLocale(obj, args, context) {
  49. try {
  50. WIKI.config.site.lang = args.locale
  51. WIKI.config.site.langAutoUpdate = args.autoUpdate
  52. await WIKI.configSvc.saveToDb(['site'])
  53. await WIKI.lang.setCurrentLocale(args.locale)
  54. return {
  55. responseResult: graphHelper.generateSuccess('Locale config updated')
  56. }
  57. } catch (err) {
  58. return graphHelper.generateError(err)
  59. }
  60. }
  61. }
  62. }