2017-09-25 03:22:33 +00:00
|
|
|
const _ = require('lodash')
|
|
|
|
const dotize = require('dotize')
|
2018-02-04 05:53:13 +00:00
|
|
|
const i18nMW = require('i18next-express-middleware')
|
2017-09-25 03:22:33 +00:00
|
|
|
const i18next = require('i18next')
|
|
|
|
const Promise = require('bluebird')
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-09-25 03:22:33 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
engine: null,
|
2018-01-10 01:41:53 +00:00
|
|
|
namespaces: [],
|
2017-09-25 03:22:33 +00:00
|
|
|
init() {
|
2018-03-05 20:49:36 +00:00
|
|
|
this.namespaces = WIKI.data.localeNamespaces
|
2017-09-25 03:22:33 +00:00
|
|
|
this.engine = i18next
|
2018-04-29 21:55:36 +00:00
|
|
|
this.engine.init({
|
2017-09-25 03:22:33 +00:00
|
|
|
load: 'languageOnly',
|
|
|
|
ns: this.namespaces,
|
|
|
|
defaultNS: 'common',
|
|
|
|
saveMissing: false,
|
2018-05-28 23:36:35 +00:00
|
|
|
lng: WIKI.config.lang.code,
|
2018-04-29 21:55:36 +00:00
|
|
|
fallbackLng: 'en'
|
2017-09-25 03:22:33 +00:00
|
|
|
})
|
2018-05-06 20:19:37 +00:00
|
|
|
|
|
|
|
// Load fallback defaults
|
|
|
|
const enFallback = require('../locales/default.json')
|
|
|
|
if (_.isPlainObject(enFallback)) {
|
|
|
|
_.forOwn(enFallback, (data, ns) => {
|
|
|
|
this.namespaces.push(ns)
|
|
|
|
this.engine.addResourceBundle('en', ns, data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-17 21:29:19 +00:00
|
|
|
// Load current language + namespaces
|
|
|
|
this.refreshNamespaces(true)
|
2018-05-06 20:19:37 +00:00
|
|
|
|
2017-09-25 03:22:33 +00:00
|
|
|
return this
|
|
|
|
},
|
2018-02-04 05:53:13 +00:00
|
|
|
attachMiddleware (app) {
|
|
|
|
app.use(i18nMW.handle(this.engine))
|
|
|
|
},
|
2018-01-10 01:41:53 +00:00
|
|
|
async getByNamespace(locale, namespace) {
|
2017-09-25 03:22:33 +00:00
|
|
|
if (this.engine.hasResourceBundle(locale, namespace)) {
|
|
|
|
let data = this.engine.getResourceBundle(locale, namespace)
|
|
|
|
return _.map(dotize.convert(data), (value, key) => {
|
|
|
|
return {
|
|
|
|
key,
|
|
|
|
value
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
throw new Error('Invalid locale or namespace')
|
|
|
|
}
|
|
|
|
},
|
2018-05-06 20:19:37 +00:00
|
|
|
async loadLocale(locale, opts = { silent: false }) {
|
2018-07-30 02:23:33 +00:00
|
|
|
const res = await WIKI.models.locales.query().findOne('code', locale)
|
2018-05-06 20:19:37 +00:00
|
|
|
if (res) {
|
|
|
|
if (_.isPlainObject(res.strings)) {
|
|
|
|
_.forOwn(res.strings, (data, ns) => {
|
|
|
|
this.namespaces.push(ns)
|
|
|
|
this.engine.addResourceBundle(locale, ns, data, true, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else if (!opts.silent) {
|
|
|
|
throw new Error('No such locale in local store.')
|
|
|
|
}
|
2017-09-25 03:22:33 +00:00
|
|
|
},
|
2018-06-17 21:29:19 +00:00
|
|
|
async refreshNamespaces (silent = false) {
|
|
|
|
await this.loadLocale(WIKI.config.lang.code, { silent })
|
|
|
|
if (WIKI.config.lang.namespacing) {
|
|
|
|
for (let ns of WIKI.config.lang.namespaces) {
|
|
|
|
await this.loadLocale(ns, { silent })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2018-01-10 01:41:53 +00:00
|
|
|
async setCurrentLocale(locale) {
|
2018-06-17 21:29:19 +00:00
|
|
|
await Promise.fromCallback(cb => {
|
2017-09-25 03:22:33 +00:00
|
|
|
return this.engine.changeLanguage(locale, cb)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|