Compare commits
2 Commits
feature-pl
...
feature-pl
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
57e3e930a7 | ||
|
|
8112bf929c |
@@ -45,7 +45,6 @@ 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 `''`.
|
||||||
|
|||||||
40
index.js
40
index.js
@@ -14,7 +14,6 @@ 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: {},
|
||||||
@@ -23,13 +22,12 @@ const DEFAULT_OPTIONS = {
|
|||||||
dir: undefined,
|
dir: undefined,
|
||||||
dirs: [path.join(process.cwd(), 'locales')],
|
dirs: [path.join(process.cwd(), 'locales')],
|
||||||
functionName: '__',
|
functionName: '__',
|
||||||
functionnName: '__n'
|
functionnName: '__n',
|
||||||
};
|
};
|
||||||
|
|
||||||
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;
|
||||||
@@ -94,9 +92,6 @@ 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;
|
||||||
}
|
}
|
||||||
@@ -141,51 +136,48 @@ module.exports = function (app, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
app[functionName] = gettext;
|
app[functionName] = gettext;
|
||||||
|
|
||||||
|
|
||||||
function gettextn(locale, key, count, value) {
|
function gettextn(locale, key, count, value) {
|
||||||
if (arguments.length === 0 || arguments.length === 1) {
|
if (arguments.length === 0 || arguments.length === 1) {
|
||||||
// __n()
|
// __n()
|
||||||
// __n('en')
|
// __n('en')
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arguments.length === 2) {
|
if (arguments.length === 2) {
|
||||||
// __n('en', key)
|
// __n('en', key)
|
||||||
return gettext(locale, key);
|
return gettext(locale, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
const resource = resources[locale] || {};
|
const resource = resources[locale] || {};
|
||||||
|
|
||||||
// enforce number
|
// enforce number
|
||||||
count = Number(count);
|
count = Number(count);
|
||||||
|
|
||||||
//**************************************************************//
|
//**************************************************************//
|
||||||
// Directly adapted from __n function of i18n module :
|
// Directly adapted from __n function of i18n module :
|
||||||
// https://github.com/mashpie/i18n-node/blob/master/i18n.js
|
// https://github.com/mashpie/i18n-node/blob/master/i18n.js
|
||||||
var p;
|
let p;
|
||||||
// create a new Plural for locale
|
// create a new Plural for locale
|
||||||
// and try to cache instance
|
// and try to cache instance
|
||||||
if (PluralsForLocale[locale]) {
|
if (PluralsForLocale[locale]) {
|
||||||
p = PluralsForLocale[locale];
|
p = PluralsForLocale[locale];
|
||||||
} else {
|
} else {
|
||||||
// split locales with a region code
|
// split locales with a region code
|
||||||
var lc = locale.toLowerCase().split(/[_-\s]+/)
|
const lc = locale.toLowerCase().split(/[_-\s]+/)
|
||||||
.filter(function(el){ return true && el; });
|
.filter(function(el){ return true && el; });
|
||||||
// take the first part of locale, fallback to full locale
|
// take the first part of locale, fallback to full locale
|
||||||
p = MakePlural[lc[0] || locale];
|
p = MakePlural[lc[0] || locale];
|
||||||
PluralsForLocale[locale] = p;
|
PluralsForLocale[locale] = p;
|
||||||
}
|
}
|
||||||
// fallback to 'other' on case of missing translations
|
// fallback to 'other' on case of missing translations
|
||||||
let text = resource[key+"."+p(count)] || resource[key+".other"];
|
let text = resource[key + '.' + p(count)] || resource[key + '.other'];
|
||||||
//**************************************************************//
|
//**************************************************************//
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -227,9 +219,9 @@ module.exports = function (app, options) {
|
|||||||
args[i - 2] = arguments[i];
|
args[i - 2] = arguments[i];
|
||||||
}
|
}
|
||||||
return util.format.apply(util, args);
|
return util.format.apply(util, args);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
app[functionnName] = gettextn;
|
app[functionnName] = gettextn;
|
||||||
|
|
||||||
app.context[functionName] = function (key, value) {
|
app.context[functionName] = function (key, value) {
|
||||||
@@ -252,7 +244,7 @@ module.exports = function (app, options) {
|
|||||||
}
|
}
|
||||||
return gettext.apply(this, args);
|
return gettext.apply(this, args);
|
||||||
};
|
};
|
||||||
|
|
||||||
app.context[functionnName] = function (key, count, value) {
|
app.context[functionnName] = function (key, count, value) {
|
||||||
if (arguments.length === 0) {
|
if (arguments.length === 0) {
|
||||||
// __n()
|
// __n()
|
||||||
@@ -264,7 +256,7 @@ module.exports = function (app, options) {
|
|||||||
// __n(key)
|
// __n(key)
|
||||||
return gettext(locale, key);
|
return gettext(locale, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arguments.length === 2) {
|
if (arguments.length === 2) {
|
||||||
return gettextn(locale, key, count);
|
return gettextn(locale, key, count);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user