Skip to content

Commit

Permalink
feat: load dev locale files
Browse files Browse the repository at this point in the history
  • Loading branch information
NGPixel committed Feb 9, 2019
1 parent 466c05f commit a8c7710
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion client/components/admin/admin-storage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion server/app/data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ defaults:
config:
# File defaults
port: 80
bindIP: 0.0.0.0
db:
type: postgres
host: localhost
Expand All @@ -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:
Expand Down
47 changes: 47 additions & 0 deletions server/core/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions server/locales/README.md
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit a8c7710

Please sign in to comment.