koa-i18next-detector
Advanced tools
Comparing version 0.3.3 to 0.4.0
'use strict';Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _querystring = require('./lookups/querystring');var _querystring2 = _interopRequireDefault(_querystring); | ||
var _path = require('./lookups/path');var _path2 = _interopRequireDefault(_path);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}const assert = require('assert'); | ||
// import cookieLookup from './languageLookups/cookie'; | ||
// import headerLookup from './languageLookups/header'; | ||
// import sessionLookup from './languageLookups/session'; | ||
var _path = require('./lookups/path');var _path2 = _interopRequireDefault(_path); | ||
var _cookie = require('./lookups/cookie');var _cookie2 = _interopRequireDefault(_cookie);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}const assert = require('assert'); | ||
// import headerLookup from './lookups/header'; | ||
// import sessionLookup from './lookups/session'; | ||
function getDefaults() { | ||
function getDefaultsOpt() { | ||
return { | ||
detectors: [{ | ||
name: 'querystring', | ||
lookupQuerystring: 'lng' }, | ||
order: ['querystring', 'path', 'cookie', 'header', 'session'], | ||
{ | ||
name: 'path', | ||
lookupParam: 'lng', // for route like: 'path1/:lng/result' | ||
lookupFromPathIndex: 0 }, | ||
lookupQuerystring: 'lng', | ||
{ | ||
name: 'cookie', | ||
lookupCookie: 'i18next' }, | ||
lookupParam: 'lng', // for route like: 'path1/:lng/result' | ||
lookupFromPathIndex: 0, | ||
{ | ||
name: 'header' }, | ||
lookupCookie: 'i18next', | ||
cookieExpirationDate: new Date(), | ||
cookieDomain: 'myDomain', | ||
{ | ||
name: 'session', | ||
lookupSession: 'lng' }], | ||
lookupSession: 'lng', | ||
// cache user language | ||
caches: false }; | ||
} | ||
function defaults(obj) { | ||
Array.from(arguments).slice(1).forEach(source => { | ||
if (source) { | ||
for (var prop in source) { | ||
if (obj[prop] === undefined) obj[prop] = source[prop]; | ||
} | ||
} | ||
}); | ||
return obj; | ||
} | ||
@@ -50,3 +49,3 @@ | ||
this.services = services; | ||
this.options = Object.assign({}, getDefaults(), this.options || {}, options); | ||
this.options = defaults(options, this.options || {}, getDefaultsOpt()); | ||
this.i18nextOptions = i18nextOptions; | ||
@@ -56,3 +55,3 @@ try { | ||
this.addDetector(_path2.default); | ||
this.addDetector(cookieLookup); | ||
this.addDetector(_cookie2.default); | ||
this.addDetector(headerLookup); | ||
@@ -76,10 +75,12 @@ this.addDetector(sessionLookup); | ||
let found; | ||
this.options.detectors.forEach(detectorOpt => { | ||
if (found || !this.detectors[detectorOpt.name]) { | ||
this.options.order.forEach(detectorName => { | ||
if (found || !this.detectors[detectorName] || !this.detectors[detectorName].lookup) { | ||
return; | ||
} | ||
let detections = this.detectors[detectorOpt.name].lookup(ctx, detectorOpt); | ||
let detections = this.detectors[detectorName].lookup(ctx, this.options); | ||
if (!detections) return; | ||
if (typeof detections === 'string') detections = [detections]; | ||
if (typeof detections === 'string') { | ||
detections = [detections]; | ||
} | ||
@@ -93,3 +94,3 @@ detections.forEach(lng => { | ||
found = cleanedLng; | ||
ctx.i18nextLookupName = detectorOpt.name; | ||
ctx.i18nextLookupName = detectorName; | ||
}; | ||
@@ -113,4 +114,5 @@ }); | ||
caches.forEach(cacheName => { | ||
if (this.detectors[cacheName] && this.detectors[cacheName].cacheUserLanguage) { | ||
this.detectors[cacheName].cacheUserLanguage(ctx, lng, this.options); | ||
let detector = this.detectors[cacheName]; | ||
if (detector && detector.cacheUserLanguage) { | ||
detector.cacheUserLanguage(ctx, lng, this.options); | ||
} | ||
@@ -117,0 +119,0 @@ }); |
{ | ||
"name": "koa-i18next-detector", | ||
"version": "0.3.3", | ||
"version": "0.4.0", | ||
"description": "A i18next language detecting plugin for Koa 2.0 framework.", | ||
@@ -50,3 +50,6 @@ "main": "dist/index.js", | ||
"watch": "^0.16.0" | ||
}, | ||
"dependencies": { | ||
"cookies": "^0.7.0" | ||
} | ||
} |
const assert = require('assert'); | ||
import querystringLookup from './lookups/querystring'; | ||
import pathLookup from './lookups/path'; | ||
// import cookieLookup from './languageLookups/cookie'; | ||
// import headerLookup from './languageLookups/header'; | ||
// import sessionLookup from './languageLookups/session'; | ||
import cookieLookup from './lookups/cookie'; | ||
// import headerLookup from './lookups/header'; | ||
// import sessionLookup from './lookups/session'; | ||
function getDefaults() { | ||
function getDefaultsOpt() { | ||
return { | ||
detectors: [{ | ||
name: 'querystring', | ||
lookupQuerystring: 'lng', | ||
}, | ||
{ | ||
name: 'path', | ||
lookupParam: 'lng', // for route like: 'path1/:lng/result' | ||
lookupFromPathIndex: 0 | ||
}, | ||
{ | ||
name: 'cookie', | ||
lookupCookie: 'i18next', | ||
}, | ||
{ | ||
name: 'header', | ||
}, | ||
{ | ||
name: 'session', | ||
lookupSession: 'lng' | ||
} | ||
], | ||
order: ['querystring', 'path', 'cookie', 'header', 'session'], | ||
lookupQuerystring: 'lng', | ||
lookupParam: 'lng', // for route like: 'path1/:lng/result' | ||
lookupFromPathIndex: 0, | ||
lookupCookie: 'i18next', | ||
cookieExpirationDate: new Date(), | ||
cookieDomain: 'myDomain', | ||
lookupSession: 'lng', | ||
// cache user language | ||
caches: false, | ||
// ['cookie'] | ||
//cookieExpirationDate: new Date(), | ||
//cookieDomain: 'myDomain' | ||
caches: false, //['cookie'] | ||
}; | ||
} | ||
function defaults(obj) { | ||
Array.from(arguments).slice(1).forEach(source => { | ||
if (source) { | ||
for (var prop in source) { | ||
if (obj[prop] === undefined) obj[prop] = source[prop]; | ||
} | ||
} | ||
}); | ||
return obj; | ||
} | ||
class LanguageDetector { | ||
@@ -50,3 +49,3 @@ constructor(services, options = {}, i18nextOptions = {}) { | ||
this.services = services; | ||
this.options = Object.assign({}, getDefaults(), this.options || {}, options); | ||
this.options = defaults(options, this.options || {}, getDefaultsOpt()); | ||
this.i18nextOptions = i18nextOptions; | ||
@@ -75,10 +74,12 @@ try { | ||
let found; | ||
this.options.detectors.forEach(detectorOpt => { | ||
if (found || !this.detectors[detectorOpt.name]) { | ||
this.options.order.forEach(detectorName => { | ||
if (found || !this.detectors[detectorName] || !this.detectors[detectorName].lookup) { | ||
return; | ||
} | ||
let detections = this.detectors[detectorOpt.name].lookup(ctx, detectorOpt); | ||
let detections = this.detectors[detectorName].lookup(ctx, this.options); | ||
if (!detections) return; | ||
if (typeof detections === 'string') detections = [detections]; | ||
if (typeof detections === 'string') { | ||
detections = [detections]; | ||
} | ||
@@ -92,3 +93,3 @@ detections.forEach(lng => { | ||
found = cleanedLng; | ||
ctx.i18nextLookupName = detectorOpt.name; | ||
ctx.i18nextLookupName = detectorName; | ||
}; | ||
@@ -112,4 +113,5 @@ }); | ||
caches.forEach(cacheName => { | ||
if (this.detectors[cacheName] && this.detectors[cacheName].cacheUserLanguage) { | ||
this.detectors[cacheName].cacheUserLanguage(ctx, lng, this.options); | ||
let detector = this.detectors[cacheName]; | ||
if (detector && detector.cacheUserLanguage) { | ||
detector.cacheUserLanguage(ctx, lng, this.options); | ||
} | ||
@@ -116,0 +118,0 @@ }); |
17118
12
318
1
+ Addedcookies@^0.7.0
+ Addedcookies@0.7.3(transitive)
+ Addeddepd@1.1.2(transitive)
+ Addedkeygrip@1.0.3(transitive)