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.

70 lines
2.1 KiB

  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.lang.code,
  28. autoUpdate: WIKI.config.lang.autoUpdate,
  29. namespacing: WIKI.config.lang.namespacing,
  30. namespaces: WIKI.config.lang.namespaces
  31. }
  32. }
  33. },
  34. LocalizationMutation: {
  35. async downloadLocale(obj, args, context) {
  36. try {
  37. const job = await WIKI.queue.job.fetchGraphLocale.add({
  38. locale: args.locale
  39. }, {
  40. timeout: 30000
  41. })
  42. await job.finished()
  43. return {
  44. responseResult: graphHelper.generateSuccess('Locale downloaded successfully')
  45. }
  46. } catch (err) {
  47. return graphHelper.generateError(err)
  48. }
  49. },
  50. async updateLocale(obj, args, context) {
  51. try {
  52. WIKI.config.lang.code = args.locale
  53. WIKI.config.lang.autoUpdate = args.autoUpdate
  54. WIKI.config.lang.namespacing = args.namespacing
  55. WIKI.config.lang.namespaces = args.namespaces
  56. await WIKI.configSvc.saveToDb(['lang'])
  57. await WIKI.lang.setCurrentLocale(args.locale)
  58. return {
  59. responseResult: graphHelper.generateSuccess('Locale config updated')
  60. }
  61. } catch (err) {
  62. return graphHelper.generateError(err)
  63. }
  64. }
  65. }
  66. }