resolve-accept-language 1.1.18 → 1.1.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +108 -108
- package/lib/locale-list.d.ts +19 -19
- package/lib/locale-list.js +34 -34
- package/lib/locale.d.ts +38 -38
- package/lib/locale.js +56 -56
- package/lib/lookup-list.d.ts +76 -76
- package/lib/lookup-list.js +158 -158
- package/lib/resolve-accept-language.d.ts +76 -76
- package/lib/resolve-accept-language.js +136 -136
- package/package.json +62 -62
package/lib/lookup-list.d.ts
CHANGED
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
/** Lookup list used to match the preferred locale based on the value of an `Accept-Language` HTTP header. */
|
|
2
|
-
export default class LookupList {
|
|
3
|
-
/** The list of locales used to get the match during the lookup. */
|
|
4
|
-
private localeList;
|
|
5
|
-
/** Data object where the properties are quality (in string format) and their values a set containing locale
|
|
6
|
-
* identifiers using the `language`-`country` format and ISO 639-1 alpha-2 language code. */
|
|
7
|
-
private localesAndLanguagesByQuality;
|
|
8
|
-
/** Data object where the properties are quality (in string format) and their value a set of ISO 639-1 alpha-2
|
|
9
|
-
* language code. */
|
|
10
|
-
private relatedLocaleLanguagesByQuality;
|
|
11
|
-
/**
|
|
12
|
-
* Create a new `LookupList` object.
|
|
13
|
-
*
|
|
14
|
-
* @param acceptLanguageHeader - The value of an HTTP request `Accept-Language` header (also known as a "language priority list").
|
|
15
|
-
* @param locales - An array of locale identifiers. The order will be used for matching where the first identifier will be more
|
|
16
|
-
* likely to be matched than the last identifier.
|
|
17
|
-
*/
|
|
18
|
-
constructor(acceptLanguageHeader: string, locales: string[]);
|
|
19
|
-
/**
|
|
20
|
-
* Get the top (highest-ranked) locale by language.
|
|
21
|
-
*
|
|
22
|
-
* @param languageCode - An ISO 639-1 alpha-2 language code.
|
|
23
|
-
*
|
|
24
|
-
* @returns The top locale with the specified language.
|
|
25
|
-
*/
|
|
26
|
-
getTopByLanguage(languageCode: string): string | undefined;
|
|
27
|
-
/**
|
|
28
|
-
* Get the top (highest-ranked) locale or language.
|
|
29
|
-
*
|
|
30
|
-
* @returns The top match, which can either be a locale or a language.
|
|
31
|
-
*/
|
|
32
|
-
getTopLocaleOrLanguage(): string | undefined;
|
|
33
|
-
/**
|
|
34
|
-
* Get the top (highest-ranked) related locale.
|
|
35
|
-
*
|
|
36
|
-
* @returns The top related locale.
|
|
37
|
-
*/
|
|
38
|
-
getTopRelatedLocale(): string | undefined;
|
|
39
|
-
/**
|
|
40
|
-
* Add a language in the data object matching its quality.
|
|
41
|
-
*
|
|
42
|
-
* @param quality - The HTTP header's quality factor associated with a language.
|
|
43
|
-
* @param languageCode - An ISO 639-1 alpha-2 language code.
|
|
44
|
-
*/
|
|
45
|
-
private addLanguage;
|
|
46
|
-
/**
|
|
47
|
-
* Add a locale in the data object matching its quality.
|
|
48
|
-
*
|
|
49
|
-
* @param quality - The HTTP header's quality factor associated with a locale.
|
|
50
|
-
* @param identifier - A locale identifier using the BCP 47 `language`-`country` case-normalized format.
|
|
51
|
-
*/
|
|
52
|
-
private addLocale;
|
|
53
|
-
/**
|
|
54
|
-
* Add a related locale's language in the data object matching its quality.
|
|
55
|
-
*
|
|
56
|
-
* @param quality - The HTTP header's quality factor associated with a related locale's language.
|
|
57
|
-
* @param languageCode - An ISO 639-1 alpha-2 language code.
|
|
58
|
-
*/
|
|
59
|
-
private addRelatedLocaleLanguage;
|
|
60
|
-
/**
|
|
61
|
-
* Get a directive object from a directive string.
|
|
62
|
-
*
|
|
63
|
-
* @param directiveString - The string representing a directive, extracted from the HTTP header.
|
|
64
|
-
*
|
|
65
|
-
* @returns A `Directive` object or `undefined` if the string's format is invalid.
|
|
66
|
-
*/
|
|
67
|
-
private getDirective;
|
|
68
|
-
/**
|
|
69
|
-
* Get the top (highest-ranked) entry from a dataset object entries.
|
|
70
|
-
*
|
|
71
|
-
* @param dataObjectEntries - The object entries of a dataset object.
|
|
72
|
-
*
|
|
73
|
-
* @returns The top entry from a dataset object entries.
|
|
74
|
-
*/
|
|
75
|
-
private getTop;
|
|
76
|
-
}
|
|
1
|
+
/** Lookup list used to match the preferred locale based on the value of an `Accept-Language` HTTP header. */
|
|
2
|
+
export default class LookupList {
|
|
3
|
+
/** The list of locales used to get the match during the lookup. */
|
|
4
|
+
private localeList;
|
|
5
|
+
/** Data object where the properties are quality (in string format) and their values a set containing locale
|
|
6
|
+
* identifiers using the `language`-`country` format and ISO 639-1 alpha-2 language code. */
|
|
7
|
+
private localesAndLanguagesByQuality;
|
|
8
|
+
/** Data object where the properties are quality (in string format) and their value a set of ISO 639-1 alpha-2
|
|
9
|
+
* language code. */
|
|
10
|
+
private relatedLocaleLanguagesByQuality;
|
|
11
|
+
/**
|
|
12
|
+
* Create a new `LookupList` object.
|
|
13
|
+
*
|
|
14
|
+
* @param acceptLanguageHeader - The value of an HTTP request `Accept-Language` header (also known as a "language priority list").
|
|
15
|
+
* @param locales - An array of locale identifiers. The order will be used for matching where the first identifier will be more
|
|
16
|
+
* likely to be matched than the last identifier.
|
|
17
|
+
*/
|
|
18
|
+
constructor(acceptLanguageHeader: string, locales: string[]);
|
|
19
|
+
/**
|
|
20
|
+
* Get the top (highest-ranked) locale by language.
|
|
21
|
+
*
|
|
22
|
+
* @param languageCode - An ISO 639-1 alpha-2 language code.
|
|
23
|
+
*
|
|
24
|
+
* @returns The top locale with the specified language.
|
|
25
|
+
*/
|
|
26
|
+
getTopByLanguage(languageCode: string): string | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Get the top (highest-ranked) locale or language.
|
|
29
|
+
*
|
|
30
|
+
* @returns The top match, which can either be a locale or a language.
|
|
31
|
+
*/
|
|
32
|
+
getTopLocaleOrLanguage(): string | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Get the top (highest-ranked) related locale.
|
|
35
|
+
*
|
|
36
|
+
* @returns The top related locale.
|
|
37
|
+
*/
|
|
38
|
+
getTopRelatedLocale(): string | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Add a language in the data object matching its quality.
|
|
41
|
+
*
|
|
42
|
+
* @param quality - The HTTP header's quality factor associated with a language.
|
|
43
|
+
* @param languageCode - An ISO 639-1 alpha-2 language code.
|
|
44
|
+
*/
|
|
45
|
+
private addLanguage;
|
|
46
|
+
/**
|
|
47
|
+
* Add a locale in the data object matching its quality.
|
|
48
|
+
*
|
|
49
|
+
* @param quality - The HTTP header's quality factor associated with a locale.
|
|
50
|
+
* @param identifier - A locale identifier using the BCP 47 `language`-`country` case-normalized format.
|
|
51
|
+
*/
|
|
52
|
+
private addLocale;
|
|
53
|
+
/**
|
|
54
|
+
* Add a related locale's language in the data object matching its quality.
|
|
55
|
+
*
|
|
56
|
+
* @param quality - The HTTP header's quality factor associated with a related locale's language.
|
|
57
|
+
* @param languageCode - An ISO 639-1 alpha-2 language code.
|
|
58
|
+
*/
|
|
59
|
+
private addRelatedLocaleLanguage;
|
|
60
|
+
/**
|
|
61
|
+
* Get a directive object from a directive string.
|
|
62
|
+
*
|
|
63
|
+
* @param directiveString - The string representing a directive, extracted from the HTTP header.
|
|
64
|
+
*
|
|
65
|
+
* @returns A `Directive` object or `undefined` if the string's format is invalid.
|
|
66
|
+
*/
|
|
67
|
+
private getDirective;
|
|
68
|
+
/**
|
|
69
|
+
* Get the top (highest-ranked) entry from a dataset object entries.
|
|
70
|
+
*
|
|
71
|
+
* @param dataObjectEntries - The object entries of a dataset object.
|
|
72
|
+
*
|
|
73
|
+
* @returns The top entry from a dataset object entries.
|
|
74
|
+
*/
|
|
75
|
+
private getTop;
|
|
76
|
+
}
|
package/lib/lookup-list.js
CHANGED
|
@@ -1,158 +1,158 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
var locale_list_1 = require("./locale-list");
|
|
4
|
-
/** Lookup list used to match the preferred locale based on the value of an `Accept-Language` HTTP header. */
|
|
5
|
-
var LookupList = /** @class */ (function () {
|
|
6
|
-
/**
|
|
7
|
-
* Create a new `LookupList` object.
|
|
8
|
-
*
|
|
9
|
-
* @param acceptLanguageHeader - The value of an HTTP request `Accept-Language` header (also known as a "language priority list").
|
|
10
|
-
* @param locales - An array of locale identifiers. The order will be used for matching where the first identifier will be more
|
|
11
|
-
* likely to be matched than the last identifier.
|
|
12
|
-
*/
|
|
13
|
-
function LookupList(acceptLanguageHeader, locales) {
|
|
14
|
-
/** Data object where the properties are quality (in string format) and their values a set containing locale
|
|
15
|
-
* identifiers using the `language`-`country` format and ISO 639-1 alpha-2 language code. */
|
|
16
|
-
this.localesAndLanguagesByQuality = {};
|
|
17
|
-
/** Data object where the properties are quality (in string format) and their value a set of ISO 639-1 alpha-2
|
|
18
|
-
* language code. */
|
|
19
|
-
this.relatedLocaleLanguagesByQuality = {};
|
|
20
|
-
this.localeList = new locale_list_1.default(locales);
|
|
21
|
-
var directiveStrings = acceptLanguageHeader
|
|
22
|
-
.split(',')
|
|
23
|
-
.map(function (directiveString) { return directiveString.trim(); });
|
|
24
|
-
for (var _i = 0, directiveStrings_1 = directiveStrings; _i < directiveStrings_1.length; _i++) {
|
|
25
|
-
var directiveString = directiveStrings_1[_i];
|
|
26
|
-
var directive = this.getDirective(directiveString);
|
|
27
|
-
if (directive === undefined)
|
|
28
|
-
continue; // No match for this directive.
|
|
29
|
-
var locale = directive.locale, languageCode = directive.languageCode, quality = directive.quality;
|
|
30
|
-
// If the language is not supported, skip to the next match.
|
|
31
|
-
if (!this.localeList.languages.has(languageCode)) {
|
|
32
|
-
continue;
|
|
33
|
-
}
|
|
34
|
-
// If there is no country code (while the language is supported), add the language preference.
|
|
35
|
-
if (!locale) {
|
|
36
|
-
this.addLanguage(quality, languageCode);
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
// If the locale is not supported, but the locale's language is, add to locale language preference.
|
|
40
|
-
if (!this.localeList.locales.has(locale) && this.localeList.languages.has(languageCode)) {
|
|
41
|
-
this.addRelatedLocaleLanguage(quality, languageCode);
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
|
-
// If the locale is supported, add the locale preference.
|
|
45
|
-
this.addLocale(quality, locale);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Get the top (highest-ranked) locale by language.
|
|
50
|
-
*
|
|
51
|
-
* @param languageCode - An ISO 639-1 alpha-2 language code.
|
|
52
|
-
*
|
|
53
|
-
* @returns The top locale with the specified language.
|
|
54
|
-
*/
|
|
55
|
-
LookupList.prototype.getTopByLanguage = function (languageCode) {
|
|
56
|
-
var _a;
|
|
57
|
-
return (_a = this.localeList.objects.find(function (locale) { return locale.languageCode === languageCode; })) === null || _a === void 0 ? void 0 : _a.identifier;
|
|
58
|
-
};
|
|
59
|
-
/**
|
|
60
|
-
* Get the top (highest-ranked) locale or language.
|
|
61
|
-
*
|
|
62
|
-
* @returns The top match, which can either be a locale or a language.
|
|
63
|
-
*/
|
|
64
|
-
LookupList.prototype.getTopLocaleOrLanguage = function () {
|
|
65
|
-
var localesAndLanguagesByQuality = Object.entries(this.localesAndLanguagesByQuality);
|
|
66
|
-
if (localesAndLanguagesByQuality.length === 0) {
|
|
67
|
-
return undefined;
|
|
68
|
-
}
|
|
69
|
-
return this.getTop(localesAndLanguagesByQuality);
|
|
70
|
-
};
|
|
71
|
-
/**
|
|
72
|
-
* Get the top (highest-ranked) related locale.
|
|
73
|
-
*
|
|
74
|
-
* @returns The top related locale.
|
|
75
|
-
*/
|
|
76
|
-
LookupList.prototype.getTopRelatedLocale = function () {
|
|
77
|
-
var relatedLocaleLanguagesByQuality = Object.entries(this.relatedLocaleLanguagesByQuality);
|
|
78
|
-
if (relatedLocaleLanguagesByQuality.length === 0) {
|
|
79
|
-
return undefined;
|
|
80
|
-
}
|
|
81
|
-
var topRelatedLocaleLanguage = this.getTop(relatedLocaleLanguagesByQuality);
|
|
82
|
-
return this.getTopByLanguage(topRelatedLocaleLanguage);
|
|
83
|
-
};
|
|
84
|
-
/**
|
|
85
|
-
* Add a language in the data object matching its quality.
|
|
86
|
-
*
|
|
87
|
-
* @param quality - The HTTP header's quality factor associated with a language.
|
|
88
|
-
* @param languageCode - An ISO 639-1 alpha-2 language code.
|
|
89
|
-
*/
|
|
90
|
-
LookupList.prototype.addLanguage = function (quality, languageCode) {
|
|
91
|
-
if (!this.localesAndLanguagesByQuality[quality]) {
|
|
92
|
-
this.localesAndLanguagesByQuality[quality] = new Set();
|
|
93
|
-
}
|
|
94
|
-
this.localesAndLanguagesByQuality[quality].add(languageCode);
|
|
95
|
-
};
|
|
96
|
-
/**
|
|
97
|
-
* Add a locale in the data object matching its quality.
|
|
98
|
-
*
|
|
99
|
-
* @param quality - The HTTP header's quality factor associated with a locale.
|
|
100
|
-
* @param identifier - A locale identifier using the BCP 47 `language`-`country` case-normalized format.
|
|
101
|
-
*/
|
|
102
|
-
LookupList.prototype.addLocale = function (quality, identifier) {
|
|
103
|
-
if (!this.localesAndLanguagesByQuality[quality]) {
|
|
104
|
-
this.localesAndLanguagesByQuality[quality] = new Set();
|
|
105
|
-
}
|
|
106
|
-
this.localesAndLanguagesByQuality[quality].add(identifier);
|
|
107
|
-
};
|
|
108
|
-
/**
|
|
109
|
-
* Add a related locale's language in the data object matching its quality.
|
|
110
|
-
*
|
|
111
|
-
* @param quality - The HTTP header's quality factor associated with a related locale's language.
|
|
112
|
-
* @param languageCode - An ISO 639-1 alpha-2 language code.
|
|
113
|
-
*/
|
|
114
|
-
LookupList.prototype.addRelatedLocaleLanguage = function (quality, languageCode) {
|
|
115
|
-
if (!this.relatedLocaleLanguagesByQuality[quality]) {
|
|
116
|
-
this.relatedLocaleLanguagesByQuality[quality] = new Set();
|
|
117
|
-
}
|
|
118
|
-
this.relatedLocaleLanguagesByQuality[quality].add(languageCode);
|
|
119
|
-
};
|
|
120
|
-
/**
|
|
121
|
-
* Get a directive object from a directive string.
|
|
122
|
-
*
|
|
123
|
-
* @param directiveString - The string representing a directive, extracted from the HTTP header.
|
|
124
|
-
*
|
|
125
|
-
* @returns A `Directive` object or `undefined` if the string's format is invalid.
|
|
126
|
-
*/
|
|
127
|
-
LookupList.prototype.getDirective = function (directiveString) {
|
|
128
|
-
/**
|
|
129
|
-
* The regular expression is excluding certain directives due to the inability to configure those options in modern
|
|
130
|
-
* browsers today (also those options seem unpractical):
|
|
131
|
-
*
|
|
132
|
-
* - The wildcard character "*", as per RFC 2616 (section 14.4), should match any unmatched language tag.
|
|
133
|
-
* - Language tags that starts with a wildcard (e.g. "*-CA") should match the first supported locale of a country.
|
|
134
|
-
* - A quality value equivalent to "0", as per RFC 2616 (section 3.9), should be considered as "not acceptable".
|
|
135
|
-
*/
|
|
136
|
-
var directiveMatch = directiveString.match(/^((?<matchedLanguageCode>([a-z]{2}))(-(?<matchedCountryCode>[a-z]{2}))?)(;q=(?<matchedQuality>1|0.(\d*[1-9]\d*){1,3}))?$/i);
|
|
137
|
-
if (!(directiveMatch === null || directiveMatch === void 0 ? void 0 : directiveMatch.groups))
|
|
138
|
-
return undefined; // No regular expression match.
|
|
139
|
-
var _a = directiveMatch.groups, matchedLanguageCode = _a.matchedLanguageCode, matchedCountryCode = _a.matchedCountryCode, matchedQuality = _a.matchedQuality;
|
|
140
|
-
var languageCode = matchedLanguageCode.toLowerCase();
|
|
141
|
-
var countryCode = matchedCountryCode ? matchedCountryCode.toUpperCase() : undefined;
|
|
142
|
-
var quality = matchedQuality === undefined ? '1' : Number.parseFloat(matchedQuality).toString(); // Remove trailing zeros.
|
|
143
|
-
var locale = countryCode ? "".concat(languageCode, "-").concat(countryCode) : undefined;
|
|
144
|
-
return { languageCode: languageCode, locale: locale, quality: quality };
|
|
145
|
-
};
|
|
146
|
-
/**
|
|
147
|
-
* Get the top (highest-ranked) entry from a dataset object entries.
|
|
148
|
-
*
|
|
149
|
-
* @param dataObjectEntries - The object entries of a dataset object.
|
|
150
|
-
*
|
|
151
|
-
* @returns The top entry from a dataset object entries.
|
|
152
|
-
*/
|
|
153
|
-
LookupList.prototype.getTop = function (dataObjectEntries) {
|
|
154
|
-
return dataObjectEntries.sort().reverse()[0][1].values().next().value;
|
|
155
|
-
};
|
|
156
|
-
return LookupList;
|
|
157
|
-
}());
|
|
158
|
-
exports.default = LookupList;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var locale_list_1 = require("./locale-list");
|
|
4
|
+
/** Lookup list used to match the preferred locale based on the value of an `Accept-Language` HTTP header. */
|
|
5
|
+
var LookupList = /** @class */ (function () {
|
|
6
|
+
/**
|
|
7
|
+
* Create a new `LookupList` object.
|
|
8
|
+
*
|
|
9
|
+
* @param acceptLanguageHeader - The value of an HTTP request `Accept-Language` header (also known as a "language priority list").
|
|
10
|
+
* @param locales - An array of locale identifiers. The order will be used for matching where the first identifier will be more
|
|
11
|
+
* likely to be matched than the last identifier.
|
|
12
|
+
*/
|
|
13
|
+
function LookupList(acceptLanguageHeader, locales) {
|
|
14
|
+
/** Data object where the properties are quality (in string format) and their values a set containing locale
|
|
15
|
+
* identifiers using the `language`-`country` format and ISO 639-1 alpha-2 language code. */
|
|
16
|
+
this.localesAndLanguagesByQuality = {};
|
|
17
|
+
/** Data object where the properties are quality (in string format) and their value a set of ISO 639-1 alpha-2
|
|
18
|
+
* language code. */
|
|
19
|
+
this.relatedLocaleLanguagesByQuality = {};
|
|
20
|
+
this.localeList = new locale_list_1.default(locales);
|
|
21
|
+
var directiveStrings = acceptLanguageHeader
|
|
22
|
+
.split(',')
|
|
23
|
+
.map(function (directiveString) { return directiveString.trim(); });
|
|
24
|
+
for (var _i = 0, directiveStrings_1 = directiveStrings; _i < directiveStrings_1.length; _i++) {
|
|
25
|
+
var directiveString = directiveStrings_1[_i];
|
|
26
|
+
var directive = this.getDirective(directiveString);
|
|
27
|
+
if (directive === undefined)
|
|
28
|
+
continue; // No match for this directive.
|
|
29
|
+
var locale = directive.locale, languageCode = directive.languageCode, quality = directive.quality;
|
|
30
|
+
// If the language is not supported, skip to the next match.
|
|
31
|
+
if (!this.localeList.languages.has(languageCode)) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
// If there is no country code (while the language is supported), add the language preference.
|
|
35
|
+
if (!locale) {
|
|
36
|
+
this.addLanguage(quality, languageCode);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
// If the locale is not supported, but the locale's language is, add to locale language preference.
|
|
40
|
+
if (!this.localeList.locales.has(locale) && this.localeList.languages.has(languageCode)) {
|
|
41
|
+
this.addRelatedLocaleLanguage(quality, languageCode);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
// If the locale is supported, add the locale preference.
|
|
45
|
+
this.addLocale(quality, locale);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get the top (highest-ranked) locale by language.
|
|
50
|
+
*
|
|
51
|
+
* @param languageCode - An ISO 639-1 alpha-2 language code.
|
|
52
|
+
*
|
|
53
|
+
* @returns The top locale with the specified language.
|
|
54
|
+
*/
|
|
55
|
+
LookupList.prototype.getTopByLanguage = function (languageCode) {
|
|
56
|
+
var _a;
|
|
57
|
+
return (_a = this.localeList.objects.find(function (locale) { return locale.languageCode === languageCode; })) === null || _a === void 0 ? void 0 : _a.identifier;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Get the top (highest-ranked) locale or language.
|
|
61
|
+
*
|
|
62
|
+
* @returns The top match, which can either be a locale or a language.
|
|
63
|
+
*/
|
|
64
|
+
LookupList.prototype.getTopLocaleOrLanguage = function () {
|
|
65
|
+
var localesAndLanguagesByQuality = Object.entries(this.localesAndLanguagesByQuality);
|
|
66
|
+
if (localesAndLanguagesByQuality.length === 0) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
return this.getTop(localesAndLanguagesByQuality);
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Get the top (highest-ranked) related locale.
|
|
73
|
+
*
|
|
74
|
+
* @returns The top related locale.
|
|
75
|
+
*/
|
|
76
|
+
LookupList.prototype.getTopRelatedLocale = function () {
|
|
77
|
+
var relatedLocaleLanguagesByQuality = Object.entries(this.relatedLocaleLanguagesByQuality);
|
|
78
|
+
if (relatedLocaleLanguagesByQuality.length === 0) {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
var topRelatedLocaleLanguage = this.getTop(relatedLocaleLanguagesByQuality);
|
|
82
|
+
return this.getTopByLanguage(topRelatedLocaleLanguage);
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Add a language in the data object matching its quality.
|
|
86
|
+
*
|
|
87
|
+
* @param quality - The HTTP header's quality factor associated with a language.
|
|
88
|
+
* @param languageCode - An ISO 639-1 alpha-2 language code.
|
|
89
|
+
*/
|
|
90
|
+
LookupList.prototype.addLanguage = function (quality, languageCode) {
|
|
91
|
+
if (!this.localesAndLanguagesByQuality[quality]) {
|
|
92
|
+
this.localesAndLanguagesByQuality[quality] = new Set();
|
|
93
|
+
}
|
|
94
|
+
this.localesAndLanguagesByQuality[quality].add(languageCode);
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Add a locale in the data object matching its quality.
|
|
98
|
+
*
|
|
99
|
+
* @param quality - The HTTP header's quality factor associated with a locale.
|
|
100
|
+
* @param identifier - A locale identifier using the BCP 47 `language`-`country` case-normalized format.
|
|
101
|
+
*/
|
|
102
|
+
LookupList.prototype.addLocale = function (quality, identifier) {
|
|
103
|
+
if (!this.localesAndLanguagesByQuality[quality]) {
|
|
104
|
+
this.localesAndLanguagesByQuality[quality] = new Set();
|
|
105
|
+
}
|
|
106
|
+
this.localesAndLanguagesByQuality[quality].add(identifier);
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Add a related locale's language in the data object matching its quality.
|
|
110
|
+
*
|
|
111
|
+
* @param quality - The HTTP header's quality factor associated with a related locale's language.
|
|
112
|
+
* @param languageCode - An ISO 639-1 alpha-2 language code.
|
|
113
|
+
*/
|
|
114
|
+
LookupList.prototype.addRelatedLocaleLanguage = function (quality, languageCode) {
|
|
115
|
+
if (!this.relatedLocaleLanguagesByQuality[quality]) {
|
|
116
|
+
this.relatedLocaleLanguagesByQuality[quality] = new Set();
|
|
117
|
+
}
|
|
118
|
+
this.relatedLocaleLanguagesByQuality[quality].add(languageCode);
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Get a directive object from a directive string.
|
|
122
|
+
*
|
|
123
|
+
* @param directiveString - The string representing a directive, extracted from the HTTP header.
|
|
124
|
+
*
|
|
125
|
+
* @returns A `Directive` object or `undefined` if the string's format is invalid.
|
|
126
|
+
*/
|
|
127
|
+
LookupList.prototype.getDirective = function (directiveString) {
|
|
128
|
+
/**
|
|
129
|
+
* The regular expression is excluding certain directives due to the inability to configure those options in modern
|
|
130
|
+
* browsers today (also those options seem unpractical):
|
|
131
|
+
*
|
|
132
|
+
* - The wildcard character "*", as per RFC 2616 (section 14.4), should match any unmatched language tag.
|
|
133
|
+
* - Language tags that starts with a wildcard (e.g. "*-CA") should match the first supported locale of a country.
|
|
134
|
+
* - A quality value equivalent to "0", as per RFC 2616 (section 3.9), should be considered as "not acceptable".
|
|
135
|
+
*/
|
|
136
|
+
var directiveMatch = directiveString.match(/^((?<matchedLanguageCode>([a-z]{2}))(-(?<matchedCountryCode>[a-z]{2}))?)(;q=(?<matchedQuality>1|0.(\d*[1-9]\d*){1,3}))?$/i);
|
|
137
|
+
if (!(directiveMatch === null || directiveMatch === void 0 ? void 0 : directiveMatch.groups))
|
|
138
|
+
return undefined; // No regular expression match.
|
|
139
|
+
var _a = directiveMatch.groups, matchedLanguageCode = _a.matchedLanguageCode, matchedCountryCode = _a.matchedCountryCode, matchedQuality = _a.matchedQuality;
|
|
140
|
+
var languageCode = matchedLanguageCode.toLowerCase();
|
|
141
|
+
var countryCode = matchedCountryCode ? matchedCountryCode.toUpperCase() : undefined;
|
|
142
|
+
var quality = matchedQuality === undefined ? '1' : Number.parseFloat(matchedQuality).toString(); // Remove trailing zeros.
|
|
143
|
+
var locale = countryCode ? "".concat(languageCode, "-").concat(countryCode) : undefined;
|
|
144
|
+
return { languageCode: languageCode, locale: locale, quality: quality };
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* Get the top (highest-ranked) entry from a dataset object entries.
|
|
148
|
+
*
|
|
149
|
+
* @param dataObjectEntries - The object entries of a dataset object.
|
|
150
|
+
*
|
|
151
|
+
* @returns The top entry from a dataset object entries.
|
|
152
|
+
*/
|
|
153
|
+
LookupList.prototype.getTop = function (dataObjectEntries) {
|
|
154
|
+
return dataObjectEntries.sort().reverse()[0][1].values().next().value;
|
|
155
|
+
};
|
|
156
|
+
return LookupList;
|
|
157
|
+
}());
|
|
158
|
+
exports.default = LookupList;
|
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
/** Resolve the preferred locale from an HTTP `Accept-Language` header. */
|
|
2
|
-
export declare class ResolveAcceptLanguage {
|
|
3
|
-
/** The language-based match, if applicable. */
|
|
4
|
-
private languageBasedMatch;
|
|
5
|
-
/** The locale-based match, if applicable. */
|
|
6
|
-
private localeBasedMatch;
|
|
7
|
-
/** The related-locale-based match, if applicable. */
|
|
8
|
-
private relatedLocaleBasedMatch;
|
|
9
|
-
/**
|
|
10
|
-
* Create a new `ResolveAcceptLanguage` object.
|
|
11
|
-
*
|
|
12
|
-
* All locale identifiers provided as parameters must following the BCP 47 `language`-`country` (case insensitive).
|
|
13
|
-
*
|
|
14
|
-
* @param acceptLanguageHeader - The value of an HTTP request `Accept-Language` header (also known as a "language priority list").
|
|
15
|
-
* @param locales - An array of locale identifiers. The order will be used for matching where the first identifier will be more
|
|
16
|
-
* likely to be matched than the last identifier.
|
|
17
|
-
*/
|
|
18
|
-
constructor(acceptLanguageHeader: string, locales: string[]);
|
|
19
|
-
/**
|
|
20
|
-
* Is the best match language-based?
|
|
21
|
-
*
|
|
22
|
-
* @returns True if the best match language-based, otherwise false.
|
|
23
|
-
*/
|
|
24
|
-
bestMatchIsLanguageBased(): boolean;
|
|
25
|
-
/**
|
|
26
|
-
* Is the best match locale-based?
|
|
27
|
-
*
|
|
28
|
-
* @returns True if the best match locale-based, otherwise false.
|
|
29
|
-
*/
|
|
30
|
-
bestMatchIsLocaleBased(): boolean;
|
|
31
|
-
/**
|
|
32
|
-
* Is the best match related-locale-based?
|
|
33
|
-
*
|
|
34
|
-
* @returns True if the best match related-locale-based, otherwise false.
|
|
35
|
-
*/
|
|
36
|
-
bestMatchIsRelatedLocaleBased(): boolean;
|
|
37
|
-
/**
|
|
38
|
-
* Get the locale which was the best match.
|
|
39
|
-
*
|
|
40
|
-
* @returns The locale which was the best match.
|
|
41
|
-
*/
|
|
42
|
-
getBestMatch(): string | undefined;
|
|
43
|
-
/**
|
|
44
|
-
* Was a match found when resolving the preferred locale?
|
|
45
|
-
*
|
|
46
|
-
* @returns True when a match is found, otherwise false.
|
|
47
|
-
*/
|
|
48
|
-
hasMatch(): boolean;
|
|
49
|
-
/**
|
|
50
|
-
* Did the resolution of the preferred locale find no match?
|
|
51
|
-
*
|
|
52
|
-
* @returns True when there is no match, otherwise false.
|
|
53
|
-
*/
|
|
54
|
-
hasNoMatch(): boolean;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Resolve the preferred locale from an HTTP `Accept-Language` header.
|
|
58
|
-
*
|
|
59
|
-
* All locale identifiers provided as parameters must following the BCP 47 `language`-`country` (case insensitive).
|
|
60
|
-
*
|
|
61
|
-
* @param acceptLanguageHeader - The value of an HTTP request `Accept-Language` header (also known as a "language priority list").
|
|
62
|
-
* @param locales - An array of locale identifiers that must include the default locale. The order will be used for matching where
|
|
63
|
-
* the first identifier will be more likely to be matched than the last identifier.
|
|
64
|
-
* @param defaultLocale - The default locale identifier when no match is found.
|
|
65
|
-
*
|
|
66
|
-
* @returns The locale identifier which was the best match, in case-normalized format.
|
|
67
|
-
*
|
|
68
|
-
* @example
|
|
69
|
-
* // returns 'fr-CA'
|
|
70
|
-
* resolveAcceptLanguage(
|
|
71
|
-
* 'fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001',
|
|
72
|
-
* ['en-US', 'fr-CA'],
|
|
73
|
-
* 'en-US'
|
|
74
|
-
* )
|
|
75
|
-
*/
|
|
76
|
-
export default function resolveAcceptLanguage(acceptLanguageHeader: string, locales: string[], defaultLocale: string): string;
|
|
1
|
+
/** Resolve the preferred locale from an HTTP `Accept-Language` header. */
|
|
2
|
+
export declare class ResolveAcceptLanguage {
|
|
3
|
+
/** The language-based match, if applicable. */
|
|
4
|
+
private languageBasedMatch;
|
|
5
|
+
/** The locale-based match, if applicable. */
|
|
6
|
+
private localeBasedMatch;
|
|
7
|
+
/** The related-locale-based match, if applicable. */
|
|
8
|
+
private relatedLocaleBasedMatch;
|
|
9
|
+
/**
|
|
10
|
+
* Create a new `ResolveAcceptLanguage` object.
|
|
11
|
+
*
|
|
12
|
+
* All locale identifiers provided as parameters must following the BCP 47 `language`-`country` (case insensitive).
|
|
13
|
+
*
|
|
14
|
+
* @param acceptLanguageHeader - The value of an HTTP request `Accept-Language` header (also known as a "language priority list").
|
|
15
|
+
* @param locales - An array of locale identifiers. The order will be used for matching where the first identifier will be more
|
|
16
|
+
* likely to be matched than the last identifier.
|
|
17
|
+
*/
|
|
18
|
+
constructor(acceptLanguageHeader: string, locales: string[]);
|
|
19
|
+
/**
|
|
20
|
+
* Is the best match language-based?
|
|
21
|
+
*
|
|
22
|
+
* @returns True if the best match language-based, otherwise false.
|
|
23
|
+
*/
|
|
24
|
+
bestMatchIsLanguageBased(): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Is the best match locale-based?
|
|
27
|
+
*
|
|
28
|
+
* @returns True if the best match locale-based, otherwise false.
|
|
29
|
+
*/
|
|
30
|
+
bestMatchIsLocaleBased(): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Is the best match related-locale-based?
|
|
33
|
+
*
|
|
34
|
+
* @returns True if the best match related-locale-based, otherwise false.
|
|
35
|
+
*/
|
|
36
|
+
bestMatchIsRelatedLocaleBased(): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Get the locale which was the best match.
|
|
39
|
+
*
|
|
40
|
+
* @returns The locale which was the best match.
|
|
41
|
+
*/
|
|
42
|
+
getBestMatch(): string | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Was a match found when resolving the preferred locale?
|
|
45
|
+
*
|
|
46
|
+
* @returns True when a match is found, otherwise false.
|
|
47
|
+
*/
|
|
48
|
+
hasMatch(): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Did the resolution of the preferred locale find no match?
|
|
51
|
+
*
|
|
52
|
+
* @returns True when there is no match, otherwise false.
|
|
53
|
+
*/
|
|
54
|
+
hasNoMatch(): boolean;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Resolve the preferred locale from an HTTP `Accept-Language` header.
|
|
58
|
+
*
|
|
59
|
+
* All locale identifiers provided as parameters must following the BCP 47 `language`-`country` (case insensitive).
|
|
60
|
+
*
|
|
61
|
+
* @param acceptLanguageHeader - The value of an HTTP request `Accept-Language` header (also known as a "language priority list").
|
|
62
|
+
* @param locales - An array of locale identifiers that must include the default locale. The order will be used for matching where
|
|
63
|
+
* the first identifier will be more likely to be matched than the last identifier.
|
|
64
|
+
* @param defaultLocale - The default locale identifier when no match is found.
|
|
65
|
+
*
|
|
66
|
+
* @returns The locale identifier which was the best match, in case-normalized format.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* // returns 'fr-CA'
|
|
70
|
+
* resolveAcceptLanguage(
|
|
71
|
+
* 'fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001',
|
|
72
|
+
* ['en-US', 'fr-CA'],
|
|
73
|
+
* 'en-US'
|
|
74
|
+
* )
|
|
75
|
+
*/
|
|
76
|
+
export default function resolveAcceptLanguage(acceptLanguageHeader: string, locales: string[], defaultLocale: string): string;
|