feat: ctx.__setLocale (#36)
parent
5b00e90a11
commit
0767037b3c
|
@ -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`.
|
||||
|
|
30
index.js
30
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) {
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue