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.

79 lines
2.5 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.cache.get('locales')
  14. let localLocales = await WIKI.models.locales.query().select('code', 'isRTL', 'name', 'nativeName', 'createdAt', 'updatedAt', 'availability')
  15. remoteLocales = 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. translations (obj, args, context, info) {
  34. return WIKI.lang.getByNamespace(args.locale, args.namespace)
  35. }
  36. },
  37. LocalizationMutation: {
  38. async downloadLocale(obj, args, context) {
  39. try {
  40. const job = await WIKI.scheduler.registerJob({
  41. name: 'fetch-graph-locale',
  42. immediate: true
  43. }, args.locale)
  44. await job.finished
  45. return {
  46. responseResult: graphHelper.generateSuccess('Locale downloaded successfully')
  47. }
  48. } catch (err) {
  49. return graphHelper.generateError(err)
  50. }
  51. },
  52. async updateLocale(obj, args, context) {
  53. try {
  54. WIKI.config.lang.code = args.locale
  55. WIKI.config.lang.autoUpdate = args.autoUpdate
  56. WIKI.config.lang.namespacing = args.namespacing
  57. WIKI.config.lang.namespaces = _.union(args.namespaces, [args.locale])
  58. const newLocale = await WIKI.models.locales.query().select('isRTL').where('code', args.locale).first()
  59. WIKI.config.lang.rtl = newLocale.isRTL
  60. await WIKI.configSvc.saveToDb(['lang'])
  61. await WIKI.lang.setCurrentLocale(args.locale)
  62. await WIKI.lang.refreshNamespaces()
  63. await WIKI.cache.del('nav:locales')
  64. return {
  65. responseResult: graphHelper.generateSuccess('Locale config updated')
  66. }
  67. } catch (err) {
  68. return graphHelper.generateError(err)
  69. }
  70. }
  71. }
  72. }