feat: load dev locale files

This commit is contained in:
Nick 2019-02-09 12:32:23 -05:00
parent 466c05f8b2
commit a8c7710412
5 changed files with 72 additions and 3 deletions

View File

@ -2,12 +2,13 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/). 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
- Added Patreon link in Contribute admin page - Added Patreon link in Contribute admin page
- Added Theme Code Injection functionality - Added Theme Code Injection functionality
- Added Theme CSS Injection code minification - Added Theme CSS Injection code minification
- Added Page Delete functionality - Added Page Delete functionality
- Dev locale .yml files in `server/locales` are now loaded
### Fixed ### Fixed
- Fixed root admin refresh token fail - Fixed root admin refresh token fail

View File

@ -6,7 +6,7 @@
img(src='/svg/icon-cloud-storage.svg', alt='Storage', style='width: 80px;') img(src='/svg/icon-cloud-storage.svg', alt='Storage', style='width: 80px;')
.admin-header-title .admin-header-title
.headline.primary--text Storage .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-spacer
v-btn(outline, color='grey', @click='refresh', large) v-btn(outline, color='grey', @click='refresh', large)
v-icon refresh v-icon refresh

View File

@ -7,7 +7,6 @@ defaults:
config: config:
# File defaults # File defaults
port: 80 port: 80
bindIP: 0.0.0.0
db: db:
type: postgres type: postgres
host: localhost host: localhost
@ -23,6 +22,8 @@ defaults:
password: null password: null
ssl: ssl:
enabled: false enabled: false
bindIP: 0.0.0.0
logLevel: info
# DB defaults # DB defaults
graphEndpoint: 'https://graph.requarks.io' graphEndpoint: 'https://graph.requarks.io'
lang: lang:

View File

@ -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)

20
server/locales/README.md Normal file
View File

@ -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.