New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@fluent/langneg

Package Overview
Dependencies
Maintainers
4
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fluent/langneg - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

esm/accepted_languages.d.ts

6

CHANGELOG.md
# Changelog
## @fluent/langneg 0.4.0 (March 31, 2020)
- Migrate `@fluent/langneg` to TypeScript. (#462)
There are no functional nor API changes in this release.
## @fluent/langneg 0.3.0 (July 25, 2019)

@@ -4,0 +10,0 @@

191

compat.js

@@ -1,2 +0,2 @@

/* @fluent/langneg@0.3.0 */
/* @fluent/langneg@0.4.0 */
(function (global, factory) {

@@ -6,3 +6,3 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :

(global = global || self, factory(global.FluentLangNeg = {}));
}(this, function (exports) { 'use strict';
}(this, (function (exports) { 'use strict';

@@ -18,2 +18,6 @@ function _slicedToArray(arr, i) {

function _iterableToArrayLimit(arr, i) {
if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
return;
}
var _arr = [];

@@ -58,3 +62,3 @@ var _n = true;

const likelySubtagsMin = {
var likelySubtagsMin = {
"ar": "ar-arab-eg",

@@ -82,3 +86,3 @@ "az-arab": "az-arab-ir",

};
const regionMatchingLangs = ["az", "bg", "cs", "de", "es", "fi", "fr", "hu", "it", "lt", "lv", "nl", "pl", "ro", "ru"];
var regionMatchingLangs = ["az", "bg", "cs", "de", "es", "fi", "fr", "hu", "it", "lt", "lv", "nl", "pl", "ro", "ru"];
function getLikelySubtagsMin(loc) {

@@ -89,5 +93,5 @@ if (likelySubtagsMin.hasOwnProperty(loc)) {

const locale = new Locale(loc);
var locale = new Locale(loc);
if (regionMatchingLangs.includes(locale.language)) {
if (locale.language && regionMatchingLangs.includes(locale.language)) {
locale.region = locale.language.toUpperCase();

@@ -100,6 +104,6 @@ return locale;

const languageCodeRe = "([a-z]{2,3}|\\*)";
const scriptCodeRe = "(?:-([a-z]{4}|\\*))";
const regionCodeRe = "(?:-([a-z]{2}|\\*))";
const variantCodeRe = "(?:-(([0-9][a-z0-9]{3}|[a-z0-9]{5,8})|\\*))";
var languageCodeRe = "([a-z]{2,3}|\\*)";
var scriptCodeRe = "(?:-([a-z]{4}|\\*))";
var regionCodeRe = "(?:-([a-z]{2}|\\*))";
var variantCodeRe = "(?:-(([0-9][a-z0-9]{3}|[a-z0-9]{5,8})|\\*))";
/**

@@ -118,4 +122,3 @@ * Regular expression splitting locale id into four pieces:

const localeRe = new RegExp("^".concat(languageCodeRe).concat(scriptCodeRe, "?").concat(regionCodeRe, "?").concat(variantCodeRe, "?$"), "i");
const localeParts = ["language", "script", "region", "variant"];
var localeRe = new RegExp("^".concat(languageCodeRe).concat(scriptCodeRe, "?").concat(regionCodeRe, "?").concat(variantCodeRe, "?$"), "i");
class Locale {

@@ -132,3 +135,3 @@ /**

constructor(locale) {
const result = localeRe.exec(locale.replace(/_/g, "-"));
var result = localeRe.exec(locale.replace(/_/g, "-"));

@@ -140,3 +143,3 @@ if (!result) {

let _result = _slicedToArray(result, 5),
var _result = _slicedToArray(result, 5),
language = _result[1],

@@ -163,16 +166,14 @@ script = _result[2],

isEqual(locale) {
return localeParts.every(part => this[part] === locale[part]);
isEqual(other) {
return this.language === other.language && this.script === other.script && this.region === other.region && this.variant === other.variant;
}
matches(locale) {
let thisRange = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
let otherRange = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
return localeParts.every(part => {
return thisRange && this[part] === undefined || otherRange && locale[part] === undefined || this[part] === locale[part];
});
matches(other) {
var thisRange = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
var otherRange = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
return (this.language === other.language || thisRange && this.language === undefined || otherRange && other.language === undefined) && (this.script === other.script || thisRange && this.script === undefined || otherRange && other.script === undefined) && (this.region === other.region || thisRange && this.region === undefined || otherRange && other.region === undefined) && (this.variant === other.variant || thisRange && this.variant === undefined || otherRange && other.variant === undefined);
}
toString() {
return localeParts.map(part => this[part]).filter(part => part !== undefined).join("-");
return [this.language, this.script, this.region, this.variant].filter(part => part !== undefined).join("-");
}

@@ -189,6 +190,9 @@

addLikelySubtags() {
const newLocale = getLikelySubtagsMin(this.toString().toLowerCase());
var newLocale = getLikelySubtagsMin(this.toString().toLowerCase());
if (newLocale) {
localeParts.forEach(part => this[part] = newLocale[part]);
this.language = newLocale.language;
this.script = newLocale.script;
this.region = newLocale.region;
this.variant = newLocale.variant;
return true;

@@ -274,5 +278,4 @@ }

function filterMatches(requestedLocales, availableLocales, strategy) {
/* eslint complexity: ["error", 31]*/
const supportedLocales = new Set();
const availableLocalesMap = new Map();
var supportedLocales = new Set();
var availableLocalesMap = new Map();
var _iteratorNormalCompletion = true;

@@ -284,4 +287,4 @@ var _didIteratorError = false;

for (var _iterator = availableLocales[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
let locale = _step.value;
let newLocale = new Locale(locale);
var locale = _step.value;
var newLocale = new Locale(locale);

@@ -313,5 +316,5 @@ if (newLocale.isWellFormed) {

outer: for (var _iterator2 = requestedLocales[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
const reqLocStr = _step2.value;
const reqLocStrLC = reqLocStr.toLowerCase();
const requestedLocale = new Locale(reqLocStrLC);
var reqLocStr = _step2.value;
var reqLocStrLC = reqLocStr.toLowerCase();
var requestedLocale = new Locale(reqLocStrLC);

@@ -330,7 +333,7 @@ if (requestedLocale.language === undefined) {

for (var _iterator3 = availableLocalesMap.keys()[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
const key = _step3.value;
var _key2 = _step3.value;
if (reqLocStrLC === key.toLowerCase()) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (reqLocStrLC === _key2.toLowerCase()) {
supportedLocales.add(_key2);
availableLocalesMap.delete(_key2);

@@ -370,9 +373,9 @@ if (strategy === "lookup") {

for (var _iterator4 = availableLocalesMap.entries()[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
const _step4$value = _slicedToArray(_step4.value, 2),
key = _step4$value[0],
availableLocale = _step4$value[1];
var _step4$value = _slicedToArray(_step4.value, 2),
_key3 = _step4$value[0],
_availableLocale2 = _step4$value[1];
if (availableLocale.matches(requestedLocale, true, false)) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (_availableLocale2.matches(requestedLocale, true, false)) {
supportedLocales.add(_key3);
availableLocalesMap.delete(_key3);

@@ -414,5 +417,5 @@ if (strategy === "lookup") {

for (var _iterator5 = availableLocalesMap.entries()[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
const _step5$value = _slicedToArray(_step5.value, 2),
key = _step5$value[0],
availableLocale = _step5$value[1];
var _step5$value = _slicedToArray(_step5.value, 2),
key = _step5$value[0],
availableLocale = _step5$value[1];

@@ -457,9 +460,9 @@ if (availableLocale.matches(requestedLocale, true, false)) {

for (var _iterator6 = availableLocalesMap.entries()[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
const _step6$value = _slicedToArray(_step6.value, 2),
key = _step6$value[0],
availableLocale = _step6$value[1];
var _step6$value = _slicedToArray(_step6.value, 2),
_key4 = _step6$value[0],
_availableLocale3 = _step6$value[1];
if (availableLocale.matches(requestedLocale, true, true)) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (_availableLocale3.matches(requestedLocale, true, true)) {
supportedLocales.add(_key4);
availableLocalesMap.delete(_key4);

@@ -505,9 +508,9 @@ if (strategy === "lookup") {

for (var _iterator7 = availableLocalesMap.entries()[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
const _step7$value = _slicedToArray(_step7.value, 2),
key = _step7$value[0],
availableLocale = _step7$value[1];
var _step7$value = _slicedToArray(_step7.value, 2),
_key = _step7$value[0],
_availableLocale = _step7$value[1];
if (availableLocale.matches(requestedLocale, true, false)) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (_availableLocale.matches(requestedLocale, true, false)) {
supportedLocales.add(_key);
availableLocalesMap.delete(_key);

@@ -548,9 +551,9 @@ if (strategy === "lookup") {

for (var _iterator8 = availableLocalesMap.entries()[Symbol.iterator](), _step8; !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) {
const _step8$value = _slicedToArray(_step8.value, 2),
key = _step8$value[0],
availableLocale = _step8$value[1];
var _step8$value = _slicedToArray(_step8.value, 2),
_key5 = _step8$value[0],
_availableLocale4 = _step8$value[1];
if (availableLocale.matches(requestedLocale, true, true)) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (_availableLocale4.matches(requestedLocale, true, true)) {
supportedLocales.add(_key5);
availableLocalesMap.delete(_key5);

@@ -599,21 +602,2 @@ if (strategy === "lookup") {

function GetOption(options, property, type, values, fallback) {
let value = options[property];
if (value !== undefined) {
if (type === "boolean") {
value = new Boolean(value);
} else if (type === "string") {
value = String(value);
}
if (values !== undefined && values.indexOf(value) === -1) {
throw new Error("Invalid option value");
}
return value;
}
return fallback;
}
/**

@@ -663,21 +647,15 @@ * Negotiates the languages between the list of requested locales against

function negotiateLanguages(requestedLocales, availableLocales) {
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
const defaultLocale = GetOption(options, "defaultLocale", "string");
const strategy = GetOption(options, "strategy", "string", ["filtering", "matching", "lookup"], "filtering");
var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
_ref$strategy = _ref.strategy,
strategy = _ref$strategy === void 0 ? "filtering" : _ref$strategy,
defaultLocale = _ref.defaultLocale;
if (strategy === "lookup" && !defaultLocale) {
throw new Error("defaultLocale cannot be undefined for strategy `lookup`");
}
var supportedLocales = filterMatches(Array.from(Object(requestedLocales)).map(String), Array.from(Object(availableLocales)).map(String), strategy);
const resolvedReqLoc = Array.from(Object(requestedLocales)).map(loc => {
return String(loc);
});
const resolvedAvailLoc = Array.from(Object(availableLocales)).map(loc => {
return String(loc);
});
const supportedLocales = filterMatches(resolvedReqLoc, resolvedAvailLoc, strategy);
if (strategy === "lookup") {
if (defaultLocale === undefined) {
throw new Error("defaultLocale cannot be undefined for strategy `lookup`");
}
if (strategy === "lookup") {
if (supportedLocales.length === 0) {

@@ -694,21 +672,12 @@ supportedLocales.push(defaultLocale);

function acceptedLanguages() {
let string = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
if (typeof string !== "string") {
if (typeof str !== "string") {
throw new TypeError("Argument must be a string");
}
const tokens = string.split(",").map(t => t.trim());
var tokens = str.split(",").map(t => t.trim());
return tokens.filter(t => t !== "").map(t => t.split(";")[0]);
}
/*
* @module fluent-langneg
* @overview
*
* `fluent-langneg` provides language negotiation API that fits into
* Project Fluent localization composition and fallbacking strategy.
*
*/
exports.acceptedLanguages = acceptedLanguages;

@@ -719,2 +688,2 @@ exports.negotiateLanguages = negotiateLanguages;

}));
})));

@@ -1,500 +0,438 @@

/* @fluent/langneg@0.3.0 */
/* @fluent/langneg@0.4.0 */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define('@fluent/langneg', ['exports'], factory) :
(global = global || self, factory(global.FluentLangNeg = {}));
}(this, function (exports) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define('@fluent/langneg', ['exports'], factory) :
(global = global || self, factory(global.FluentLangNeg = {}));
}(this, (function (exports) { 'use strict';
/**
* Below is a manually a list of likely subtags corresponding to Unicode
* CLDR likelySubtags list.
* This list is curated by the maintainers of Project Fluent and is
* intended to be used in place of the full likelySubtags list in use cases
* where full list cannot be (for example, due to the size).
*
* This version of the list is based on CLDR 30.0.3.
*/
const likelySubtagsMin = {
"ar": "ar-arab-eg",
"az-arab": "az-arab-ir",
"az-ir": "az-arab-ir",
"be": "be-cyrl-by",
"da": "da-latn-dk",
"el": "el-grek-gr",
"en": "en-latn-us",
"fa": "fa-arab-ir",
"ja": "ja-jpan-jp",
"ko": "ko-kore-kr",
"pt": "pt-latn-br",
"sr": "sr-cyrl-rs",
"sr-ru": "sr-latn-ru",
"sv": "sv-latn-se",
"ta": "ta-taml-in",
"uk": "uk-cyrl-ua",
"zh": "zh-hans-cn",
"zh-hant": "zh-hant-tw",
"zh-hk": "zh-hant-hk",
"zh-gb": "zh-hant-gb",
"zh-us": "zh-hant-us",
};
const regionMatchingLangs = [
"az",
"bg",
"cs",
"de",
"es",
"fi",
"fr",
"hu",
"it",
"lt",
"lv",
"nl",
"pl",
"ro",
"ru",
];
function getLikelySubtagsMin(loc) {
if (likelySubtagsMin.hasOwnProperty(loc)) {
return new Locale(likelySubtagsMin[loc]);
/**
* Below is a manually a list of likely subtags corresponding to Unicode
* CLDR likelySubtags list.
* This list is curated by the maintainers of Project Fluent and is
* intended to be used in place of the full likelySubtags list in use cases
* where full list cannot be (for example, due to the size).
*
* This version of the list is based on CLDR 30.0.3.
*/
const likelySubtagsMin = {
"ar": "ar-arab-eg",
"az-arab": "az-arab-ir",
"az-ir": "az-arab-ir",
"be": "be-cyrl-by",
"da": "da-latn-dk",
"el": "el-grek-gr",
"en": "en-latn-us",
"fa": "fa-arab-ir",
"ja": "ja-jpan-jp",
"ko": "ko-kore-kr",
"pt": "pt-latn-br",
"sr": "sr-cyrl-rs",
"sr-ru": "sr-latn-ru",
"sv": "sv-latn-se",
"ta": "ta-taml-in",
"uk": "uk-cyrl-ua",
"zh": "zh-hans-cn",
"zh-hant": "zh-hant-tw",
"zh-hk": "zh-hant-hk",
"zh-gb": "zh-hant-gb",
"zh-us": "zh-hant-us",
};
const regionMatchingLangs = [
"az",
"bg",
"cs",
"de",
"es",
"fi",
"fr",
"hu",
"it",
"lt",
"lv",
"nl",
"pl",
"ro",
"ru",
];
function getLikelySubtagsMin(loc) {
if (likelySubtagsMin.hasOwnProperty(loc)) {
return new Locale(likelySubtagsMin[loc]);
}
const locale = new Locale(loc);
if (locale.language && regionMatchingLangs.includes(locale.language)) {
locale.region = locale.language.toUpperCase();
return locale;
}
return null;
}
const locale = new Locale(loc);
if (regionMatchingLangs.includes(locale.language)) {
locale.region = locale.language.toUpperCase();
return locale;
}
return null;
}
/* eslint no-magic-numbers: 0 */
const languageCodeRe = "([a-z]{2,3}|\\*)";
const scriptCodeRe = "(?:-([a-z]{4}|\\*))";
const regionCodeRe = "(?:-([a-z]{2}|\\*))";
const variantCodeRe = "(?:-(([0-9][a-z0-9]{3}|[a-z0-9]{5,8})|\\*))";
/**
* Regular expression splitting locale id into four pieces:
*
* Example: `en-Latn-US-macos`
*
* language: en
* script: Latn
* region: US
* variant: macos
*
* It can also accept a range `*` character on any position.
*/
const localeRe = new RegExp(
`^${languageCodeRe}${scriptCodeRe}?${regionCodeRe}?${variantCodeRe}?$`, "i");
const localeParts = ["language", "script", "region", "variant"];
class Locale {
/* eslint no-magic-numbers: 0 */
const languageCodeRe = "([a-z]{2,3}|\\*)";
const scriptCodeRe = "(?:-([a-z]{4}|\\*))";
const regionCodeRe = "(?:-([a-z]{2}|\\*))";
const variantCodeRe = "(?:-(([0-9][a-z0-9]{3}|[a-z0-9]{5,8})|\\*))";
/**
* Parses a locale id using the localeRe into an array with four elements.
* Regular expression splitting locale id into four pieces:
*
* If the second argument `range` is set to true, it places range `*` char
* in place of any missing piece.
* Example: `en-Latn-US-macos`
*
* It also allows skipping the script section of the id, so `en-US` is
* properly parsed as `en-*-US-*`.
* language: en
* script: Latn
* region: US
* variant: macos
*
* It can also accept a range `*` character on any position.
*/
constructor(locale) {
const result = localeRe.exec(locale.replace(/_/g, "-"));
if (!result) {
this.isWellFormed = false;
return;
}
let [, language, script, region, variant] = result;
if (language) {
this.language = language.toLowerCase();
}
if (script) {
this.script = script[0].toUpperCase() + script.slice(1);
}
if (region) {
this.region = region.toUpperCase();
}
this.variant = variant;
this.isWellFormed = true;
}
isEqual(locale) {
return localeParts.every(part => this[part] === locale[part]);
}
matches(locale, thisRange = false, otherRange = false) {
return localeParts.every(part => {
return ((thisRange && this[part] === undefined) ||
(otherRange && locale[part] === undefined) ||
this[part] === locale[part]);
});
}
toString() {
return localeParts
.map(part => this[part])
.filter(part => part !== undefined)
.join("-");
}
clearVariants() {
this.variant = undefined;
}
clearRegion() {
this.region = undefined;
}
addLikelySubtags() {
const newLocale = getLikelySubtagsMin(this.toString().toLowerCase());
if (newLocale) {
localeParts.forEach(part => this[part] = newLocale[part]);
return true;
}
return false;
}
}
/* eslint no-magic-numbers: 0 */
/**
* Negotiates the languages between the list of requested locales against
* a list of available locales.
*
* The algorithm is based on the BCP4647 3.3.2 Extended Filtering algorithm,
* with several modifications:
*
* 1) available locales are treated as ranges
*
* This change allows us to match a more specific request against
* more generic available locale.
*
* For example, if the available locale list provides locale `en`,
* and the requested locale is `en-US`, we treat the available locale as
* a locale that matches all possible english requests.
*
* This means that we expect available locale ID to be as precize as
* the matches they want to cover.
*
* For example, if there is only `sr` available, it's ok to list
* it in available locales. But once the available locales has both,
* Cyrl and Latn variants, the locale IDs should be `sr-Cyrl` and `sr-Latn`
* to avoid any `sr-*` request to match against whole `sr` range.
*
* What it does ([requested] * [available] = [supported]):
*
* ['en-US'] * ['en'] = ['en']
*
* 2) likely subtags from LDML 4.3 Likely Subtags has been added
*
* The most obvious likely subtag that can be computed is a duplication
* of the language field onto region field (`fr` => `fr-FR`).
*
* On top of that, likely subtags may use a list of mappings, that
* allow the algorithm to handle non-obvious matches.
* For example, making sure that we match `en` to `en-US` or `sr` to
* `sr-Cyrl`, while `sr-RU` to `sr-Latn-RU`.
*
* This list can be taken directly from CLDR Supplemental Data.
*
* What it does ([requested] * [available] = [supported]):
*
* ['fr'] * ['fr-FR'] = ['fr-FR']
* ['en'] * ['en-US'] = ['en-US']
* ['sr'] * ['sr-Latn', 'sr-Cyrl'] = ['sr-Cyrl']
*
* 3) variant/region range check has been added
*
* Lastly, the last form of check is against the requested locale ID
* but with the variant/region field replaced with a `*` range.
*
* The rationale here laid out in LDML 4.4 Language Matching:
* "(...) normally the fall-off between the user's languages is
* substantially greated than regional variants."
*
* In other words, if we can't match for the given region, maybe
* we can match for the same language/script but other region, and
* it will in most cases be preferred over falling back on the next
* language.
*
* What it does ([requested] * [available] = [supported]):
*
* ['en-AU'] * ['en-US'] = ['en-US']
* ['sr-RU'] * ['sr-Latn-RO'] = ['sr-Latn-RO'] // sr-RU -> sr-Latn-RU
*
* It works similarly to getParentLocales algo, except that we stop
* after matching against variant/region ranges and don't try to match
* ignoring script ranges. That means that `sr-Cyrl` will never match
* against `sr-Latn`.
*/
function filterMatches(
requestedLocales, availableLocales, strategy
) {
/* eslint complexity: ["error", 31]*/
const supportedLocales = new Set();
const availableLocalesMap = new Map();
for (let locale of availableLocales) {
let newLocale = new Locale(locale);
if (newLocale.isWellFormed) {
availableLocalesMap.set(locale, new Locale(locale));
}
}
outer:
for (const reqLocStr of requestedLocales) {
const reqLocStrLC = reqLocStr.toLowerCase();
const requestedLocale = new Locale(reqLocStrLC);
if (requestedLocale.language === undefined) {
continue;
}
// 1) Attempt to make an exact match
// Example: `en-US` === `en-US`
for (const key of availableLocalesMap.keys()) {
if (reqLocStrLC === key.toLowerCase()) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (strategy === "lookup") {
return Array.from(supportedLocales);
} else if (strategy === "filtering") {
continue;
} else {
continue outer;
}
const localeRe = new RegExp(`^${languageCodeRe}${scriptCodeRe}?${regionCodeRe}?${variantCodeRe}?$`, "i");
class Locale {
/**
* Parses a locale id using the localeRe into an array with four elements.
*
* If the second argument `range` is set to true, it places range `*` char
* in place of any missing piece.
*
* It also allows skipping the script section of the id, so `en-US` is
* properly parsed as `en-*-US-*`.
*/
constructor(locale) {
const result = localeRe.exec(locale.replace(/_/g, "-"));
if (!result) {
this.isWellFormed = false;
return;
}
let [, language, script, region, variant] = result;
if (language) {
this.language = language.toLowerCase();
}
if (script) {
this.script = script[0].toUpperCase() + script.slice(1);
}
if (region) {
this.region = region.toUpperCase();
}
this.variant = variant;
this.isWellFormed = true;
}
}
// 2) Attempt to match against the available range
// This turns `en` into `en-*-*-*` and `en-US` into `en-*-US-*`
// Example: ['en-US'] * ['en'] = ['en']
for (const [key, availableLocale] of availableLocalesMap.entries()) {
if (availableLocale.matches(requestedLocale, true, false)) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (strategy === "lookup") {
return Array.from(supportedLocales);
} else if (strategy === "filtering") {
continue;
} else {
continue outer;
}
isEqual(other) {
return this.language === other.language
&& this.script === other.script
&& this.region === other.region
&& this.variant === other.variant;
}
}
// 3) Attempt to retrieve a maximal version of the requested locale ID
// If data is available, it'll expand `en` into `en-Latn-US` and
// `zh` into `zh-Hans-CN`.
// Example: ['en'] * ['en-GB', 'en-US'] = ['en-US']
if (requestedLocale.addLikelySubtags()) {
for (const [key, availableLocale] of availableLocalesMap.entries()) {
if (availableLocale.matches(requestedLocale, true, false)) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (strategy === "lookup") {
return Array.from(supportedLocales);
} else if (strategy === "filtering") {
continue;
} else {
continue outer;
matches(other, thisRange = false, otherRange = false) {
return (this.language === other.language
|| thisRange && this.language === undefined
|| otherRange && other.language === undefined)
&& (this.script === other.script
|| thisRange && this.script === undefined
|| otherRange && other.script === undefined)
&& (this.region === other.region
|| thisRange && this.region === undefined
|| otherRange && other.region === undefined)
&& (this.variant === other.variant
|| thisRange && this.variant === undefined
|| otherRange && other.variant === undefined);
}
toString() {
return [this.language, this.script, this.region, this.variant]
.filter(part => part !== undefined)
.join("-");
}
clearVariants() {
this.variant = undefined;
}
clearRegion() {
this.region = undefined;
}
addLikelySubtags() {
const newLocale = getLikelySubtagsMin(this.toString().toLowerCase());
if (newLocale) {
this.language = newLocale.language;
this.script = newLocale.script;
this.region = newLocale.region;
this.variant = newLocale.variant;
return true;
}
}
return false;
}
}
}
// 4) Attempt to look up for a different variant for the same locale ID
// Example: ['en-US-mac'] * ['en-US-win'] = ['en-US-win']
requestedLocale.clearVariants();
for (const [key, availableLocale] of availableLocalesMap.entries()) {
if (availableLocale.matches(requestedLocale, true, true)) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (strategy === "lookup") {
return Array.from(supportedLocales);
} else if (strategy === "filtering") {
continue;
} else {
continue outer;
}
/* eslint no-magic-numbers: 0 */
/**
* Negotiates the languages between the list of requested locales against
* a list of available locales.
*
* The algorithm is based on the BCP4647 3.3.2 Extended Filtering algorithm,
* with several modifications:
*
* 1) available locales are treated as ranges
*
* This change allows us to match a more specific request against
* more generic available locale.
*
* For example, if the available locale list provides locale `en`,
* and the requested locale is `en-US`, we treat the available locale as
* a locale that matches all possible english requests.
*
* This means that we expect available locale ID to be as precize as
* the matches they want to cover.
*
* For example, if there is only `sr` available, it's ok to list
* it in available locales. But once the available locales has both,
* Cyrl and Latn variants, the locale IDs should be `sr-Cyrl` and `sr-Latn`
* to avoid any `sr-*` request to match against whole `sr` range.
*
* What it does ([requested] * [available] = [supported]):
*
* ['en-US'] * ['en'] = ['en']
*
* 2) likely subtags from LDML 4.3 Likely Subtags has been added
*
* The most obvious likely subtag that can be computed is a duplication
* of the language field onto region field (`fr` => `fr-FR`).
*
* On top of that, likely subtags may use a list of mappings, that
* allow the algorithm to handle non-obvious matches.
* For example, making sure that we match `en` to `en-US` or `sr` to
* `sr-Cyrl`, while `sr-RU` to `sr-Latn-RU`.
*
* This list can be taken directly from CLDR Supplemental Data.
*
* What it does ([requested] * [available] = [supported]):
*
* ['fr'] * ['fr-FR'] = ['fr-FR']
* ['en'] * ['en-US'] = ['en-US']
* ['sr'] * ['sr-Latn', 'sr-Cyrl'] = ['sr-Cyrl']
*
* 3) variant/region range check has been added
*
* Lastly, the last form of check is against the requested locale ID
* but with the variant/region field replaced with a `*` range.
*
* The rationale here laid out in LDML 4.4 Language Matching:
* "(...) normally the fall-off between the user's languages is
* substantially greated than regional variants."
*
* In other words, if we can't match for the given region, maybe
* we can match for the same language/script but other region, and
* it will in most cases be preferred over falling back on the next
* language.
*
* What it does ([requested] * [available] = [supported]):
*
* ['en-AU'] * ['en-US'] = ['en-US']
* ['sr-RU'] * ['sr-Latn-RO'] = ['sr-Latn-RO'] // sr-RU -> sr-Latn-RU
*
* It works similarly to getParentLocales algo, except that we stop
* after matching against variant/region ranges and don't try to match
* ignoring script ranges. That means that `sr-Cyrl` will never match
* against `sr-Latn`.
*/
function filterMatches(requestedLocales, availableLocales, strategy) {
const supportedLocales = new Set();
const availableLocalesMap = new Map();
for (let locale of availableLocales) {
let newLocale = new Locale(locale);
if (newLocale.isWellFormed) {
availableLocalesMap.set(locale, new Locale(locale));
}
}
}
// 5) Attempt to match against the likely subtag without region
// In the example below, addLikelySubtags will turn
// `zh-Hant` into `zh-Hant-TW` giving `zh-TW` priority match
// over `zh-CN`.
//
// Example: ['zh-Hant-HK'] * ['zh-TW', 'zh-CN'] = ['zh-TW']
requestedLocale.clearRegion();
if (requestedLocale.addLikelySubtags()) {
for (const [key, availableLocale] of availableLocalesMap.entries()) {
if (availableLocale.matches(requestedLocale, true, false)) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (strategy === "lookup") {
return Array.from(supportedLocales);
} else if (strategy === "filtering") {
continue;
} else {
continue outer;
outer: for (const reqLocStr of requestedLocales) {
const reqLocStrLC = reqLocStr.toLowerCase();
const requestedLocale = new Locale(reqLocStrLC);
if (requestedLocale.language === undefined) {
continue;
}
}
// 1) Attempt to make an exact match
// Example: `en-US` === `en-US`
for (const key of availableLocalesMap.keys()) {
if (reqLocStrLC === key.toLowerCase()) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (strategy === "lookup") {
return Array.from(supportedLocales);
}
else if (strategy === "filtering") {
continue;
}
else {
continue outer;
}
}
}
// 2) Attempt to match against the available range
// This turns `en` into `en-*-*-*` and `en-US` into `en-*-US-*`
// Example: ['en-US'] * ['en'] = ['en']
for (const [key, availableLocale] of availableLocalesMap.entries()) {
if (availableLocale.matches(requestedLocale, true, false)) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (strategy === "lookup") {
return Array.from(supportedLocales);
}
else if (strategy === "filtering") {
continue;
}
else {
continue outer;
}
}
}
// 3) Attempt to retrieve a maximal version of the requested locale ID
// If data is available, it'll expand `en` into `en-Latn-US` and
// `zh` into `zh-Hans-CN`.
// Example: ['en'] * ['en-GB', 'en-US'] = ['en-US']
if (requestedLocale.addLikelySubtags()) {
for (const [key, availableLocale] of availableLocalesMap.entries()) {
if (availableLocale.matches(requestedLocale, true, false)) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (strategy === "lookup") {
return Array.from(supportedLocales);
}
else if (strategy === "filtering") {
continue;
}
else {
continue outer;
}
}
}
}
// 4) Attempt to look up for a different variant for the same locale ID
// Example: ['en-US-mac'] * ['en-US-win'] = ['en-US-win']
requestedLocale.clearVariants();
for (const [key, availableLocale] of availableLocalesMap.entries()) {
if (availableLocale.matches(requestedLocale, true, true)) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (strategy === "lookup") {
return Array.from(supportedLocales);
}
else if (strategy === "filtering") {
continue;
}
else {
continue outer;
}
}
}
// 5) Attempt to match against the likely subtag without region
// In the example below, addLikelySubtags will turn
// `zh-Hant` into `zh-Hant-TW` giving `zh-TW` priority match
// over `zh-CN`.
//
// Example: ['zh-Hant-HK'] * ['zh-TW', 'zh-CN'] = ['zh-TW']
requestedLocale.clearRegion();
if (requestedLocale.addLikelySubtags()) {
for (const [key, availableLocale] of availableLocalesMap.entries()) {
if (availableLocale.matches(requestedLocale, true, false)) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (strategy === "lookup") {
return Array.from(supportedLocales);
}
else if (strategy === "filtering") {
continue;
}
else {
continue outer;
}
}
}
}
// 6) Attempt to look up for a different region for the same locale ID
// Example: ['en-US'] * ['en-AU'] = ['en-AU']
requestedLocale.clearRegion();
for (const [key, availableLocale] of availableLocalesMap.entries()) {
if (availableLocale.matches(requestedLocale, true, true)) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (strategy === "lookup") {
return Array.from(supportedLocales);
}
else if (strategy === "filtering") {
continue;
}
else {
continue outer;
}
}
}
}
}
return Array.from(supportedLocales);
}
// 6) Attempt to look up for a different region for the same locale ID
// Example: ['en-US'] * ['en-AU'] = ['en-AU']
requestedLocale.clearRegion();
for (const [key, availableLocale] of availableLocalesMap.entries()) {
if (availableLocale.matches(requestedLocale, true, true)) {
supportedLocales.add(key);
availableLocalesMap.delete(key);
if (strategy === "lookup") {
return Array.from(supportedLocales);
} else if (strategy === "filtering") {
continue;
} else {
continue outer;
}
/**
* Negotiates the languages between the list of requested locales against
* a list of available locales.
*
* It accepts three arguments:
*
* requestedLocales:
* an Array of strings with BCP47 locale IDs sorted
* according to user preferences.
*
* availableLocales:
* an Array of strings with BCP47 locale IDs of locale for which
* resources are available. Unsorted.
*
* options:
* An object with the following, optional keys:
*
* strategy: 'filtering' (default) | 'matching' | 'lookup'
*
* defaultLocale:
* a string with BCP47 locale ID to be used
* as a last resort locale.
*
*
* It returns an Array of strings with BCP47 locale IDs sorted according to the
* user preferences.
*
* The exact list will be selected differently depending on the strategy:
*
* 'filtering': (default)
* In the filtering strategy, the algorithm will attempt to match
* as many keys in the available locales in order of the requested locales.
*
* 'matching':
* In the matching strategy, the algorithm will attempt to find the
* best possible match for each element of the requestedLocales list.
*
* 'lookup':
* In the lookup strategy, the algorithm will attempt to find a single
* best available locale based on the requested locales list.
*
* This strategy requires defaultLocale option to be set.
*/
function negotiateLanguages(requestedLocales, availableLocales, { strategy = "filtering", defaultLocale, } = {}) {
const supportedLocales = filterMatches(Array.from(Object(requestedLocales)).map(String), Array.from(Object(availableLocales)).map(String), strategy);
if (strategy === "lookup") {
if (defaultLocale === undefined) {
throw new Error("defaultLocale cannot be undefined for strategy `lookup`");
}
if (supportedLocales.length === 0) {
supportedLocales.push(defaultLocale);
}
}
}
else if (defaultLocale && !supportedLocales.includes(defaultLocale)) {
supportedLocales.push(defaultLocale);
}
return supportedLocales;
}
return Array.from(supportedLocales);
}
function GetOption(options, property, type, values, fallback) {
let value = options[property];
if (value !== undefined) {
if (type === "boolean") {
value = new Boolean(value);
} else if (type === "string") {
value = String(value);
}
if (values !== undefined && values.indexOf(value) === -1) {
throw new Error("Invalid option value");
}
return value;
function acceptedLanguages(str = "") {
if (typeof str !== "string") {
throw new TypeError("Argument must be a string");
}
const tokens = str.split(",").map(t => t.trim());
return tokens.filter(t => t !== "").map(t => t.split(";")[0]);
}
return fallback;
}
exports.acceptedLanguages = acceptedLanguages;
exports.negotiateLanguages = negotiateLanguages;
/**
* Negotiates the languages between the list of requested locales against
* a list of available locales.
*
* It accepts three arguments:
*
* requestedLocales:
* an Array of strings with BCP47 locale IDs sorted
* according to user preferences.
*
* availableLocales:
* an Array of strings with BCP47 locale IDs of locale for which
* resources are available. Unsorted.
*
* options:
* An object with the following, optional keys:
*
* strategy: 'filtering' (default) | 'matching' | 'lookup'
*
* defaultLocale:
* a string with BCP47 locale ID to be used
* as a last resort locale.
*
*
* It returns an Array of strings with BCP47 locale IDs sorted according to the
* user preferences.
*
* The exact list will be selected differently depending on the strategy:
*
* 'filtering': (default)
* In the filtering strategy, the algorithm will attempt to match
* as many keys in the available locales in order of the requested locales.
*
* 'matching':
* In the matching strategy, the algorithm will attempt to find the
* best possible match for each element of the requestedLocales list.
*
* 'lookup':
* In the lookup strategy, the algorithm will attempt to find a single
* best available locale based on the requested locales list.
*
* This strategy requires defaultLocale option to be set.
*/
function negotiateLanguages(
requestedLocales,
availableLocales,
options = {}
) {
Object.defineProperty(exports, '__esModule', { value: true });
const defaultLocale = GetOption(options, "defaultLocale", "string");
const strategy = GetOption(options, "strategy", "string",
["filtering", "matching", "lookup"], "filtering");
if (strategy === "lookup" && !defaultLocale) {
throw new Error("defaultLocale cannot be undefined for strategy `lookup`");
}
const resolvedReqLoc = Array.from(Object(requestedLocales)).map(loc => {
return String(loc);
});
const resolvedAvailLoc = Array.from(Object(availableLocales)).map(loc => {
return String(loc);
});
const supportedLocales = filterMatches(
resolvedReqLoc,
resolvedAvailLoc, strategy
);
if (strategy === "lookup") {
if (supportedLocales.length === 0) {
supportedLocales.push(defaultLocale);
}
} else if (defaultLocale && !supportedLocales.includes(defaultLocale)) {
supportedLocales.push(defaultLocale);
}
return supportedLocales;
}
function acceptedLanguages(string = "") {
if (typeof string !== "string") {
throw new TypeError("Argument must be a string");
}
const tokens = string.split(",").map(t => t.trim());
return tokens.filter(t => t !== "").map(t => t.split(";")[0]);
}
/*
* @module fluent-langneg
* @overview
*
* `fluent-langneg` provides language negotiation API that fits into
* Project Fluent localization composition and fallbacking strategy.
*
*/
exports.acceptedLanguages = acceptedLanguages;
exports.negotiateLanguages = negotiateLanguages;
Object.defineProperty(exports, '__esModule', { value: true });
}));
})));
{
"name": "@fluent/langneg",
"description": "Language Negotiation API for Fluent",
"version": "0.3.0",
"version": "0.4.0",
"homepage": "https://projectfluent.org",

@@ -18,7 +18,6 @@ "author": "Mozilla <l10n-drivers@mozilla.org>",

],
"directories": {
"lib": "./src"
},
"type": "commonjs",
"main": "./index.js",
"module": "./src/index.js",
"module": "./esm/index.js",
"types": "./esm/index.d.ts",
"repository": {

@@ -38,4 +37,4 @@ "type": "git",

"engines": {
"node": ">=8.9.0"
"node": ">=10.0.0"
}
}

@@ -89,3 +89,3 @@ # @fluent/langneg

An example of that scenario is when the user requests `en` locale, and
the application supportes `en-GB` and `en-US`.
the application supports `en-GB` and `en-US`.

@@ -92,0 +92,0 @@ ## Learn more

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc