koa-locales
Advanced tools
Comparing version 1.11.0 to 1.12.0
1.12.0 / 2019-06-16 | ||
================== | ||
**features** | ||
* [[`47162d3`](http://github.com/koajs/locales/commit/47162d3230427957bfb16644f4db6e3c5490b830)] - feat: gettext from app (#38) (fengmk2 <<fengmk2@gmail.com>>) | ||
1.11.0 / 2019-04-30 | ||
@@ -3,0 +9,0 @@ ================== |
57
index.js
@@ -74,9 +74,13 @@ 'use strict'; | ||
app.context[functionName] = function (key, value) { | ||
if (arguments.length === 0) { | ||
if (typeof app[functionName] !== 'undefined') { | ||
console.warn('[koa-locales] will override exists "%s" function on app', functionName); | ||
} | ||
function gettext(locale, key, value) { | ||
if (arguments.length === 0 || arguments.length === 1) { | ||
// __() | ||
// --('en') | ||
return ''; | ||
} | ||
const locale = this.__getLocale(); | ||
const resource = resources[locale] || {}; | ||
@@ -94,10 +98,10 @@ | ||
if (arguments.length === 1) { | ||
// __(key) | ||
if (arguments.length === 2) { | ||
// __(locale, key) | ||
return text; | ||
} | ||
if (arguments.length === 2) { | ||
if (arguments.length === 3) { | ||
if (isObject(value)) { | ||
// __(key, object) | ||
// __('{a} {b} {b} {a}', {a: 'foo', b: 'bar'}) | ||
// __(locale, key, object) | ||
// __('zh', '{a} {b} {b} {a}', {a: 'foo', b: 'bar'}) | ||
// => | ||
@@ -109,4 +113,4 @@ // foo bar bar foo | ||
if (Array.isArray(value)) { | ||
// __(key, array) | ||
// __('{0} {1} {1} {0}', ['foo', 'bar']) | ||
// __(locale, key, array) | ||
// __('zh', '{0} {1} {1} {0}', ['foo', 'bar']) | ||
// => | ||
@@ -117,13 +121,36 @@ // foo bar bar foo | ||
// __(key, value) | ||
// __(locale, key, value) | ||
return util.format(text, value); | ||
} | ||
// __(key, value1, ...) | ||
const args = new Array(arguments.length); | ||
// __(locale, key, value1, ...) | ||
const args = new Array(arguments.length - 1); | ||
args[0] = text; | ||
for(let i = 1; i < args.length; i++) { | ||
args[i] = arguments[i]; | ||
for(let i = 2; i < arguments.length; i++) { | ||
args[i - 1] = arguments[i]; | ||
} | ||
return util.format.apply(util, args); | ||
} | ||
app[functionName] = gettext; | ||
app.context[functionName] = function (key, value) { | ||
if (arguments.length === 0) { | ||
// __() | ||
return ''; | ||
} | ||
const locale = this.__getLocale(); | ||
if (arguments.length === 1) { | ||
return gettext(locale, key); | ||
} | ||
if (arguments.length === 2) { | ||
return gettext(locale, key, value); | ||
} | ||
const args = new Array(arguments.length + 1); | ||
args[0] = locale; | ||
for(let i = 0; i < arguments.length; i++) { | ||
args[i + 1] = arguments[i]; | ||
} | ||
return gettext.apply(this, args); | ||
}; | ||
@@ -130,0 +157,0 @@ |
{ | ||
"name": "koa-locales", | ||
"version": "1.11.0", | ||
"version": "1.12.0", | ||
"description": "koa locales, i18n solution for koa", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -79,5 +79,5 @@ koa-locales | ||
```js | ||
function* home() { | ||
this.body = { | ||
message: this.__('Hello, %s', 'fengmk2'), | ||
async function home(ctx) { | ||
ctx.body = { | ||
message: ctx.__('Hello, %s', 'fengmk2'), | ||
}; | ||
@@ -115,2 +115,11 @@ } | ||
### `app.__(locale, key[, value1[, value2, ...]])` | ||
Get the given locale text on application level. | ||
```js | ||
console.log(app.__('zh', 'Hello')); | ||
// stdout '你好' for Chinese | ||
``` | ||
## Usage on template | ||
@@ -117,0 +126,0 @@ |
18230
267
188