parent
86514e3f97
commit
3043365e09
|
@ -5,7 +5,7 @@ node_js:
|
|||
- '6'
|
||||
- '8'
|
||||
- '10'
|
||||
- '11'
|
||||
- '12'
|
||||
install:
|
||||
- npm i npminstall && npminstall
|
||||
script:
|
||||
|
|
|
@ -99,6 +99,14 @@ __('{a} {a} {b} {b} {b}', {a: 'foo', b: 'bar'})
|
|||
'foo foo bar bar bar'
|
||||
```
|
||||
|
||||
### `context.__getLocale()`
|
||||
|
||||
Get locale from query / cookie and header.
|
||||
|
||||
### `context.__getLocaleOrigin()`
|
||||
|
||||
Where does locale come from, could be `query`, `cookie`, `header` and `default`.
|
||||
|
||||
## Usage on template
|
||||
|
||||
```js
|
||||
|
|
|
@ -4,7 +4,7 @@ environment:
|
|||
- nodejs_version: '6'
|
||||
- nodejs_version: '8'
|
||||
- nodejs_version: '10'
|
||||
- nodejs_version: '11'
|
||||
- nodejs_version: '12'
|
||||
|
||||
install:
|
||||
- ps: Install-Product node $env:nodejs_version
|
||||
|
|
9
index.js
9
index.js
|
@ -166,7 +166,7 @@ module.exports = function (app, options) {
|
|||
}
|
||||
} else {
|
||||
locale = languages;
|
||||
localeOrigin = 'header (only one accepted language)';
|
||||
localeOrigin = 'header';
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,8 +208,15 @@ module.exports = function (app, options) {
|
|||
debug('Locale: %s from %s', locale, localeOrigin);
|
||||
debugSilly('Locale: %s from %s', locale, localeOrigin);
|
||||
this.__locale = locale;
|
||||
this.__localeOrigin = localeOrigin;
|
||||
return locale;
|
||||
};
|
||||
|
||||
app.context.__getLocaleOrigin = function () {
|
||||
if (this.__localeOrigin) return this.__localeOrigin;
|
||||
this.__getLocale();
|
||||
return this.__localeOrigin;
|
||||
};
|
||||
};
|
||||
|
||||
function isObject(obj) {
|
||||
|
|
|
@ -54,7 +54,7 @@
|
|||
"node": ">=4.0.0"
|
||||
},
|
||||
"ci": {
|
||||
"version": "4, 6, 8, 10, 11"
|
||||
"version": "4, 6, 8, 10, 12"
|
||||
},
|
||||
"author": "fengmk2 <fengmk2@gmail.com> (https://fengmk2.com)",
|
||||
"license": "MIT"
|
||||
|
|
|
@ -521,6 +521,34 @@ describe('koa-locales.test.js', function () {
|
|||
.expect(200, done);
|
||||
});
|
||||
});
|
||||
|
||||
describe('__getLocale and __getLocaleOrigin', function() {
|
||||
it('should __getLocale and __getLocaleOrigin from cookie', function () {
|
||||
return request(app.callback())
|
||||
.get('/methods')
|
||||
.set('cookie', 'locale=de')
|
||||
.expect(200, { locale: 'de', localeOrigin: 'cookie' });
|
||||
});
|
||||
|
||||
it('should __getLocale and __getLocaleOrigin from query', function () {
|
||||
return request(app.callback())
|
||||
.get('/methods?locale=de')
|
||||
.expect(200, { locale: 'de', localeOrigin: 'query' });
|
||||
});
|
||||
|
||||
it('should __getLocale and __getLocaleOrigin from header', function () {
|
||||
return request(app.callback())
|
||||
.get('/methods')
|
||||
.set('Accept-Language', 'zh-cn')
|
||||
.expect(200, { locale: 'zh-cn', localeOrigin: 'header' });
|
||||
});
|
||||
|
||||
it('should __getLocale and __getLocaleOrigin from default', function () {
|
||||
return request(app.callback())
|
||||
.get('/methods')
|
||||
.expect(200, { locale: 'en-us', localeOrigin: 'default' });
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -530,6 +558,15 @@ function createApp(options) {
|
|||
const fname = options && options.functionName || '__';
|
||||
|
||||
app.use(function* () {
|
||||
if (this.path === '/methods') {
|
||||
assert(this.__getLocaleOrigin() === this.__getLocaleOrigin());
|
||||
this.body = {
|
||||
locale: this.__getLocale(),
|
||||
localeOrigin: this.__getLocaleOrigin(),
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.url === '/headerSent') {
|
||||
this.body = 'foo';
|
||||
const that = this;
|
||||
|
|
Loading…
Reference in New Issue