parent
b3c8ab19e5
commit
08037ee0ae
|
@ -3,7 +3,9 @@ language: node_js
|
|||
node_js:
|
||||
- '4'
|
||||
- '6'
|
||||
- '7'
|
||||
- '8'
|
||||
- '10'
|
||||
- '11'
|
||||
install:
|
||||
- npm i npminstall && npminstall
|
||||
script:
|
||||
|
|
|
@ -46,7 +46,8 @@ Patch locales functions to koa app.
|
|||
- {String} defaultLocale: default locale. Optional, default is `en-US`.
|
||||
- {String} queryField: locale field name on query. Optional, default is `locale`.
|
||||
- {String} cookieField: locale field name on cookie. Optional, default is `locale`.
|
||||
- {Object} localeAlias: locale value map. Optional, default is {}.
|
||||
- {String} cookieDomain: domain on cookie. Optional, default is `''`.
|
||||
- {Object} localeAlias: locale value map. Optional, default is `{}`.
|
||||
- {String|Number} cookieMaxAge: set locale cookie value max age. Optional, default is `1y`, expired after one year.
|
||||
|
||||
```js
|
||||
|
|
|
@ -2,7 +2,9 @@ environment:
|
|||
matrix:
|
||||
- nodejs_version: '4'
|
||||
- nodejs_version: '6'
|
||||
- nodejs_version: '7'
|
||||
- nodejs_version: '8'
|
||||
- nodejs_version: '10'
|
||||
- nodejs_version: '11'
|
||||
|
||||
install:
|
||||
- ps: Install-Product node $env:nodejs_version
|
||||
|
@ -11,6 +13,6 @@ install:
|
|||
test_script:
|
||||
- node --version
|
||||
- npm --version
|
||||
- npm run ci
|
||||
- npm run test
|
||||
|
||||
build: off
|
||||
|
|
7
index.js
7
index.js
|
@ -27,6 +27,7 @@ module.exports = function (app, options) {
|
|||
const defaultLocale = formatLocale(options.defaultLocale);
|
||||
const queryField = options.queryField;
|
||||
const cookieField = options.cookieField;
|
||||
const cookieDomain = options.cookieDomain;
|
||||
const localeAlias = options.localeAlias;
|
||||
const writeCookie = options.writeCookie;
|
||||
const cookieMaxAge = ms(options.cookieMaxAge);
|
||||
|
@ -194,12 +195,14 @@ 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
|
||||
this.cookies.set(cookieField, locale, {
|
||||
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);
|
||||
}
|
||||
debug('Locale: %s from %s', locale, localeOrigin);
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
"istanbul": "*",
|
||||
"koa": "^1.2.4",
|
||||
"mm": "^2.0.0",
|
||||
"mocha": "*",
|
||||
"mocha": "4",
|
||||
"pedding": "^1.1.0",
|
||||
"supertest": "^2.0.1"
|
||||
},
|
||||
|
@ -41,8 +41,7 @@
|
|||
"web": "https://github.com/koajs/locales"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/koajs/locales/issues",
|
||||
"email": "m@fengmk2.com"
|
||||
"url": "https://github.com/koajs/locales/issues"
|
||||
},
|
||||
"keywords": [
|
||||
"koa-locales",
|
||||
|
@ -55,8 +54,8 @@
|
|||
"node": ">=4.0.0"
|
||||
},
|
||||
"ci": {
|
||||
"version": "4, 6, 7"
|
||||
"version": "4, 6, 8, 10, 11"
|
||||
},
|
||||
"author": "fengmk2 <m@fengmk2.com> (https://fengmk2.com)",
|
||||
"author": "fengmk2 <fengmk2@gmail.com> (https://fengmk2.com)",
|
||||
"license": "MIT"
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ describe('koa-locales.test.js', function () {
|
|||
'gender': 'model.user.fields.gender',
|
||||
'name': 'model.user.fields.name',
|
||||
})
|
||||
.expect('Set-Cookie', /^locale=en\-us; path=\/; expires=\w+/)
|
||||
.expect('Set-Cookie', /^locale=en\-us; path=\/; expires=[^;]+ GMT$/)
|
||||
.expect(200, done);
|
||||
});
|
||||
|
||||
|
@ -50,6 +50,37 @@ describe('koa-locales.test.js', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('options.cookieDomain', function () {
|
||||
const app = createApp({
|
||||
cookieDomain: '.foo.com',
|
||||
});
|
||||
|
||||
it('should use default locale: en-US', function (done) {
|
||||
request(app.callback())
|
||||
.get('/')
|
||||
.expect({
|
||||
email: 'Email',
|
||||
hello: 'Hello fengmk2, how are you today?',
|
||||
message: 'Hello fengmk2, how are you today? How was your 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=en\-us; path=\/; expires=[^;]+; domain=.foo.com$/)
|
||||
.expect(200, done);
|
||||
});
|
||||
});
|
||||
|
||||
describe('custom options', function () {
|
||||
const app = createApp({
|
||||
dirs: [__dirname + '/locales', __dirname + '/other-locales'],
|
||||
|
|
Loading…
Reference in New Issue