update deps

This commit is contained in:
s2
2019-12-20 20:02:44 +01:00
parent 14c1b72301
commit b7fa481dcb
833 changed files with 68364 additions and 18390 deletions

View File

@@ -0,0 +1,8 @@
{
"bracketSpacing": true,
"jsxBracketSameLine": false,
"printWidth": 100,
"semi": true,
"singleQuote": true,
"trailingComma": "all"
}

View File

@@ -1,3 +1,19 @@
### v3.1.1
- add default checkWhitelist: true
### v3.1.0
- Added option to prevent checking whitelist for detected languages `checkWhitelist: true` [190](https://github.com/i18next/i18next-browser-languageDetector/pull/190)
### v3.0.3
- Remove clutter from npm package [181](https://github.com/i18next/i18next-browser-languageDetector/pull/181)
### v3.0.2
- typescript: Fix types for `use()` module [180](https://github.com/i18next/i18next-browser-languageDetector/pull/180)
### v3.0.1
- typescript: fix types [165](https://github.com/i18next/i18next-browser-languageDetector/pull/165)

View File

@@ -6,13 +6,13 @@
This is a i18next language detection plugin use to detect user language in the browser with support for:
- cookie
- localStorage
- navigator
- cookie (set cookie i18next=LANGUAGE)
- localStorage (set key i18nextLng=LANGUAGE)
- navigator (set browser language)
- querystring (append `?lng=LANGUAGE` to URL)
- htmlTag
- path
- subdomain
- htmlTag (add html language tag <html lang="LANGUAGE" ...)
- path (http://my.site.com/LANGUAGE/...)
- subdomain (http://LANGUAGE.site.com/...)
# Getting started
@@ -32,10 +32,10 @@ Wiring up:
```js
import i18next from 'i18next';
import LngDetector from 'i18next-browser-languagedetector';
import LanguageDetector from 'i18next-browser-languagedetector';
i18next
.use(LngDetector)
.use(LanguageDetector)
.init(i18nextOptions);
```
@@ -64,7 +64,10 @@ As with all modules you can either pass the constructor function (class) to the
cookieDomain: 'myDomain',
// optional htmlTag with lang attribute, the default is:
htmlTag: document.documentElement
htmlTag: document.documentElement,
// only detect languages that are in the whitelist
checkWhitelist: true
}
```
@@ -74,10 +77,10 @@ Options can be passed in:
```js
import i18next from 'i18next';
import LngDetector from 'i18next-browser-languagedetector';
import LanguageDetector from 'i18next-browser-languagedetector';
i18next
.use(LngDetector)
.use(LanguageDetector)
.init({
detection: options
});
@@ -86,15 +89,15 @@ i18next
on construction:
```js
import LngDetector from 'i18next-browser-languagedetector';
const lngDetector = new LngDetector(null, options);
import LanguageDetector from 'i18next-browser-languagedetector';
const languageDetector = new LanguageDetector(null, options);
```
via calling init:
```js
import LngDetector from 'i18next-browser-languagedetector';
const lngDetector = new LngDetector();
import LanguageDetector from 'i18next-browser-languagedetector';
const languageDetector = new LanguageDetector();
lngDetector.init(options);
```
@@ -124,12 +127,12 @@ export default {
### adding it
```js
import LngDetector from 'i18next-browser-languagedetector';
const lngDetector = new LngDetector();
lngDetector.addDetector(myDetector);
import LanguageDetector from 'i18next-browser-languagedetector';
const languageDetector = new LanguageDetector();
languageDetector.addDetector(myDetector);
i18next
.use(lngDetector)
.use(languageDetector)
.init({
detection: options
});

View File

@@ -0,0 +1,53 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var cookie = {
create: function create(name, value, minutes, domain) {
var expires = void 0;
if (minutes) {
var date = new Date();
date.setTime(date.getTime() + minutes * 60 * 1000);
expires = '; expires=' + date.toGMTString();
} else expires = '';
domain = domain ? 'domain=' + domain + ';' : '';
document.cookie = name + '=' + value + expires + ';' + domain + 'path=/';
},
read: function read(name) {
var nameEQ = name + '=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}
return null;
},
remove: function remove(name) {
this.create(name, '', -1);
}
};
exports.default = {
name: 'cookie',
lookup: function lookup(options) {
var found = void 0;
if (options.lookupCookie && typeof document !== 'undefined') {
var c = cookie.read(options.lookupCookie);
if (c) found = c;
}
return found;
},
cacheUserLanguage: function cacheUserLanguage(lng, options) {
if (options.lookupCookie && typeof document !== 'undefined') {
cookie.create(options.lookupCookie, lng, options.cookieMinutes, options.cookieDomain);
}
}
};

View File

@@ -0,0 +1,19 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = {
name: 'htmlTag',
lookup: function lookup(options) {
var found = void 0;
var htmlTag = options.htmlTag || (typeof document !== 'undefined' ? document.documentElement : null);
if (htmlTag && typeof htmlTag.getAttribute === 'function') {
found = htmlTag.getAttribute('lang');
}
return found;
}
};

View File

@@ -0,0 +1,34 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var hasLocalStorageSupport = void 0;
try {
hasLocalStorageSupport = window !== 'undefined' && window.localStorage !== null;
var testKey = 'i18next.translate.boo';
window.localStorage.setItem(testKey, 'foo');
window.localStorage.removeItem(testKey);
} catch (e) {
hasLocalStorageSupport = false;
}
exports.default = {
name: 'localStorage',
lookup: function lookup(options) {
var found = void 0;
if (options.lookupLocalStorage && hasLocalStorageSupport) {
var lng = window.localStorage.getItem(options.lookupLocalStorage);
if (lng) found = lng;
}
return found;
},
cacheUserLanguage: function cacheUserLanguage(lng, options) {
if (options.lookupLocalStorage && hasLocalStorageSupport) {
window.localStorage.setItem(options.lookupLocalStorage, lng);
}
}
};

View File

@@ -0,0 +1,29 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = {
name: 'navigator',
lookup: function lookup(options) {
var found = [];
if (typeof navigator !== 'undefined') {
if (navigator.languages) {
// chrome only; not an array, so can't use .push.apply instead of iterating
for (var i = 0; i < navigator.languages.length; i++) {
found.push(navigator.languages[i]);
}
}
if (navigator.userLanguage) {
found.push(navigator.userLanguage);
}
if (navigator.language) {
found.push(navigator.language);
}
}
return found.length > 0 ? found : undefined;
}
};

View File

@@ -0,0 +1,26 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = {
name: 'path',
lookup: function lookup(options) {
var found = void 0;
if (typeof window !== 'undefined') {
var language = window.location.pathname.match(/\/([a-zA-Z-]*)/g);
if (language instanceof Array) {
if (typeof options.lookupFromPathIndex === 'number') {
if (typeof language[options.lookupFromPathIndex] !== 'string') {
return undefined;
}
found = language[options.lookupFromPathIndex].replace('/', '');
} else {
found = language[0].replace('/', '');
}
}
}
return found;
}
};

View File

@@ -0,0 +1,28 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = {
name: 'querystring',
lookup: function lookup(options) {
var found = void 0;
if (typeof window !== 'undefined') {
var query = window.location.search.substring(1);
var params = query.split('&');
for (var i = 0; i < params.length; i++) {
var pos = params[i].indexOf('=');
if (pos > 0) {
var key = params[i].substring(0, pos);
if (key === options.lookupQuerystring) {
found = params[i].substring(pos + 1);
}
}
}
}
return found;
}
};

View File

@@ -0,0 +1,23 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = {
name: 'subdomain',
lookup: function lookup(options) {
var found = void 0;
if (typeof window !== 'undefined') {
var language = window.location.href.match(/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/gi);
if (language instanceof Array) {
if (typeof options.lookupFromSubdomainIndex === 'number') {
found = language[options.lookupFromSubdomainIndex].replace('http://', '').replace('https://', '').replace('.', '');
} else {
found = language[0].replace('http://', '').replace('https://', '').replace('.', '');
}
}
}
return found;
}
};

View File

@@ -0,0 +1,158 @@
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _utils = require('./utils.js');
var utils = _interopRequireWildcard(_utils);
var _cookie = require('./browserLookups/cookie.js');
var _cookie2 = _interopRequireDefault(_cookie);
var _querystring = require('./browserLookups/querystring.js');
var _querystring2 = _interopRequireDefault(_querystring);
var _localStorage = require('./browserLookups/localStorage.js');
var _localStorage2 = _interopRequireDefault(_localStorage);
var _navigator = require('./browserLookups/navigator.js');
var _navigator2 = _interopRequireDefault(_navigator);
var _htmlTag = require('./browserLookups/htmlTag.js');
var _htmlTag2 = _interopRequireDefault(_htmlTag);
var _path = require('./browserLookups/path.js');
var _path2 = _interopRequireDefault(_path);
var _subdomain = require('./browserLookups/subdomain.js');
var _subdomain2 = _interopRequireDefault(_subdomain);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function getDefaults() {
return {
order: ['querystring', 'cookie', 'localStorage', 'navigator', 'htmlTag'],
lookupQuerystring: 'lng',
lookupCookie: 'i18next',
lookupLocalStorage: 'i18nextLng',
// cache user language
caches: ['localStorage'],
excludeCacheFor: ['cimode'],
//cookieMinutes: 10,
//cookieDomain: 'myDomain'
checkWhitelist: true
};
}
var Browser = function () {
function Browser(services) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, Browser);
this.type = 'languageDetector';
this.detectors = {};
this.init(services, options);
}
_createClass(Browser, [{
key: 'init',
value: function init(services) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var i18nOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
this.services = services;
this.options = utils.defaults(options, this.options || {}, getDefaults());
// backwards compatibility
if (this.options.lookupFromUrlIndex) this.options.lookupFromPathIndex = this.options.lookupFromUrlIndex;
this.i18nOptions = i18nOptions;
this.addDetector(_cookie2.default);
this.addDetector(_querystring2.default);
this.addDetector(_localStorage2.default);
this.addDetector(_navigator2.default);
this.addDetector(_htmlTag2.default);
this.addDetector(_path2.default);
this.addDetector(_subdomain2.default);
}
}, {
key: 'addDetector',
value: function addDetector(detector) {
this.detectors[detector.name] = detector;
}
}, {
key: 'detect',
value: function detect(detectionOrder) {
var _this = this;
if (!detectionOrder) detectionOrder = this.options.order;
var detected = [];
detectionOrder.forEach(function (detectorName) {
if (_this.detectors[detectorName]) {
var lookup = _this.detectors[detectorName].lookup(_this.options);
if (lookup && typeof lookup === 'string') lookup = [lookup];
if (lookup) detected = detected.concat(lookup);
}
});
var found = void 0;
detected.forEach(function (lng) {
if (found) return;
var cleanedLng = _this.services.languageUtils.formatLanguageCode(lng);
if (!_this.options.checkWhitelist || _this.services.languageUtils.isWhitelisted(cleanedLng)) found = cleanedLng;
});
if (!found) {
var fallbacks = this.i18nOptions.fallbackLng;
if (typeof fallbacks === 'string') fallbacks = [fallbacks];
if (!fallbacks) fallbacks = [];
if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
found = fallbacks[0];
} else {
found = fallbacks[0] || fallbacks.default && fallbacks.default[0];
}
}
return found;
}
}, {
key: 'cacheUserLanguage',
value: function cacheUserLanguage(lng, caches) {
var _this2 = this;
if (!caches) caches = this.options.caches;
if (!caches) return;
if (this.options.excludeCacheFor && this.options.excludeCacheFor.indexOf(lng) > -1) return;
caches.forEach(function (cacheName) {
if (_this2.detectors[cacheName]) _this2.detectors[cacheName].cacheUserLanguage(lng, _this2.options);
});
}
}]);
return Browser;
}();
Browser.type = 'languageDetector';
exports.default = Browser;

View File

@@ -0,0 +1,32 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.defaults = defaults;
exports.extend = extend;
var arr = [];
var each = arr.forEach;
var slice = arr.slice;
function defaults(obj) {
each.call(slice.call(arguments, 1), function (source) {
if (source) {
for (var prop in source) {
if (obj[prop] === undefined) obj[prop] = source[prop];
}
}
});
return obj;
}
function extend(obj) {
each.call(slice.call(arguments, 1), function (source) {
if (source) {
for (var prop in source) {
obj[prop] = source[prop];
}
}
});
return obj;
}

View File

@@ -0,0 +1,48 @@
var cookie = {
create: function create(name, value, minutes, domain) {
var expires = void 0;
if (minutes) {
var date = new Date();
date.setTime(date.getTime() + minutes * 60 * 1000);
expires = '; expires=' + date.toGMTString();
} else expires = '';
domain = domain ? 'domain=' + domain + ';' : '';
document.cookie = name + '=' + value + expires + ';' + domain + 'path=/';
},
read: function read(name) {
var nameEQ = name + '=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}
return null;
},
remove: function remove(name) {
this.create(name, '', -1);
}
};
export default {
name: 'cookie',
lookup: function lookup(options) {
var found = void 0;
if (options.lookupCookie && typeof document !== 'undefined') {
var c = cookie.read(options.lookupCookie);
if (c) found = c;
}
return found;
},
cacheUserLanguage: function cacheUserLanguage(lng, options) {
if (options.lookupCookie && typeof document !== 'undefined') {
cookie.create(options.lookupCookie, lng, options.cookieMinutes, options.cookieDomain);
}
}
};

View File

@@ -0,0 +1,14 @@
export default {
name: 'htmlTag',
lookup: function lookup(options) {
var found = void 0;
var htmlTag = options.htmlTag || (typeof document !== 'undefined' ? document.documentElement : null);
if (htmlTag && typeof htmlTag.getAttribute === 'function') {
found = htmlTag.getAttribute('lang');
}
return found;
}
};

View File

@@ -0,0 +1,29 @@
var hasLocalStorageSupport = void 0;
try {
hasLocalStorageSupport = window !== 'undefined' && window.localStorage !== null;
var testKey = 'i18next.translate.boo';
window.localStorage.setItem(testKey, 'foo');
window.localStorage.removeItem(testKey);
} catch (e) {
hasLocalStorageSupport = false;
}
export default {
name: 'localStorage',
lookup: function lookup(options) {
var found = void 0;
if (options.lookupLocalStorage && hasLocalStorageSupport) {
var lng = window.localStorage.getItem(options.lookupLocalStorage);
if (lng) found = lng;
}
return found;
},
cacheUserLanguage: function cacheUserLanguage(lng, options) {
if (options.lookupLocalStorage && hasLocalStorageSupport) {
window.localStorage.setItem(options.lookupLocalStorage, lng);
}
}
};

View File

@@ -0,0 +1,24 @@
export default {
name: 'navigator',
lookup: function lookup(options) {
var found = [];
if (typeof navigator !== 'undefined') {
if (navigator.languages) {
// chrome only; not an array, so can't use .push.apply instead of iterating
for (var i = 0; i < navigator.languages.length; i++) {
found.push(navigator.languages[i]);
}
}
if (navigator.userLanguage) {
found.push(navigator.userLanguage);
}
if (navigator.language) {
found.push(navigator.language);
}
}
return found.length > 0 ? found : undefined;
}
};

View File

@@ -0,0 +1,21 @@
export default {
name: 'path',
lookup: function lookup(options) {
var found = void 0;
if (typeof window !== 'undefined') {
var language = window.location.pathname.match(/\/([a-zA-Z-]*)/g);
if (language instanceof Array) {
if (typeof options.lookupFromPathIndex === 'number') {
if (typeof language[options.lookupFromPathIndex] !== 'string') {
return undefined;
}
found = language[options.lookupFromPathIndex].replace('/', '');
} else {
found = language[0].replace('/', '');
}
}
}
return found;
}
};

View File

@@ -0,0 +1,23 @@
export default {
name: 'querystring',
lookup: function lookup(options) {
var found = void 0;
if (typeof window !== 'undefined') {
var query = window.location.search.substring(1);
var params = query.split('&');
for (var i = 0; i < params.length; i++) {
var pos = params[i].indexOf('=');
if (pos > 0) {
var key = params[i].substring(0, pos);
if (key === options.lookupQuerystring) {
found = params[i].substring(pos + 1);
}
}
}
}
return found;
}
};

View File

@@ -0,0 +1,18 @@
export default {
name: 'subdomain',
lookup: function lookup(options) {
var found = void 0;
if (typeof window !== 'undefined') {
var language = window.location.href.match(/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/gi);
if (language instanceof Array) {
if (typeof options.lookupFromSubdomainIndex === 'number') {
found = language[options.lookupFromSubdomainIndex].replace('http://', '').replace('https://', '').replace('.', '');
} else {
found = language[0].replace('http://', '').replace('https://', '').replace('.', '');
}
}
}
return found;
}
};

View File

@@ -0,0 +1,125 @@
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
import * as utils from './utils.js';
import cookie from './browserLookups/cookie.js';
import querystring from './browserLookups/querystring.js';
import localStorage from './browserLookups/localStorage.js';
import navigator from './browserLookups/navigator.js';
import htmlTag from './browserLookups/htmlTag.js';
import path from './browserLookups/path.js';
import subdomain from './browserLookups/subdomain.js';
function getDefaults() {
return {
order: ['querystring', 'cookie', 'localStorage', 'navigator', 'htmlTag'],
lookupQuerystring: 'lng',
lookupCookie: 'i18next',
lookupLocalStorage: 'i18nextLng',
// cache user language
caches: ['localStorage'],
excludeCacheFor: ['cimode'],
//cookieMinutes: 10,
//cookieDomain: 'myDomain'
checkWhitelist: true
};
}
var Browser = function () {
function Browser(services) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, Browser);
this.type = 'languageDetector';
this.detectors = {};
this.init(services, options);
}
_createClass(Browser, [{
key: 'init',
value: function init(services) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var i18nOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
this.services = services;
this.options = utils.defaults(options, this.options || {}, getDefaults());
// backwards compatibility
if (this.options.lookupFromUrlIndex) this.options.lookupFromPathIndex = this.options.lookupFromUrlIndex;
this.i18nOptions = i18nOptions;
this.addDetector(cookie);
this.addDetector(querystring);
this.addDetector(localStorage);
this.addDetector(navigator);
this.addDetector(htmlTag);
this.addDetector(path);
this.addDetector(subdomain);
}
}, {
key: 'addDetector',
value: function addDetector(detector) {
this.detectors[detector.name] = detector;
}
}, {
key: 'detect',
value: function detect(detectionOrder) {
var _this = this;
if (!detectionOrder) detectionOrder = this.options.order;
var detected = [];
detectionOrder.forEach(function (detectorName) {
if (_this.detectors[detectorName]) {
var lookup = _this.detectors[detectorName].lookup(_this.options);
if (lookup && typeof lookup === 'string') lookup = [lookup];
if (lookup) detected = detected.concat(lookup);
}
});
var found = void 0;
detected.forEach(function (lng) {
if (found) return;
var cleanedLng = _this.services.languageUtils.formatLanguageCode(lng);
if (!_this.options.checkWhitelist || _this.services.languageUtils.isWhitelisted(cleanedLng)) found = cleanedLng;
});
if (!found) {
var fallbacks = this.i18nOptions.fallbackLng;
if (typeof fallbacks === 'string') fallbacks = [fallbacks];
if (!fallbacks) fallbacks = [];
if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
found = fallbacks[0];
} else {
found = fallbacks[0] || fallbacks.default && fallbacks.default[0];
}
}
return found;
}
}, {
key: 'cacheUserLanguage',
value: function cacheUserLanguage(lng, caches) {
var _this2 = this;
if (!caches) caches = this.options.caches;
if (!caches) return;
if (this.options.excludeCacheFor && this.options.excludeCacheFor.indexOf(lng) > -1) return;
caches.forEach(function (cacheName) {
if (_this2.detectors[cacheName]) _this2.detectors[cacheName].cacheUserLanguage(lng, _this2.options);
});
}
}]);
return Browser;
}();
Browser.type = 'languageDetector';
export default Browser;

View File

@@ -0,0 +1,25 @@
var arr = [];
var each = arr.forEach;
var slice = arr.slice;
export function defaults(obj) {
each.call(slice.call(arguments, 1), function (source) {
if (source) {
for (var prop in source) {
if (obj[prop] === undefined) obj[prop] = source[prop];
}
}
});
return obj;
}
export function extend(obj) {
each.call(slice.call(arguments, 1), function (source) {
if (source) {
for (var prop in source) {
obj[prop] = source[prop];
}
}
});
return obj;
}

View File

@@ -0,0 +1,323 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.i18nextBrowserLanguageDetector = factory());
}(this, function () { 'use strict';
var arr = [];
var each = arr.forEach;
var slice = arr.slice;
function defaults(obj) {
each.call(slice.call(arguments, 1), function (source) {
if (source) {
for (var prop in source) {
if (obj[prop] === undefined) obj[prop] = source[prop];
}
}
});
return obj;
}
var cookie = {
create: function create(name, value, minutes, domain) {
var expires = void 0;
if (minutes) {
var date = new Date();
date.setTime(date.getTime() + minutes * 60 * 1000);
expires = '; expires=' + date.toGMTString();
} else expires = '';
domain = domain ? 'domain=' + domain + ';' : '';
document.cookie = name + '=' + value + expires + ';' + domain + 'path=/';
},
read: function read(name) {
var nameEQ = name + '=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}
return null;
},
remove: function remove(name) {
this.create(name, '', -1);
}
};
var cookie$1 = {
name: 'cookie',
lookup: function lookup(options) {
var found = void 0;
if (options.lookupCookie && typeof document !== 'undefined') {
var c = cookie.read(options.lookupCookie);
if (c) found = c;
}
return found;
},
cacheUserLanguage: function cacheUserLanguage(lng, options) {
if (options.lookupCookie && typeof document !== 'undefined') {
cookie.create(options.lookupCookie, lng, options.cookieMinutes, options.cookieDomain);
}
}
};
var querystring = {
name: 'querystring',
lookup: function lookup(options) {
var found = void 0;
if (typeof window !== 'undefined') {
var query = window.location.search.substring(1);
var params = query.split('&');
for (var i = 0; i < params.length; i++) {
var pos = params[i].indexOf('=');
if (pos > 0) {
var key = params[i].substring(0, pos);
if (key === options.lookupQuerystring) {
found = params[i].substring(pos + 1);
}
}
}
}
return found;
}
};
var hasLocalStorageSupport = void 0;
try {
hasLocalStorageSupport = window !== 'undefined' && window.localStorage !== null;
var testKey = 'i18next.translate.boo';
window.localStorage.setItem(testKey, 'foo');
window.localStorage.removeItem(testKey);
} catch (e) {
hasLocalStorageSupport = false;
}
var localStorage = {
name: 'localStorage',
lookup: function lookup(options) {
var found = void 0;
if (options.lookupLocalStorage && hasLocalStorageSupport) {
var lng = window.localStorage.getItem(options.lookupLocalStorage);
if (lng) found = lng;
}
return found;
},
cacheUserLanguage: function cacheUserLanguage(lng, options) {
if (options.lookupLocalStorage && hasLocalStorageSupport) {
window.localStorage.setItem(options.lookupLocalStorage, lng);
}
}
};
var navigator$1 = {
name: 'navigator',
lookup: function lookup(options) {
var found = [];
if (typeof navigator !== 'undefined') {
if (navigator.languages) {
// chrome only; not an array, so can't use .push.apply instead of iterating
for (var i = 0; i < navigator.languages.length; i++) {
found.push(navigator.languages[i]);
}
}
if (navigator.userLanguage) {
found.push(navigator.userLanguage);
}
if (navigator.language) {
found.push(navigator.language);
}
}
return found.length > 0 ? found : undefined;
}
};
var htmlTag = {
name: 'htmlTag',
lookup: function lookup(options) {
var found = void 0;
var htmlTag = options.htmlTag || (typeof document !== 'undefined' ? document.documentElement : null);
if (htmlTag && typeof htmlTag.getAttribute === 'function') {
found = htmlTag.getAttribute('lang');
}
return found;
}
};
var path = {
name: 'path',
lookup: function lookup(options) {
var found = void 0;
if (typeof window !== 'undefined') {
var language = window.location.pathname.match(/\/([a-zA-Z-]*)/g);
if (language instanceof Array) {
if (typeof options.lookupFromPathIndex === 'number') {
if (typeof language[options.lookupFromPathIndex] !== 'string') {
return undefined;
}
found = language[options.lookupFromPathIndex].replace('/', '');
} else {
found = language[0].replace('/', '');
}
}
}
return found;
}
};
var subdomain = {
name: 'subdomain',
lookup: function lookup(options) {
var found = void 0;
if (typeof window !== 'undefined') {
var language = window.location.href.match(/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/gi);
if (language instanceof Array) {
if (typeof options.lookupFromSubdomainIndex === 'number') {
found = language[options.lookupFromSubdomainIndex].replace('http://', '').replace('https://', '').replace('.', '');
} else {
found = language[0].replace('http://', '').replace('https://', '').replace('.', '');
}
}
}
return found;
}
};
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function getDefaults() {
return {
order: ['querystring', 'cookie', 'localStorage', 'navigator', 'htmlTag'],
lookupQuerystring: 'lng',
lookupCookie: 'i18next',
lookupLocalStorage: 'i18nextLng',
// cache user language
caches: ['localStorage'],
excludeCacheFor: ['cimode'],
//cookieMinutes: 10,
//cookieDomain: 'myDomain'
checkWhitelist: true
};
}
var Browser = function () {
function Browser(services) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, Browser);
this.type = 'languageDetector';
this.detectors = {};
this.init(services, options);
}
_createClass(Browser, [{
key: 'init',
value: function init(services) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var i18nOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
this.services = services;
this.options = defaults(options, this.options || {}, getDefaults());
// backwards compatibility
if (this.options.lookupFromUrlIndex) this.options.lookupFromPathIndex = this.options.lookupFromUrlIndex;
this.i18nOptions = i18nOptions;
this.addDetector(cookie$1);
this.addDetector(querystring);
this.addDetector(localStorage);
this.addDetector(navigator$1);
this.addDetector(htmlTag);
this.addDetector(path);
this.addDetector(subdomain);
}
}, {
key: 'addDetector',
value: function addDetector(detector) {
this.detectors[detector.name] = detector;
}
}, {
key: 'detect',
value: function detect(detectionOrder) {
var _this = this;
if (!detectionOrder) detectionOrder = this.options.order;
var detected = [];
detectionOrder.forEach(function (detectorName) {
if (_this.detectors[detectorName]) {
var lookup = _this.detectors[detectorName].lookup(_this.options);
if (lookup && typeof lookup === 'string') lookup = [lookup];
if (lookup) detected = detected.concat(lookup);
}
});
var found = void 0;
detected.forEach(function (lng) {
if (found) return;
var cleanedLng = _this.services.languageUtils.formatLanguageCode(lng);
if (!_this.options.checkWhitelist || _this.services.languageUtils.isWhitelisted(cleanedLng)) found = cleanedLng;
});
if (!found) {
var fallbacks = this.i18nOptions.fallbackLng;
if (typeof fallbacks === 'string') fallbacks = [fallbacks];
if (!fallbacks) fallbacks = [];
if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
found = fallbacks[0];
} else {
found = fallbacks[0] || fallbacks.default && fallbacks.default[0];
}
}
return found;
}
}, {
key: 'cacheUserLanguage',
value: function cacheUserLanguage(lng, caches) {
var _this2 = this;
if (!caches) caches = this.options.caches;
if (!caches) return;
if (this.options.excludeCacheFor && this.options.excludeCacheFor.indexOf(lng) > -1) return;
caches.forEach(function (cacheName) {
if (_this2.detectors[cacheName]) _this2.detectors[cacheName].cacheUserLanguage(lng, _this2.options);
});
}
}]);
return Browser;
}();
Browser.type = 'languageDetector';
return Browser;
}));

File diff suppressed because one or more lines are too long

View File

@@ -216,9 +216,10 @@
// cache user language
caches: ['localStorage'],
excludeCacheFor: ['cimode']
excludeCacheFor: ['cimode'],
//cookieMinutes: 10,
//cookieDomain: 'myDomain'
checkWhitelist: true
};
}
@@ -281,7 +282,7 @@
detected.forEach(function (lng) {
if (found) return;
var cleanedLng = _this.services.languageUtils.formatLanguageCode(lng);
if (_this.services.languageUtils.isWhitelisted(cleanedLng)) found = cleanedLng;
if (!_this.options.checkWhitelist || _this.services.languageUtils.isWhitelisted(cleanedLng)) found = cleanedLng;
});
if (!found) {
@@ -294,7 +295,7 @@
} else {
found = fallbacks[0] || fallbacks.default && fallbacks.default[0];
}
};
}
return found;
}

File diff suppressed because one or more lines are too long

View File

@@ -1,61 +1,64 @@
declare namespace I18nextBrowserLanguageDetector {
interface DetectorOptions {
/**
* order and from where user language should be detected
*/
order?: Array<"querystring" | "cookie" | "localStorage" | "navigator" | "htmlTag" | string>;
import * as i18next from "i18next";
/**
* keys or params to lookup language from
*/
lookupQuerystring?: string;
lookupCookie?: string;
lookupLocalStorage?: string;
interface DetectorOptions {
/**
* order and from where user language should be detected
*/
order?: Array<
"querystring" | "cookie" | "localStorage" | "navigator" | "htmlTag" | string
>;
/**
* cache user language on
*/
caches?: string[];
/**
* keys or params to lookup language from
*/
lookupQuerystring?: string;
lookupCookie?: string;
lookupLocalStorage?: string;
/**
* languages to not persist (cookie, localStorage)
*/
excludeCacheFor?: string[];
/**
* cache user language on
*/
caches?: string[];
/**
* optional expire and domain for set cookie
* @default 10
*/
cookieMinutes?: number;
cookieDomain?: string;
/**
* languages to not persist (cookie, localStorage)
*/
excludeCacheFor?: string[];
/**
* optional htmlTag with lang attribute
* @default document.documentElement
*/
htmlTag?: HTMLElement | null;
}
/**
* optional expire and domain for set cookie
* @default 10
*/
cookieMinutes?: number;
cookieDomain?: string;
interface CustomDetector {
name: string;
cacheUserLanguage?(lng: string, options: DetectorOptions): void;
lookup(options: DetectorOptions): string | undefined;
}
/**
* optional htmlTag with lang attribute
* @default document.documentElement
*/
htmlTag?: HTMLElement | null;
}
export default class I18nextBrowserLanguageDetector {
constructor(services?: any, options?: I18nextBrowserLanguageDetector.DetectorOptions);
interface CustomDetector {
name: string;
cacheUserLanguage?(lng: string, options: DetectorOptions): void;
lookup(options: DetectorOptions): string | undefined;
}
export default class I18nextBrowserLanguageDetector
implements i18next.LanguageDetectorModule {
constructor(services?: any, options?: DetectorOptions);
/**
* Adds detector.
*/
addDetector(detector: I18nextBrowserLanguageDetector.CustomDetector): I18nextBrowserLanguageDetector;
addDetector(detector: CustomDetector): I18nextBrowserLanguageDetector;
/**
* Initializes detector.
*/
init(services?: any, options?: I18nextBrowserLanguageDetector.DetectorOptions): void;
init(services?: any, options?: DetectorOptions): void;
detect(detectionOrder?: I18nextBrowserLanguageDetector.DetectorOptions): string | undefined;
detect(detectionOrder?: DetectorOptions["order"]): string | undefined;
cacheUserLanguage(lng: string, caches?: string[]): void;

View File

@@ -1,28 +1,28 @@
{
"_from": "i18next-browser-languagedetector",
"_id": "i18next-browser-languagedetector@3.0.1",
"_from": "i18next-browser-languagedetector@3.1.1",
"_id": "i18next-browser-languagedetector@3.1.1",
"_inBundle": false,
"_integrity": "sha512-WFjPLNPWl62uu07AHY2g+KsC9qz0tyMq+OZEB/H7N58YKL/JLiCz9U709gaR20Mule/Ppn+uyfVx5REJJjn1HA==",
"_integrity": "sha512-JBgFWijjI1t6as4WgGvDdX4GLJPZwC/SMHzLQQ3ef7XaJsEkomlXFqXifKvOVJg09Hj2BVWe6strDdIF4J/0ng==",
"_location": "/i18next-browser-languagedetector",
"_phantomChildren": {},
"_requested": {
"type": "tag",
"type": "version",
"registry": true,
"raw": "i18next-browser-languagedetector",
"raw": "i18next-browser-languagedetector@3.1.1",
"name": "i18next-browser-languagedetector",
"escapedName": "i18next-browser-languagedetector",
"rawSpec": "",
"rawSpec": "3.1.1",
"saveSpec": null,
"fetchSpec": "latest"
"fetchSpec": "3.1.1"
},
"_requiredBy": [
"#USER",
"/"
],
"_resolved": "https://registry.npmjs.org/i18next-browser-languagedetector/-/i18next-browser-languagedetector-3.0.1.tgz",
"_shasum": "a47c43176e8412c91e808afb7c6eb5367649aa8e",
"_spec": "i18next-browser-languagedetector",
"_where": "F:\\projects\\vanillajs-seed",
"_resolved": "https://registry.npmjs.org/i18next-browser-languagedetector/-/i18next-browser-languagedetector-3.1.1.tgz",
"_shasum": "1a0c236d4339476cc3632da60ff947bbc2e3ba3c",
"_spec": "i18next-browser-languagedetector@3.1.1",
"_where": "/home/s2/Code/vanillajs-seed",
"author": {
"name": "Jan Mühlemann",
"email": "jan.muehlemann@gmail.com",
@@ -36,25 +36,26 @@
"deprecated": false,
"description": "language detector used in browser environment for i18next",
"devDependencies": {
"babel-cli": "6.22.2",
"babel-core": "6.22.1",
"babel-eslint": "7.1.1",
"babel-cli": "6.26.0",
"babel-core": "6.26.3",
"babel-eslint": "10.0.2",
"babel-plugin-external-helpers": "6.22.0",
"babel-plugin-transform-es2015-classes": "6.22.0",
"babel-preset-es2015": "6.22.0",
"babel-preset-stage-0": "6.22.0",
"dtslint": "^0.4.2",
"eslint": "2.8.0",
"eslint-config-airbnb": "7.0.0",
"babel-plugin-transform-es2015-classes": "6.24.1",
"babel-preset-es2015": "6.24.1",
"babel-preset-stage-0": "6.24.1",
"dtslint": "^0.9.1",
"eslint": "6.1.0",
"eslint-config-airbnb": "17.1.1",
"i18next": "^17.0.9",
"mkdirp": "0.5.1",
"rimraf": "2.5.2",
"rollup": "0.25.8",
"rollup-plugin-babel": "2.4.0",
"rollup-plugin-node-resolve": "1.7.1",
"rollup-plugin-uglify": "0.2.0",
"tslint": "^5.12.1",
"typescript": "^3.3.1",
"yargs": "4.6.0"
"rimraf": "2.6.3",
"rollup": "1.19.4",
"rollup-plugin-babel": "4.3.3",
"rollup-plugin-node-resolve": "5.2.0",
"rollup-plugin-uglify": "6.0.2",
"tslint": "^5.18.0",
"typescript": "^3.5.3",
"yargs": "13.3.0"
},
"homepage": "https://github.com/i18next/i18next-browser-languageDetector",
"jsnext:main": "dist/es/index.js",
@@ -79,11 +80,12 @@
"clean": "rimraf dist && mkdirp dist",
"copy": "cp ./dist/umd/i18nextBrowserLanguageDetector.min.js ./i18nextBrowserLanguageDetector.min.js && cp ./dist/umd/i18nextBrowserLanguageDetector.js ./i18nextBrowserLanguageDetector.js",
"postversion": "git push && git push --tags",
"pretest": "npm run test:typescript",
"pretest": "npm run test:typescript && npm run test:typescript:noninterop",
"preversion": "npm run build && git push",
"test": "echo 'TODO'",
"test:typescript": "tslint --project tsconfig.json"
"test:typescript": "tslint --project tsconfig.json",
"test:typescript:noninterop": "tslint --project tsconfig.nonEsModuleInterop.json"
},
"types": "./index.d.ts",
"version": "3.0.1"
"version": "3.1.1"
}

View File

@@ -1,31 +0,0 @@
import babel from 'rollup-plugin-babel';
import uglify from 'rollup-plugin-uglify';
import nodeResolve from 'rollup-plugin-node-resolve';
import { argv } from 'yargs';
const format = argv.format || argv.f || 'iife';
const compress = argv.uglify;
const babelOptions = {
exclude: 'node_modules/**',
presets: [['es2015', { modules: false }], 'stage-0'],
babelrc: false
};
const dest = {
amd: `dist/amd/i18nextBrowserLanguageDetector${compress ? '.min' : ''}.js`,
umd: `dist/umd/i18nextBrowserLanguageDetector${compress ? '.min' : ''}.js`,
iife: `dist/iife/i18nextBrowserLanguageDetector${compress ? '.min' : ''}.js`
}[format];
export default {
entry: 'src/index.js',
format,
plugins: [
babel(babelOptions),
nodeResolve({ jsnext: true })
].concat(compress ? uglify() : []),
moduleName: 'i18nextBrowserLanguageDetector',
// moduleId: 'i18nextBrowserLanguageDetector',
dest
};

View File

@@ -1,15 +0,0 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"lib": ["es6", "dom"],
"jsx": "react",
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noEmit": true,
"baseUrl": ".",
"paths": { "i18next-browser-languagedetector": ["./index.d.ts"] }
},
"include": ["./indext.d.ts", "./test/**/*.ts*"]
}

View File

@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
// typescript defaults to these
"esModuleInterop": false,
"allowSyntheticDefaultImports": false
},
"include": ["./test/typescript/nonEsModuleInterop/**/*.ts"],
"exclude": []
}

View File

@@ -1,7 +0,0 @@
{
"defaultSeverity": "error",
"extends": "dtslint/dtslint.json",
"rules": {
"semicolon": false
}
}