Skip to content

Commit 3043365

Browse files
dead-horsefengmk2
authored andcommitted
feat: add __getLocaleOrigin (#35)
* chore: test on node 12
1 parent 86514e3 commit 3043365

File tree

6 files changed

+56
-4
lines changed

6 files changed

+56
-4
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ node_js:
55
- '6'
66
- '8'
77
- '10'
8-
- '11'
8+
- '12'
99
install:
1010
- npm i npminstall && npminstall
1111
script:

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ __('{a} {a} {b} {b} {b}', {a: 'foo', b: 'bar'})
9999
'foo foo bar bar bar'
100100
```
101101

102+
### `context.__getLocale()`
103+
104+
Get locale from query / cookie and header.
105+
106+
### `context.__getLocaleOrigin()`
107+
108+
Where does locale come from, could be `query`, `cookie`, `header` and `default`.
109+
102110
## Usage on template
103111

104112
```js

appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ environment:
44
- nodejs_version: '6'
55
- nodejs_version: '8'
66
- nodejs_version: '10'
7-
- nodejs_version: '11'
7+
- nodejs_version: '12'
88

99
install:
1010
- ps: Install-Product node $env:nodejs_version

index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ module.exports = function (app, options) {
166166
}
167167
} else {
168168
locale = languages;
169-
localeOrigin = 'header (only one accepted language)';
169+
localeOrigin = 'header';
170170
}
171171
}
172172

@@ -208,8 +208,15 @@ module.exports = function (app, options) {
208208
debug('Locale: %s from %s', locale, localeOrigin);
209209
debugSilly('Locale: %s from %s', locale, localeOrigin);
210210
this.__locale = locale;
211+
this.__localeOrigin = localeOrigin;
211212
return locale;
212213
};
214+
215+
app.context.__getLocaleOrigin = function () {
216+
if (this.__localeOrigin) return this.__localeOrigin;
217+
this.__getLocale();
218+
return this.__localeOrigin;
219+
};
213220
};
214221

215222
function isObject(obj) {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"node": ">=4.0.0"
5555
},
5656
"ci": {
57-
"version": "4, 6, 8, 10, 11"
57+
"version": "4, 6, 8, 10, 12"
5858
},
5959
"author": "fengmk2 <[email protected]> (https://fengmk2.com)",
6060
"license": "MIT"

test/index.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,34 @@ describe('koa-locales.test.js', function () {
521521
.expect(200, done);
522522
});
523523
});
524+
525+
describe('__getLocale and __getLocaleOrigin', function() {
526+
it('should __getLocale and __getLocaleOrigin from cookie', function () {
527+
return request(app.callback())
528+
.get('/methods')
529+
.set('cookie', 'locale=de')
530+
.expect(200, { locale: 'de', localeOrigin: 'cookie' });
531+
});
532+
533+
it('should __getLocale and __getLocaleOrigin from query', function () {
534+
return request(app.callback())
535+
.get('/methods?locale=de')
536+
.expect(200, { locale: 'de', localeOrigin: 'query' });
537+
});
538+
539+
it('should __getLocale and __getLocaleOrigin from header', function () {
540+
return request(app.callback())
541+
.get('/methods')
542+
.set('Accept-Language', 'zh-cn')
543+
.expect(200, { locale: 'zh-cn', localeOrigin: 'header' });
544+
});
545+
546+
it('should __getLocale and __getLocaleOrigin from default', function () {
547+
return request(app.callback())
548+
.get('/methods')
549+
.expect(200, { locale: 'en-us', localeOrigin: 'default' });
550+
});
551+
});
524552
});
525553
});
526554

@@ -530,6 +558,15 @@ function createApp(options) {
530558
const fname = options && options.functionName || '__';
531559

532560
app.use(function* () {
561+
if (this.path === '/methods') {
562+
assert(this.__getLocaleOrigin() === this.__getLocaleOrigin());
563+
this.body = {
564+
locale: this.__getLocale(),
565+
localeOrigin: this.__getLocaleOrigin(),
566+
};
567+
return;
568+
}
569+
533570
if (this.url === '/headerSent') {
534571
this.body = 'foo';
535572
const that = this;

0 commit comments

Comments
 (0)