@lingui/detect-locale
Advanced tools
Comparing version 3.17.0 to 3.17.1
@@ -6,21 +6,17 @@ 'use strict'; | ||
return; | ||
} // To prevent the for loop in the first place assign an empty array | ||
} | ||
// To prevent the for loop in the first place assign an empty array | ||
// in case there are no cookies at all. | ||
var cookies = globalThis.document.cookie ? globalThis.document.cookie.split('; ') : []; | ||
var jar = {}; | ||
for (var i = 0; i < cookies.length; i++) { | ||
var parts = cookies[i].split('='); | ||
var value = parts.slice(1).join('='); | ||
const cookies = globalThis.document.cookie ? globalThis.document.cookie.split("; ") : []; | ||
const jar = {}; | ||
for (let i = 0; i < cookies.length; i++) { | ||
const parts = cookies[i].split("="); | ||
let value = parts.slice(1).join("="); | ||
if (value[0] === '"') { | ||
value = value.slice(1, -1); | ||
} | ||
try { | ||
var foundKey = parts[0].replace(/(%[\dA-F]{2})+/gi, decodeURIComponent); | ||
const foundKey = parts[0].replace(/(%[\dA-F]{2})+/gi, decodeURIComponent); | ||
jar[foundKey] = value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent); | ||
if (key === foundKey) { | ||
@@ -31,3 +27,2 @@ break; | ||
} | ||
return key ? jar[key] : jar; | ||
@@ -41,9 +36,7 @@ } | ||
function detectFromPath(localePathIndex) { | ||
var location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalThis.location; | ||
var locale = location.pathname.match(/\/([a-zA-Z-]*)/g); | ||
let location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalThis.location; | ||
const locale = location.pathname.match(/\/([a-zA-Z-]*)/g); | ||
if (Array.isArray(locale)) { | ||
return locale[localePathIndex].replace("/", ""); | ||
} | ||
return null; | ||
@@ -53,10 +46,8 @@ } | ||
function detectFromStorage(key) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { | ||
useSessionStorage: false | ||
}; | ||
if (options.useSessionStorage) { | ||
return globalThis.sessionStorage.getItem(key); | ||
} | ||
return globalThis.localStorage.getItem(key); | ||
@@ -66,4 +57,4 @@ } | ||
function detectFromNavigator() { | ||
var navigator = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : globalThis.navigator; | ||
var result = navigator.language || navigator.userLanguage; | ||
let navigator = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : globalThis.navigator; | ||
const result = navigator.language || navigator.userLanguage; | ||
return result; | ||
@@ -73,9 +64,7 @@ } | ||
function detectFromSubdomain(localeSubdomainIndex) { | ||
var location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalThis.location; | ||
var locale = location.href.match(/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/gi); | ||
let location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalThis.location; | ||
const locale = location.href.match(/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/gi); | ||
if (Array.isArray(locale)) { | ||
return locale[localeSubdomainIndex].replace('http://', '').replace('https://', '').replace('.', ''); | ||
return locale[localeSubdomainIndex].replace("http://", "").replace("https://", "").replace(".", ""); | ||
} | ||
return null; | ||
@@ -85,8 +74,6 @@ } | ||
function detectHtmlTag(htmlTagIdentifier) { | ||
var document = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalThis.document; | ||
let document = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalThis.document; | ||
if (htmlTagIdentifier) { | ||
return document.documentElement.getAttribute(htmlTagIdentifier); | ||
} | ||
return null; | ||
@@ -102,9 +89,10 @@ } | ||
function parse(query) { | ||
var parser = /([^=?#&]+)=?([^&]*)/g; | ||
var result = {}; | ||
var part; | ||
const parser = /([^=?#&]+)=?([^&]*)/g; | ||
const result = {}; | ||
let part; | ||
while (part = parser.exec(query)) { | ||
const key = decode(part[1]); | ||
const value = decode(part[2]); | ||
while (part = parser.exec(query)) { | ||
var key = decode(part[1]); | ||
var value = decode(part[2]); // | ||
// | ||
// Prevent overriding of existing properties. This ensures that build-in | ||
@@ -117,13 +105,10 @@ // methods like `toString` or __proto__ are not overriden by malicious | ||
// | ||
if (key === null || value === null || key in result) continue; | ||
result[key] = value; | ||
} | ||
return result; | ||
} | ||
function decode(input) { | ||
try { | ||
return decodeURIComponent(input.replace(/\+/g, ' ')); | ||
return decodeURIComponent(input.replace(/\+/g, " ")); | ||
} catch (e) { | ||
@@ -135,5 +120,5 @@ return null; | ||
function detectFromUrl(parameter) { | ||
var location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalThis.location; | ||
let location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalThis.location; | ||
if (!parameter) throw new Error("fromUrl parameter is required"); | ||
var result = parse(location.search)[parameter] || null; | ||
const result = parse(location.search)[parameter] || null; | ||
return result; | ||
@@ -146,23 +131,17 @@ } | ||
} | ||
for (var i = 0; i < args.length; i++) { | ||
var res = typeof args[i] === "function" ? args[i]() : args[i]; | ||
for (let i = 0; i < args.length; i++) { | ||
const res = typeof args[i] === "function" ? args[i]() : args[i]; | ||
if (res) return res; | ||
} | ||
return null; | ||
} | ||
function multipleDetect() { | ||
var locales = []; | ||
const locales = []; | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
for (var i = 0; i < args.length; i++) { | ||
var res = typeof args[i] === "function" ? args[i]() : args[i]; | ||
for (let i = 0; i < args.length; i++) { | ||
const res = typeof args[i] === "function" ? args[i]() : args[i]; | ||
if (res) locales.push(res); | ||
} | ||
return locales; | ||
@@ -169,0 +148,0 @@ } |
function getCookie(key) { | ||
if (!key) { | ||
return; | ||
} // To prevent the for loop in the first place assign an empty array | ||
} | ||
// To prevent the for loop in the first place assign an empty array | ||
// in case there are no cookies at all. | ||
var cookies = globalThis.document.cookie ? globalThis.document.cookie.split('; ') : []; | ||
var jar = {}; | ||
for (var i = 0; i < cookies.length; i++) { | ||
var parts = cookies[i].split('='); | ||
var value = parts.slice(1).join('='); | ||
const cookies = globalThis.document.cookie ? globalThis.document.cookie.split("; ") : []; | ||
const jar = {}; | ||
for (let i = 0; i < cookies.length; i++) { | ||
const parts = cookies[i].split("="); | ||
let value = parts.slice(1).join("="); | ||
if (value[0] === '"') { | ||
value = value.slice(1, -1); | ||
} | ||
try { | ||
var foundKey = parts[0].replace(/(%[\dA-F]{2})+/gi, decodeURIComponent); | ||
const foundKey = parts[0].replace(/(%[\dA-F]{2})+/gi, decodeURIComponent); | ||
jar[foundKey] = value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent); | ||
if (key === foundKey) { | ||
@@ -28,3 +24,2 @@ break; | ||
} | ||
return key ? jar[key] : jar; | ||
@@ -38,9 +33,7 @@ } | ||
function detectFromPath(localePathIndex) { | ||
var location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalThis.location; | ||
var locale = location.pathname.match(/\/([a-zA-Z-]*)/g); | ||
let location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalThis.location; | ||
const locale = location.pathname.match(/\/([a-zA-Z-]*)/g); | ||
if (Array.isArray(locale)) { | ||
return locale[localePathIndex].replace("/", ""); | ||
} | ||
return null; | ||
@@ -50,10 +43,8 @@ } | ||
function detectFromStorage(key) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { | ||
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { | ||
useSessionStorage: false | ||
}; | ||
if (options.useSessionStorage) { | ||
return globalThis.sessionStorage.getItem(key); | ||
} | ||
return globalThis.localStorage.getItem(key); | ||
@@ -63,4 +54,4 @@ } | ||
function detectFromNavigator() { | ||
var navigator = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : globalThis.navigator; | ||
var result = navigator.language || navigator.userLanguage; | ||
let navigator = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : globalThis.navigator; | ||
const result = navigator.language || navigator.userLanguage; | ||
return result; | ||
@@ -70,9 +61,7 @@ } | ||
function detectFromSubdomain(localeSubdomainIndex) { | ||
var location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalThis.location; | ||
var locale = location.href.match(/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/gi); | ||
let location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalThis.location; | ||
const locale = location.href.match(/(?:http[s]*\:\/\/)*(.*?)\.(?=[^\/]*\..{2,5})/gi); | ||
if (Array.isArray(locale)) { | ||
return locale[localeSubdomainIndex].replace('http://', '').replace('https://', '').replace('.', ''); | ||
return locale[localeSubdomainIndex].replace("http://", "").replace("https://", "").replace(".", ""); | ||
} | ||
return null; | ||
@@ -82,8 +71,6 @@ } | ||
function detectHtmlTag(htmlTagIdentifier) { | ||
var document = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalThis.document; | ||
let document = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalThis.document; | ||
if (htmlTagIdentifier) { | ||
return document.documentElement.getAttribute(htmlTagIdentifier); | ||
} | ||
return null; | ||
@@ -99,9 +86,10 @@ } | ||
function parse(query) { | ||
var parser = /([^=?#&]+)=?([^&]*)/g; | ||
var result = {}; | ||
var part; | ||
const parser = /([^=?#&]+)=?([^&]*)/g; | ||
const result = {}; | ||
let part; | ||
while (part = parser.exec(query)) { | ||
const key = decode(part[1]); | ||
const value = decode(part[2]); | ||
while (part = parser.exec(query)) { | ||
var key = decode(part[1]); | ||
var value = decode(part[2]); // | ||
// | ||
// Prevent overriding of existing properties. This ensures that build-in | ||
@@ -114,13 +102,10 @@ // methods like `toString` or __proto__ are not overriden by malicious | ||
// | ||
if (key === null || value === null || key in result) continue; | ||
result[key] = value; | ||
} | ||
return result; | ||
} | ||
function decode(input) { | ||
try { | ||
return decodeURIComponent(input.replace(/\+/g, ' ')); | ||
return decodeURIComponent(input.replace(/\+/g, " ")); | ||
} catch (e) { | ||
@@ -132,5 +117,5 @@ return null; | ||
function detectFromUrl(parameter) { | ||
var location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalThis.location; | ||
let location = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : globalThis.location; | ||
if (!parameter) throw new Error("fromUrl parameter is required"); | ||
var result = parse(location.search)[parameter] || null; | ||
const result = parse(location.search)[parameter] || null; | ||
return result; | ||
@@ -143,23 +128,17 @@ } | ||
} | ||
for (var i = 0; i < args.length; i++) { | ||
var res = typeof args[i] === "function" ? args[i]() : args[i]; | ||
for (let i = 0; i < args.length; i++) { | ||
const res = typeof args[i] === "function" ? args[i]() : args[i]; | ||
if (res) return res; | ||
} | ||
return null; | ||
} | ||
function multipleDetect() { | ||
var locales = []; | ||
const locales = []; | ||
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
for (var i = 0; i < args.length; i++) { | ||
var res = typeof args[i] === "function" ? args[i]() : args[i]; | ||
for (let i = 0; i < args.length; i++) { | ||
const res = typeof args[i] === "function" ? args[i]() : args[i]; | ||
if (res) locales.push(res); | ||
} | ||
return locales; | ||
@@ -166,0 +145,0 @@ } |
@@ -9,3 +9,3 @@ declare function detectFromCookie(key: string): LocaleString; | ||
declare type IE11NavigatorLanguage = { | ||
type IE11NavigatorLanguage = { | ||
userLanguage?: string; | ||
@@ -21,4 +21,4 @@ }; | ||
declare type LocaleString = string; | ||
declare type DetectParamsFunctions = string; | ||
type LocaleString = string; | ||
type DetectParamsFunctions = string; | ||
declare function detect(...args: any[]): LocaleString | null; | ||
@@ -25,0 +25,0 @@ declare function multipleDetect(...args: any[]): LocaleString[]; |
@@ -6,2 +6,10 @@ # Change Log | ||
## [3.17.1](https://github.com/lingui/js-lingui/compare/v3.17.0...v3.17.1) (2023-02-07) | ||
**Note:** Version bump only for package @lingui/detect-locale | ||
# [3.17.0](https://github.com/lingui/js-lingui/compare/v3.16.1...v3.17.0) (2023-02-01) | ||
@@ -8,0 +16,0 @@ |
{ | ||
"name": "@lingui/detect-locale", | ||
"version": "3.17.0", | ||
"version": "3.17.1", | ||
"sideEffects": false, | ||
@@ -47,3 +47,3 @@ "description": "@Lingui package to help you find the correct browser/server locale", | ||
}, | ||
"gitHead": "1c8bc46213b35b25da8fe7a80ddcf6f6a5d9d539" | ||
"gitHead": "76ef4e8d1c668578ce2c3829ebf35d22ca5e679c" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
280
44948