diff --git a/README.md b/README.md index 456955a..09ce36b 100644 --- a/README.md +++ b/README.md @@ -76,9 +76,9 @@ locales({ Get current request locale text. ```js -function* home() { - this.body = { - message: this.__('Hello, %s', 'fengmk2'), +async function home(ctx) { + ctx.body = { + message: ctx.__('Hello, %s', 'fengmk2'), }; } ``` @@ -111,6 +111,15 @@ Set locale and cookie. Where does locale come from, could be `query`, `cookie`, `header` and `default`. +### `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 ```js diff --git a/index.js b/index.js index 2614787..9f3ee2b 100644 --- a/index.js +++ b/index.js @@ -71,13 +71,17 @@ module.exports = function (app, options) { debug('Init locales with %j, got %j resources', options, Object.keys(resources)); - 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] || {}; let text = resource[key]; @@ -90,38 +94,61 @@ module.exports = function (app, options) { return ''; } - 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'}) // => // foo bar bar foo return formatWithObject(text, value); } if (Array.isArray(value)) { - // __(key, array) - // __('{0} {1} {1} {0}', ['foo', 'bar']) + // __(locale, key, array) + // __('zh', '{0} {1} {1} {0}', ['foo', 'bar']) // => // foo bar bar foo return formatWithArray(text, value); } - // __(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); }; // 1. query: /?locale=en-US diff --git a/test/index.test.js b/test/index.test.js index b2a8a4f..e0e9a40 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -5,10 +5,9 @@ const koa = require('koa'); const request = require('supertest'); const pedding = require('pedding'); const mm = require('mm'); -const locales = require('../'); +const locales = require('..'); describe('koa-locales.test.js', function () { - afterEach(mm.restore); describe('default options', function () { @@ -122,6 +121,15 @@ describe('koa-locales.test.js', function () { .expect(200, done); }); + it('should gettext work on app.__(locale, key, value)', function (done) { + request(app.callback()) + .get('/app_locale_zh') + .expect({ + email: '邮箱1', + }) + .expect(200, done); + }); + describe('query.locale', function () { it('should use query locale: zh-CN', function (done) { request(app.callback()) @@ -568,6 +576,13 @@ function createApp(options) { const fname = options && options.functionName || '__'; app.use(function* () { + if (this.url === '/app_locale_zh') { + this.body = { + email: this.app[fname]('zh-cn', 'Email'), + }; + return; + } + if (this.path === '/origin') { assert(this.__getLocaleOrigin() === this.__getLocaleOrigin()); this.body = {