feat: support *.properties resource files
parent
1ee07403a0
commit
a2462e6a84
|
@ -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:
|
||||
|
|
9
index.js
9
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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": "*",
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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.
|
|
@ -1,4 +1,4 @@
|
|||
module.exports = {
|
||||
'Email': '邮箱',
|
||||
Email: '邮箱',
|
||||
'Hello %s, how are you today?': '%s,今天过得如何?',
|
||||
};
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
{
|
||||
|
||||
"Email": "郵箱",
|
||||
"Hello %s, how are you today?": "%s,今天過得如何?"
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue