Compare commits
2 Commits
feature-pl
...
feature-pl
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
68d3ae4dd4 | ||
|
|
86598190f2 |
@@ -45,6 +45,7 @@ Patch locales functions to koa app.
|
|||||||
- {String} functionnName: locale function (with plurals management) name patch on koa context. Optional, default is `__n`.
|
- {String} functionnName: locale function (with plurals management) name patch on koa context. Optional, default is `__n`.
|
||||||
- {String} dirs: locales resources store directories. Optional, default is `['$PWD/locales']`.
|
- {String} dirs: locales resources store directories. Optional, default is `['$PWD/locales']`.
|
||||||
- {String} defaultLocale: default locale. Optional, default is `en-US`.
|
- {String} defaultLocale: default locale. Optional, default is `en-US`.
|
||||||
|
- {Boolean} fallbackToDefaultLocale: fall back to default local if current translation is not found. Optional, default is `false`.
|
||||||
- {String} queryField: locale field name on query. Optional, default is `locale`.
|
- {String} queryField: locale field name on query. Optional, default is `locale`.
|
||||||
- {String} cookieField: locale field name on cookie. Optional, default is `locale`.
|
- {String} cookieField: locale field name on cookie. Optional, default is `locale`.
|
||||||
- {String} cookieDomain: domain on cookie. Optional, default is `''`.
|
- {String} cookieDomain: domain on cookie. Optional, default is `''`.
|
||||||
|
|||||||
9
index.js
9
index.js
@@ -14,6 +14,7 @@ const MakePlural = require('make-plural');
|
|||||||
|
|
||||||
const DEFAULT_OPTIONS = {
|
const DEFAULT_OPTIONS = {
|
||||||
defaultLocale: 'en-US',
|
defaultLocale: 'en-US',
|
||||||
|
fallbackToDefaultLocale: 'false',
|
||||||
queryField: 'locale',
|
queryField: 'locale',
|
||||||
cookieField: 'locale',
|
cookieField: 'locale',
|
||||||
localeAlias: {},
|
localeAlias: {},
|
||||||
@@ -28,6 +29,7 @@ const DEFAULT_OPTIONS = {
|
|||||||
module.exports = function (app, options) {
|
module.exports = function (app, options) {
|
||||||
options = assign({}, DEFAULT_OPTIONS, options);
|
options = assign({}, DEFAULT_OPTIONS, options);
|
||||||
const defaultLocale = formatLocale(options.defaultLocale);
|
const defaultLocale = formatLocale(options.defaultLocale);
|
||||||
|
const fallbackToDefaultLocale = options.fallbackToDefaultLocale;
|
||||||
const queryField = options.queryField;
|
const queryField = options.queryField;
|
||||||
const cookieField = options.cookieField;
|
const cookieField = options.cookieField;
|
||||||
const cookieDomain = options.cookieDomain;
|
const cookieDomain = options.cookieDomain;
|
||||||
@@ -92,6 +94,9 @@ module.exports = function (app, options) {
|
|||||||
const resource = resources[locale] || {};
|
const resource = resources[locale] || {};
|
||||||
|
|
||||||
let text = resource[key];
|
let text = resource[key];
|
||||||
|
if (text === undefined && fallbackToDefaultLocale === true) {
|
||||||
|
text = resources[defaultLocale][key];
|
||||||
|
}
|
||||||
if (text === undefined) {
|
if (text === undefined) {
|
||||||
text = key;
|
text = key;
|
||||||
}
|
}
|
||||||
@@ -178,7 +183,9 @@ module.exports = function (app, options) {
|
|||||||
if (text === undefined && isObject(key)) {
|
if (text === undefined && isObject(key)) {
|
||||||
text = key[p(count)] || key["other"];
|
text = key[p(count)] || key["other"];
|
||||||
}
|
}
|
||||||
|
if (text === undefined && fallbackToDefaultLocale === true) {
|
||||||
|
text = resources[defaultLocale][key+"."+p(count)] || resources[defaultLocale][key+".other"];
|
||||||
|
}
|
||||||
if (text === undefined) {
|
if (text === undefined) {
|
||||||
text = key;
|
text = key;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user