feat: add writeCookie option (#28)
parent
043fab2b97
commit
f89c6755c1
4
index.js
4
index.js
|
@ -15,6 +15,7 @@ const DEFAULT_OPTIONS = {
|
||||||
queryField: 'locale',
|
queryField: 'locale',
|
||||||
cookieField: 'locale',
|
cookieField: 'locale',
|
||||||
localeAlias: {},
|
localeAlias: {},
|
||||||
|
writeCookie: true,
|
||||||
cookieMaxAge: '1y',
|
cookieMaxAge: '1y',
|
||||||
dir: undefined,
|
dir: undefined,
|
||||||
dirs: [ path.join(process.cwd(), 'locales') ],
|
dirs: [ path.join(process.cwd(), 'locales') ],
|
||||||
|
@ -27,6 +28,7 @@ module.exports = function (app, options) {
|
||||||
const queryField = options.queryField;
|
const queryField = options.queryField;
|
||||||
const cookieField = options.cookieField;
|
const cookieField = options.cookieField;
|
||||||
const localeAlias = options.localeAlias;
|
const localeAlias = options.localeAlias;
|
||||||
|
const writeCookie = options.writeCookie;
|
||||||
const cookieMaxAge = ms(options.cookieMaxAge);
|
const cookieMaxAge = ms(options.cookieMaxAge);
|
||||||
const localeDir = options.dir;
|
const localeDir = options.dir;
|
||||||
const localeDirs = options.dirs;
|
const localeDirs = options.dirs;
|
||||||
|
@ -190,7 +192,7 @@ module.exports = function (app, options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// if header not send, set the locale cookie
|
// if header not send, set the locale cookie
|
||||||
if (cookieLocale !== locale && !this.headerSent) {
|
if (writeCookie && cookieLocale !== locale && !this.headerSent) {
|
||||||
// locale change, need to set cookie
|
// locale change, need to set cookie
|
||||||
this.cookies.set(cookieField, locale, {
|
this.cookies.set(cookieField, locale, {
|
||||||
// make sure brower javascript can read the cookie
|
// make sure brower javascript can read the cookie
|
||||||
|
|
|
@ -61,6 +61,10 @@ describe('koa-locales.test.js', function () {
|
||||||
'de-de': 'de',
|
'de-de': 'de',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
const appNotWriteCookie = createApp({
|
||||||
|
dirs: [__dirname + '/locales', __dirname + '/other-locales'],
|
||||||
|
writeCookie: false,
|
||||||
|
});
|
||||||
|
|
||||||
it('should use default locale: en-US', function (done) {
|
it('should use default locale: en-US', function (done) {
|
||||||
request(app.callback())
|
request(app.callback())
|
||||||
|
@ -214,18 +218,17 @@ describe('koa-locales.test.js', function () {
|
||||||
.expect(200, done);
|
.expect(200, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use localeAlias header', function (done) {
|
it('should use query locale and response without set-cookie', function (done) {
|
||||||
request(cookieFieldMapApp.callback())
|
request(appNotWriteCookie.callback())
|
||||||
.get('/')
|
.get('/?locale=zh-CN')
|
||||||
.set('Accept-Language', 'ja,de-de;q=0.8')
|
|
||||||
.expect({
|
.expect({
|
||||||
email: 'Emailde',
|
email: '邮箱1',
|
||||||
hello: 'Hallo fengmk2, wie geht es dir heute?',
|
hello: 'fengmk2,今天过得如何?',
|
||||||
message: 'Hallo fengmk2, wie geht es dir heute? Wie war dein 18.',
|
message: 'Hello fengmk2, how are you today? How was your 18.',
|
||||||
empty: '',
|
empty: '',
|
||||||
notexists_key: 'key not exists',
|
notexists_key: 'key not exists',
|
||||||
empty_string: '',
|
empty_string: '',
|
||||||
empty_value: 'emptyValue',
|
empty_value: '',
|
||||||
novalue: 'key %s ok',
|
novalue: 'key %s ok',
|
||||||
arguments3: '1 2 3',
|
arguments3: '1 2 3',
|
||||||
arguments4: '1 2 3 4',
|
arguments4: '1 2 3 4',
|
||||||
|
@ -233,10 +236,14 @@ describe('koa-locales.test.js', function () {
|
||||||
arguments6: '1 2 3 4 5. 6',
|
arguments6: '1 2 3 4 5. 6',
|
||||||
values: 'foo bar foo bar {2} {100}',
|
values: 'foo bar foo bar {2} {100}',
|
||||||
object: 'foo bar foo bar {z}',
|
object: 'foo bar foo bar {z}',
|
||||||
'gender': 'model.user.fields.gender',
|
'gender': '性别',
|
||||||
'name': 'model.user.fields.name',
|
'name': '姓名',
|
||||||
|
})
|
||||||
|
.expect(function(res) {
|
||||||
|
if(res.headers['set-cookie'] || res.headers['Set-Cookie']){
|
||||||
|
throw new Error('should not write cookie');
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.expect('Set-Cookie', /^locale=de; path=\/; expires=\w+/)
|
|
||||||
.expect(200, done);
|
.expect(200, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -401,6 +408,32 @@ describe('koa-locales.test.js', function () {
|
||||||
.expect(200, done);
|
.expect(200, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should work with "Accept-Language: de-de" by localeAlias', function (done) {
|
||||||
|
request(cookieFieldMapApp.callback())
|
||||||
|
.get('/')
|
||||||
|
.set('Accept-Language', 'ja,de-de;q=0.8')
|
||||||
|
.expect({
|
||||||
|
email: 'Emailde',
|
||||||
|
hello: 'Hallo fengmk2, wie geht es dir heute?',
|
||||||
|
message: 'Hallo fengmk2, wie geht es dir heute? Wie war dein 18.',
|
||||||
|
empty: '',
|
||||||
|
notexists_key: 'key not exists',
|
||||||
|
empty_string: '',
|
||||||
|
empty_value: 'emptyValue',
|
||||||
|
novalue: 'key %s ok',
|
||||||
|
arguments3: '1 2 3',
|
||||||
|
arguments4: '1 2 3 4',
|
||||||
|
arguments5: '1 2 3 4 5',
|
||||||
|
arguments6: '1 2 3 4 5. 6',
|
||||||
|
values: 'foo bar foo bar {2} {100}',
|
||||||
|
object: 'foo bar foo bar {z}',
|
||||||
|
'gender': 'model.user.fields.gender',
|
||||||
|
'name': 'model.user.fields.name',
|
||||||
|
})
|
||||||
|
.expect('Set-Cookie', /^locale=de; path=\/; expires=\w+/)
|
||||||
|
.expect(200, done);
|
||||||
|
});
|
||||||
|
|
||||||
it('should mock acceptsLanguages return string', function (done) {
|
it('should mock acceptsLanguages return string', function (done) {
|
||||||
mm(app.request, 'acceptsLanguages', function () {
|
mm(app.request, 'acceptsLanguages', function () {
|
||||||
return 'zh-TW';
|
return 'zh-TW';
|
||||||
|
|
Loading…
Reference in New Issue