feat: support *.properties resource files

master
fengmk2 2015-05-17 21:54:12 +08:00
parent 1ee07403a0
commit a2462e6a84
7 changed files with 42 additions and 10 deletions

View File

@ -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:

View File

@ -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);
}
}
}

View File

@ -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": "*",

View File

@ -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',

View File

@ -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.

View File

@ -1,4 +1,4 @@
module.exports = {
'Email': '邮箱',
Email: '邮箱',
'Hello %s, how are you today?': '%s今天过得如何',
};

View File

@ -1,3 +1,4 @@
{
"Email": "郵箱",
"Hello %s, how are you today?": "%s今天過得如何"
}