From a2462e6a8490a5f8d00f8c692384953149a608d9 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sun, 17 May 2015 21:54:12 +0800 Subject: [PATCH] feat: support *.properties resource files --- README.md | 7 ++++--- index.js | 9 +++++++-- package.json | 3 ++- test/index.test.js | 25 +++++++++++++++++++++++-- test/locales/de.properties | 3 +++ test/locales/zh-CN.js | 2 +- test/locales/zh_TW.json | 3 ++- 7 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 test/locales/de.properties diff --git a/README.md b/README.md index 3198a0d..da66f90 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,9 @@ koa-locales koa locales, i18n solution for koa: 1. All locales resources location on `options.dir`. -2. One api: `__(key[, value, ...])` -3. Auto detect request locale from `query`, `cookie` and `header: Accept-Language` +2. resources file supports: `*.js`, `*.json` and `*.properties` +3. One api: `__(key[, value, ...])` +4. Auto detect request locale from `query`, `cookie` and `header: Accept-Language` ## Installation @@ -101,7 +102,7 @@ __('{0} {0} {1} {1} {1}', ['foo', 'bar']) ## Usage on template ```js -this.locales.__ = this.__.bind(this); +this.state.__ = this.__.bind(this); ``` [nunjucks] example: diff --git a/index.js b/index.js index 8a2c7a5..e6a30fa 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,7 @@ */ var debug = require('debug')('koa-locales'); +var ini = require('ini'); var util = require('util'); var fs = require('fs'); var path = require('path'); @@ -34,13 +35,17 @@ module.exports = function (app, options) { var names = fs.readdirSync(localeDir); for (var i = 0; i < names.length; i++) { var name = names[i]; - if (!/\.(js|json)$/.test(name)) { + if (!/\.(js|json|properties)$/.test(name)) { continue; } var filepath = path.join(localeDir, name); // support en_US.js => en-US.js var locale = formatLocale(name.split('.')[0]); - resources[locale] = require(filepath); + if (/\.properties$/.test(name)) { + resources[locale] = ini.parse(fs.readFileSync(filepath, 'utf8')); + } else { + resources[locale] = require(filepath); + } } } diff --git a/package.json b/package.json index d8bdea5..04c80c3 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ }, "dependencies": { "debug": "~2.2.0", - "humanize-ms": "~1.0.1" + "humanize-ms": "~1.0.1", + "ini": "~1.3.3" }, "devDependencies": { "autod": "*", diff --git a/test/index.test.js b/test/index.test.js index a70d122..fd24bdf 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -98,6 +98,27 @@ describe('koa-locales.test.js', function () { .expect(200, done); }); + it('should use query locale: de on *.properties format', function (done) { + request(app.callback()) + .get('/?locale=de') + .expect({ + email: 'Emailde', + hello: 'Hallo fengmk2, wie geht es dir heute?', + message: 'Hallo fengmk2, wie geht es dir heute? Wie war dein 18.', + empty: '', + notexists_key: 'key not exists', + empty_string: '', + novalue: 'key %s ok', + arguments3: '1 2 3', + arguments4: '1 2 3 4', + arguments5: '1 2 3 4 5', + arguments6: '1 2 3 4 5. 6', + values: 'foo bar foo bar {2} {100}', + }) + .expect('Set-Cookie', /^locale=de; path=\/; expires=\w+/) + .expect(200, done); + }); + it('should use query locale and change cookie locale', function (done) { request(app.callback()) .get('/?locale=zh-CN') @@ -284,8 +305,8 @@ describe('koa-locales.test.js', function () { request(app.callback()) .get('/?locale=') .expect({ - email: 'Email', - hello: 'Hello fengmk2, how are you today?', + email: '郵箱', + hello: 'fengmk2,今天過得如何?', message: 'Hello fengmk2, how are you today? How was your 18.', empty: '', notexists_key: 'key not exists', diff --git a/test/locales/de.properties b/test/locales/de.properties new file mode 100644 index 0000000..2f73e41 --- /dev/null +++ b/test/locales/de.properties @@ -0,0 +1,3 @@ +Email = Emailde +"Hello %s, how are you today?" = "Hallo %s, wie geht es dir heute?" +Hello %s, how are you today? How was your %s. = Hallo %s, wie geht es dir heute? Wie war dein %s. diff --git a/test/locales/zh-CN.js b/test/locales/zh-CN.js index e3d836e..4995a0c 100644 --- a/test/locales/zh-CN.js +++ b/test/locales/zh-CN.js @@ -1,4 +1,4 @@ module.exports = { - 'Email': '邮箱', + Email: '邮箱', 'Hello %s, how are you today?': '%s,今天过得如何?', }; diff --git a/test/locales/zh_TW.json b/test/locales/zh_TW.json index 1797133..6bb200b 100644 --- a/test/locales/zh_TW.json +++ b/test/locales/zh_TW.json @@ -1,3 +1,4 @@ { - + "Email": "郵箱", + "Hello %s, how are you today?": "%s,今天過得如何?" }