Sfoglia il codice sorgente

feat: load dev locale files

Nick 6 anni fa
parent
commit
a8c7710412

+ 2 - 1
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

+ 1 - 1
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

+ 2 - 1
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:

+ 47 - 0
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)

+ 20 - 0
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.