|
@ -3,6 +3,9 @@ const dotize = require('dotize') |
|
|
const i18nMW = require('i18next-express-middleware') |
|
|
const i18nMW = require('i18next-express-middleware') |
|
|
const i18next = require('i18next') |
|
|
const i18next = require('i18next') |
|
|
const Promise = require('bluebird') |
|
|
const Promise = require('bluebird') |
|
|
|
|
|
const fs = require('fs-extra') |
|
|
|
|
|
const path = require('path') |
|
|
|
|
|
const yaml = require('js-yaml') |
|
|
|
|
|
|
|
|
/* global WIKI */ |
|
|
/* global WIKI */ |
|
|
|
|
|
|
|
@ -35,9 +38,20 @@ module.exports = { |
|
|
|
|
|
|
|
|
return this |
|
|
return this |
|
|
}, |
|
|
}, |
|
|
|
|
|
/** |
|
|
|
|
|
* Attach i18n middleware for Express |
|
|
|
|
|
* |
|
|
|
|
|
* @param {Object} app Express Instance |
|
|
|
|
|
*/ |
|
|
attachMiddleware (app) { |
|
|
attachMiddleware (app) { |
|
|
app.use(i18nMW.handle(this.engine)) |
|
|
app.use(i18nMW.handle(this.engine)) |
|
|
}, |
|
|
}, |
|
|
|
|
|
/** |
|
|
|
|
|
* Get all entries for a specific locale and namespace |
|
|
|
|
|
* |
|
|
|
|
|
* @param {String} locale Locale code |
|
|
|
|
|
* @param {String} namespace Namespace |
|
|
|
|
|
*/ |
|
|
async getByNamespace(locale, namespace) { |
|
|
async getByNamespace(locale, namespace) { |
|
|
if (this.engine.hasResourceBundle(locale, namespace)) { |
|
|
if (this.engine.hasResourceBundle(locale, namespace)) { |
|
|
let data = this.engine.getResourceBundle(locale, namespace) |
|
|
let data = this.engine.getResourceBundle(locale, namespace) |
|
@ -51,6 +65,12 @@ module.exports = { |
|
|
throw new Error('Invalid locale or namespace') |
|
|
throw new Error('Invalid locale or namespace') |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
|
|
|
/** |
|
|
|
|
|
* Load entries from the DB for a single locale |
|
|
|
|
|
* |
|
|
|
|
|
* @param {String} locale Locale code |
|
|
|
|
|
* @param {*} opts Additional options |
|
|
|
|
|
*/ |
|
|
async loadLocale(locale, opts = { silent: false }) { |
|
|
async loadLocale(locale, opts = { silent: false }) { |
|
|
const res = await WIKI.models.locales.query().findOne('code', locale) |
|
|
const res = await WIKI.models.locales.query().findOne('code', locale) |
|
|
if (res) { |
|
|
if (res) { |
|
@ -63,7 +83,29 @@ module.exports = { |
|
|
} else if (!opts.silent) { |
|
|
} else if (!opts.silent) { |
|
|
throw new Error('No such locale in local store.') |
|
|
throw new Error('No such locale in local store.') |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//-> Load dev locale files if present
|
|
|
|
|
|
if (WIKI.IS_DEBUG) { |
|
|
|
|
|
try { |
|
|
|
|
|
const devEntriesRaw = await fs.readFileAsync(path.join(WIKI.SERVERPATH, `locales/${locale}.yml`), 'utf8') |
|
|
|
|
|
if (devEntriesRaw) { |
|
|
|
|
|
const devEntries = yaml.safeLoad(devEntriesRaw) |
|
|
|
|
|
_.forOwn(devEntries, (data, ns) => { |
|
|
|
|
|
this.namespaces.push(ns) |
|
|
|
|
|
this.engine.addResourceBundle(locale, ns, data, true, true) |
|
|
|
|
|
}) |
|
|
|
|
|
WIKI.logger.info(`Loaded dev locales from ${locale}.yml`) |
|
|
|
|
|
} |
|
|
|
|
|
} catch (err) { |
|
|
|
|
|
// ignore
|
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
}, |
|
|
}, |
|
|
|
|
|
/** |
|
|
|
|
|
* Reload all namespaces for all active locales from the DB |
|
|
|
|
|
* |
|
|
|
|
|
* @param {Boolean} silent No error on fail |
|
|
|
|
|
*/ |
|
|
async refreshNamespaces (silent = false) { |
|
|
async refreshNamespaces (silent = false) { |
|
|
await this.loadLocale(WIKI.config.lang.code, { silent }) |
|
|
await this.loadLocale(WIKI.config.lang.code, { silent }) |
|
|
if (WIKI.config.lang.namespacing) { |
|
|
if (WIKI.config.lang.namespacing) { |
|
@ -72,6 +114,11 @@ module.exports = { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
|
|
|
/** |
|
|
|
|
|
* Set the active locale |
|
|
|
|
|
* |
|
|
|
|
|
* @param {String} locale Locale code |
|
|
|
|
|
*/ |
|
|
async setCurrentLocale(locale) { |
|
|
async setCurrentLocale(locale) { |
|
|
await Promise.fromCallback(cb => { |
|
|
await Promise.fromCallback(cb => { |
|
|
return this.engine.changeLanguage(locale, cb) |
|
|
return this.engine.changeLanguage(locale, cb) |
|
|