localization.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const _ = require('lodash')
  2. const dotize = require('dotize')
  3. const i18nMW = require('i18next-express-middleware')
  4. const i18next = require('i18next')
  5. const Promise = require('bluebird')
  6. /* global WIKI */
  7. module.exports = {
  8. engine: null,
  9. namespaces: [],
  10. init() {
  11. this.namespaces = WIKI.data.localeNamespaces
  12. this.engine = i18next
  13. this.engine.init({
  14. load: 'languageOnly',
  15. ns: this.namespaces,
  16. defaultNS: 'common',
  17. saveMissing: false,
  18. lng: WIKI.config.lang,
  19. fallbackLng: 'en'
  20. })
  21. // Load fallback defaults
  22. const enFallback = require('../locales/default.json')
  23. if (_.isPlainObject(enFallback)) {
  24. _.forOwn(enFallback, (data, ns) => {
  25. this.namespaces.push(ns)
  26. this.engine.addResourceBundle('en', ns, data)
  27. })
  28. }
  29. // Load current language
  30. this.loadLocale(WIKI.config.lang, { silent: true })
  31. return this
  32. },
  33. attachMiddleware (app) {
  34. app.use(i18nMW.handle(this.engine))
  35. },
  36. async getByNamespace(locale, namespace) {
  37. if (this.engine.hasResourceBundle(locale, namespace)) {
  38. let data = this.engine.getResourceBundle(locale, namespace)
  39. return _.map(dotize.convert(data), (value, key) => {
  40. return {
  41. key,
  42. value
  43. }
  44. })
  45. } else {
  46. throw new Error('Invalid locale or namespace')
  47. }
  48. },
  49. async loadLocale(locale, opts = { silent: false }) {
  50. const res = await WIKI.db.locales.query().findOne('code', locale)
  51. if (res) {
  52. if (_.isPlainObject(res.strings)) {
  53. console.info(res.strings)
  54. _.forOwn(res.strings, (data, ns) => {
  55. this.namespaces.push(ns)
  56. this.engine.addResourceBundle(locale, ns, data, true, true)
  57. })
  58. }
  59. } else if (!opts.silent) {
  60. throw new Error('No such locale in local store.')
  61. }
  62. },
  63. async setCurrentLocale(locale) {
  64. return Promise.fromCallback(cb => {
  65. return this.engine.changeLanguage(locale, cb)
  66. })
  67. }
  68. }