From a8c7710412019507ec38b4a652bd64fb956a2c31 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 9 Feb 2019 12:32:23 -0500 Subject: [PATCH] feat: load dev locale files --- CHANGELOG.md | 3 +- client/components/admin/admin-storage.vue | 2 +- server/app/data.yml | 3 +- server/core/localization.js | 47 +++++++++++++++++++++++ server/locales/README.md | 20 ++++++++++ 5 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 server/locales/README.md diff --git a/CHANGELOG.md b/CHANGELOG.md index bd82a818..3cb23b0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,12 +2,13 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). -## [2.0.0-beta.12] - 2018-01-27 +## [2.0.0-beta.XX] - 2018-XX-XX ### Added - Added Patreon link in Contribute admin page - Added Theme Code Injection functionality - Added Theme CSS Injection code minification - Added Page Delete functionality +- Dev locale .yml files in `server/locales` are now loaded ### Fixed - Fixed root admin refresh token fail diff --git a/client/components/admin/admin-storage.vue b/client/components/admin/admin-storage.vue index 5af7f099..87de0154 100644 --- a/client/components/admin/admin-storage.vue +++ b/client/components/admin/admin-storage.vue @@ -6,7 +6,7 @@ img(src='/svg/icon-cloud-storage.svg', alt='Storage', style='width: 80px;') .admin-header-title .headline.primary--text Storage - .subheading.grey--text Set backup and sync targets for your content #[v-chip(label, color='primary', small).white--text coming soon] + .subheading.grey--text Set backup and sync targets for your content v-spacer v-btn(outline, color='grey', @click='refresh', large) v-icon refresh diff --git a/server/app/data.yml b/server/app/data.yml index fcc53d8a..351866d1 100644 --- a/server/app/data.yml +++ b/server/app/data.yml @@ -7,7 +7,6 @@ defaults: config: # File defaults port: 80 - bindIP: 0.0.0.0 db: type: postgres host: localhost @@ -23,6 +22,8 @@ defaults: password: null ssl: enabled: false + bindIP: 0.0.0.0 + logLevel: info # DB defaults graphEndpoint: 'https://graph.requarks.io' lang: diff --git a/server/core/localization.js b/server/core/localization.js index 2dd7d1de..dd6699e2 100644 --- a/server/core/localization.js +++ b/server/core/localization.js @@ -3,6 +3,9 @@ const dotize = require('dotize') const i18nMW = require('i18next-express-middleware') const i18next = require('i18next') const Promise = require('bluebird') +const fs = require('fs-extra') +const path = require('path') +const yaml = require('js-yaml') /* global WIKI */ @@ -35,9 +38,20 @@ module.exports = { return this }, + /** + * Attach i18n middleware for Express + * + * @param {Object} app Express Instance + */ attachMiddleware (app) { 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) { if (this.engine.hasResourceBundle(locale, namespace)) { let data = this.engine.getResourceBundle(locale, namespace) @@ -51,6 +65,12 @@ module.exports = { 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 }) { const res = await WIKI.models.locales.query().findOne('code', locale) if (res) { @@ -63,7 +83,29 @@ module.exports = { } else if (!opts.silent) { 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) { await this.loadLocale(WIKI.config.lang.code, { silent }) if (WIKI.config.lang.namespacing) { @@ -72,6 +114,11 @@ module.exports = { } } }, + /** + * Set the active locale + * + * @param {String} locale Locale code + */ async setCurrentLocale(locale) { await Promise.fromCallback(cb => { return this.engine.changeLanguage(locale, cb) diff --git a/server/locales/README.md b/server/locales/README.md new file mode 100644 index 00000000..9777fe80 --- /dev/null +++ b/server/locales/README.md @@ -0,0 +1,20 @@ +## IMPORTANT + +Localization files are not stored into files! + +Contact us on Gitter to request access to the translation web service: https://gitter.im/Requarks/wiki + +## Development Mode + +If you need to add new keys and test them live, simply create a {LANG}.yml file in this folder containing the values you want to test. e.g.: + +### en.yml +```yml +admin: + api.title: 'API Access' + auth.title: 'Authentication' +``` + +The official localization keys will still be loaded first, but your local files will overwrite any existing keys (and add new ones). + +Note that you must restart Wiki.js to load any changes made to the files, which happens automatically on save when in dev mode.