From 3043365e09cfd76bf6ef54a4cd7347a97d08fdc5 Mon Sep 17 00:00:00 2001 From: Yiyu He Date: Mon, 29 Apr 2019 16:15:14 +0800 Subject: [PATCH] feat: add __getLocaleOrigin (#35) * chore: test on node 12 --- .travis.yml | 2 +- README.md | 8 ++++++++ appveyor.yml | 2 +- index.js | 9 ++++++++- package.json | 2 +- test/index.test.js | 37 +++++++++++++++++++++++++++++++++++++ 6 files changed, 56 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index faeb0c0..154ab7a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ node_js: - '6' - '8' - '10' - - '11' + - '12' install: - npm i npminstall && npminstall script: diff --git a/README.md b/README.md index dddaf53..665e306 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/appveyor.yml b/appveyor.yml index a231e55..99ed89a 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -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 diff --git a/index.js b/index.js index 006c84a..e07bd33 100644 --- a/index.js +++ b/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) { diff --git a/package.json b/package.json index aeed573..9db4388 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "node": ">=4.0.0" }, "ci": { - "version": "4, 6, 8, 10, 11" + "version": "4, 6, 8, 10, 12" }, "author": "fengmk2 (https://fengmk2.com)", "license": "MIT" diff --git a/test/index.test.js b/test/index.test.js index d4c145a..34c65a1 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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;