diff --git a/README.md b/README.md index 665e306..456955a 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,10 @@ __('{a} {a} {b} {b} {b}', {a: 'foo', b: 'bar'}) Get locale from query / cookie and header. +### `context.setLocale()` + +Set locale and cookie. + ### `context.__getLocaleOrigin()` Where does locale come from, could be `query`, `cookie`, `header` and `default`. diff --git a/index.js b/index.js index e07bd33..2614787 100644 --- a/index.js +++ b/index.js @@ -194,16 +194,7 @@ module.exports = function (app, options) { // if header not send, set the locale cookie if (writeCookie && cookieLocale !== locale && !this.headerSent) { - // locale change, need to set cookie - const cookieOptions = { - // make sure brower javascript can read the cookie - httpOnly: false, - maxAge: cookieMaxAge, - signed: false, - domain: cookieDomain, - }; - this.cookies.set(cookieField, locale, cookieOptions); - debugSilly('Saved cookie with locale %s', locale); + updateCookie(this, locale); } debug('Locale: %s from %s', locale, localeOrigin); debugSilly('Locale: %s from %s', locale, localeOrigin); @@ -217,6 +208,25 @@ module.exports = function (app, options) { this.__getLocale(); return this.__localeOrigin; }; + + app.context.__setLocale = function (locale) { + this.__locale = locale; + this.__localeOrigin = 'set'; + updateCookie(this, locale); + }; + + function updateCookie(ctx, locale) { + const cookieOptions = { + // make sure brower javascript can read the cookie + httpOnly: false, + maxAge: cookieMaxAge, + signed: false, + domain: cookieDomain, + overwrite: true, + }; + ctx.cookies.set(cookieField, locale, cookieOptions); + debugSilly('Saved cookie with locale %s', locale); + } }; function isObject(obj) { diff --git a/test/index.test.js b/test/index.test.js index 34c65a1..b2a8a4f 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -525,30 +525,40 @@ describe('koa-locales.test.js', function () { describe('__getLocale and __getLocaleOrigin', function() { it('should __getLocale and __getLocaleOrigin from cookie', function () { return request(app.callback()) - .get('/methods') + .get('/origin') .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') + .get('/origin?locale=de') .expect(200, { locale: 'de', localeOrigin: 'query' }); }); it('should __getLocale and __getLocaleOrigin from header', function () { return request(app.callback()) - .get('/methods') + .get('/origin') .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') + .get('/origin') .expect(200, { locale: 'en-us', localeOrigin: 'default' }); }); }); + + describe('__setLocale', function() { + it('should set locale and cookie', function () { + return request(app.callback()) + .get('/set') + .set('cookie', 'locale=de') + .expect(200, { locale: 'zh-hk', localeOrigin: 'set' }) + .expect('Set-Cookie', /^locale=zh\-hk; path=\/; expires=[^;]+ GMT$/); + }); + }); }); }); @@ -558,7 +568,7 @@ function createApp(options) { const fname = options && options.functionName || '__'; app.use(function* () { - if (this.path === '/methods') { + if (this.path === '/origin') { assert(this.__getLocaleOrigin() === this.__getLocaleOrigin()); this.body = { locale: this.__getLocale(), @@ -567,6 +577,17 @@ function createApp(options) { return; } + if (this.path === '/set') { + this.__getLocale(); + this.__setLocale('zh-tw'); + this.__setLocale('zh-hk'); + this.body = { + locale: this.__getLocale(), + localeOrigin: this.__getLocaleOrigin(), + }; + return; + } + if (this.url === '/headerSent') { this.body = 'foo'; const that = this;