resolve-accept-language 3.2.1 → 3.2.2
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/README.md +142 -142
- package/dist/cjs/directives.js +1 -1
- package/dist/cjs/index.d.ts +11 -15
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/locales.d.ts +4 -1
- package/dist/esm/directives.js +1 -1
- package/dist/esm/index.d.ts +11 -15
- package/dist/esm/index.js +1 -1
- package/dist/esm/locales.d.ts +4 -1
- package/package.json +18 -17
package/README.md
CHANGED
|
@@ -1,142 +1,142 @@
|
|
|
1
|
-
# resolve-accept-language
|
|
2
|
-
|
|
3
|
-
[](https://opensource.org/licenses/MIT)
|
|
4
|
-
[](https://www.npmjs.com/package/resolve-accept-language)
|
|
5
|
-

|
|
6
|
-

|
|
7
|
-

|
|
8
|
-
|
|
9
|
-
Resolve the best locale based on the value of an `Accept-Language` HTTP header.
|
|
10
|
-
|
|
11
|
-
## Usage
|
|
12
|
-
|
|
13
|
-
> ⚠ In March 2024, version 3 of this package was released, which includes breaking changes. Please refer to the [upgrade guide](./V2-TO-V3-UPGRADE-GUIDE.md) before upgrading.
|
|
14
|
-
|
|
15
|
-
Add the package as a dependency:
|
|
16
|
-
|
|
17
|
-
```
|
|
18
|
-
npm install resolve-accept-language
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
Code example:
|
|
22
|
-
|
|
23
|
-
```ts
|
|
24
|
-
import { resolveAcceptLanguage } from 'resolve-accept-language'
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* The API is well documented from within your IDE using TSDoc. The arguments are as follows:
|
|
28
|
-
*
|
|
29
|
-
* 1) The HTTP accept-language header.
|
|
30
|
-
* 2) The available locales (they must contain the default locale).
|
|
31
|
-
* 3) The default locale.
|
|
32
|
-
*/
|
|
33
|
-
console.log(
|
|
34
|
-
resolveAcceptLanguage(
|
|
35
|
-
'fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001',
|
|
36
|
-
// The `as const` is optional for TypeScript but gives better typing.
|
|
37
|
-
['en-US', 'fr-CA'] as const,
|
|
38
|
-
'en-US'
|
|
39
|
-
)
|
|
40
|
-
)
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
Output:
|
|
44
|
-
|
|
45
|
-
```
|
|
46
|
-
fr-CA
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
## Advanced use cases
|
|
50
|
-
|
|
51
|
-
### Match types
|
|
52
|
-
|
|
53
|
-
You may want to control exactly the behavior depending on the type of match. For example, you might want to display a language picker on your home page if the match is not satisfactory. In those cases, you will need to use the `{ returnMatchType: true }` option. It offers more visibility into the selection process while matching a locale:
|
|
54
|
-
|
|
55
|
-
```ts
|
|
56
|
-
import { MATCH_TYPES, resolveAcceptLanguage } from 'resolve-accept-language'
|
|
57
|
-
|
|
58
|
-
const { match, matchType } = resolveAcceptLanguage(
|
|
59
|
-
'fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001' as const,
|
|
60
|
-
['en-US', 'fr-CA'],
|
|
61
|
-
'fr-CA',
|
|
62
|
-
{ returnMatchType: true }
|
|
63
|
-
)
|
|
64
|
-
|
|
65
|
-
console.log(`A locale was matched: ${match}`)
|
|
66
|
-
|
|
67
|
-
if (matchType === MATCH_TYPES.locale) {
|
|
68
|
-
console.log('The match is locale-based')
|
|
69
|
-
} else if (matchType === MATCH_TYPES.languageSpecificLocale) {
|
|
70
|
-
console.log('The match is language specific locale-based')
|
|
71
|
-
} else if (matchType === MATCH_TYPES.language) {
|
|
72
|
-
console.log('The match is language-based')
|
|
73
|
-
} else if (matchType === MATCH_TYPES.relatedLocale) {
|
|
74
|
-
console.log('The match is related-locale-based')
|
|
75
|
-
} else if (matchType === MATCH_TYPES.languageCountry) {
|
|
76
|
-
console.log('The match is language country-based')
|
|
77
|
-
} else if (matchType === MATCH_TYPES.defaultLocale) {
|
|
78
|
-
console.log('The match is the default locale')
|
|
79
|
-
}
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
### Country match
|
|
83
|
-
|
|
84
|
-
There may be cases where it is preferred to perform a "country match" before falling back to the default locale match. For example:
|
|
85
|
-
|
|
86
|
-
```ts
|
|
87
|
-
console.log(
|
|
88
|
-
resolveAcceptLanguage('af-ZA', ['en-US', 'zu-ZA'] as const, 'en-US', {
|
|
89
|
-
returnMatchType: true,
|
|
90
|
-
matchCountry: true,
|
|
91
|
-
})
|
|
92
|
-
)
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
Output:
|
|
96
|
-
|
|
97
|
-
```json
|
|
98
|
-
{ "match": "zu-ZA", "matchType": "country" }
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
In this case, the header prefers `af-ZA`, which shares the same country as `zu-ZA`. Instead of falling back to the default `en-US`, `zu-ZA` is matched.
|
|
102
|
-
|
|
103
|
-
This behavior is not set by default because, in most cases, the quality of the default locale is better than the translations. Performing a country match could potentially lower the quality of the selection. However, there may be cases where this is not true, which is why the `matchCountry` option exists.
|
|
104
|
-
|
|
105
|
-
## How does the resolver work?
|
|
106
|
-
|
|
107
|
-
As per RFC 4647, this package uses the "lookup" matching scheme. This means that it will always produce exactly one match for a given request.
|
|
108
|
-
|
|
109
|
-
The matching strategy follows these steps, in order:
|
|
110
|
-
|
|
111
|
-
1. Start with the default locale, as it's considered the highest quality content available.
|
|
112
|
-
2. Extract all locales and languages from the HTTP header, sorted by quality factor and position in the header. Each of these elements is referred to as a "directive".
|
|
113
|
-
3. Perform matching in several stages:
|
|
114
|
-
1. **Locale Match**: Look for an exact locale match in the directives.
|
|
115
|
-
2. **Language-Specific Locale Match**: If no locale match is found, look for a locale that matches the language of the directive.
|
|
116
|
-
3. **Language Match**: If no language-specific locale match is found, look for a locale that matches the language.
|
|
117
|
-
4. **Related-Locale Match**: If no language match is found, look for a locale that matches the language in the next round of directives.
|
|
118
|
-
5. **Language Country Match**: If no related-locale match is found, look for a locale that matches the default locale's language but in a country from a locale specified in a directive.
|
|
119
|
-
6. **Country Match**: If the option is enabled and no language country match is found, look for a locale that matches the country in the next round of directives.
|
|
120
|
-
7. **Default Locale Match**: If all else fails, fall back to the default locale.
|
|
121
|
-
|
|
122
|
-
Each stage only happens if the previous stage didn't find a match. This ensures the best possible match is found according to the given criteria.
|
|
123
|
-
|
|
124
|
-
## Why another `Accept-Language` package?
|
|
125
|
-
|
|
126
|
-
The `Accept-Language` header has been around since 1999. Like many other standards that deal with languages, the header is based
|
|
127
|
-
on BCP 47 language tags. Language tags can be as simple as `fr` (non-country specific French) or more complex, for example,
|
|
128
|
-
`sr-Latn-RS` would represent Latin script Serbian.
|
|
129
|
-
|
|
130
|
-
One of the main challenges is that BCP 47 language tags can be either overly simple or too complex. This is one of the problems this
|
|
131
|
-
library will try to address by focusing on locales identifiers using the `language`-`country` format instead of trying to provide
|
|
132
|
-
full BCP 47 language tags support. The main reasons for this:
|
|
133
|
-
|
|
134
|
-
- Using 2 letter language codes is rarely sufficient. Without being explicit about the targeted country for a given language, it is impossible to provide the right format for some content such as dates and numbers. Also, while languages are similar across countries, there are different ways to say the same thing. Our hypothesis is that by better targeting the audience, the user experience will improve.
|
|
135
|
-
- About 99% of all cases can be covered using the `language`-`country` format. We could possibly extend script support in the future given a valid use case, but in the meantime, our goal is to keep this library as simple as possible, while providing the best matches.
|
|
136
|
-
|
|
137
|
-
## Additional references
|
|
138
|
-
|
|
139
|
-
- Matching of Language Tags ([RFC 4647](https://tools.ietf.org/html/rfc4647))
|
|
140
|
-
- Tags for Identifying Languages ([RFC 4646](https://tools.ietf.org/html/rfc4646))
|
|
141
|
-
- The Accept-Language request-header field ([RFC 2616 section 14.4](https://tools.ietf.org/html/rfc2616#section-14.4))
|
|
142
|
-
- Quality values ([RFC 2616 section 3.9](https://tools.ietf.org/html/rfc2616#section-3.9))
|
|
1
|
+
# resolve-accept-language
|
|
2
|
+
|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
|
+
[](https://www.npmjs.com/package/resolve-accept-language)
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
Resolve the best locale based on the value of an `Accept-Language` HTTP header.
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
> ⚠ In March 2024, version 3 of this package was released, which includes breaking changes. Please refer to the [upgrade guide](./V2-TO-V3-UPGRADE-GUIDE.md) before upgrading.
|
|
14
|
+
|
|
15
|
+
Add the package as a dependency:
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
npm install resolve-accept-language
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Code example:
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { resolveAcceptLanguage } from 'resolve-accept-language'
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The API is well documented from within your IDE using TSDoc. The arguments are as follows:
|
|
28
|
+
*
|
|
29
|
+
* 1) The HTTP accept-language header.
|
|
30
|
+
* 2) The available locales (they must contain the default locale).
|
|
31
|
+
* 3) The default locale.
|
|
32
|
+
*/
|
|
33
|
+
console.log(
|
|
34
|
+
resolveAcceptLanguage(
|
|
35
|
+
'fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001',
|
|
36
|
+
// The `as const` is optional for TypeScript but gives better typing.
|
|
37
|
+
['en-US', 'fr-CA'] as const,
|
|
38
|
+
'en-US'
|
|
39
|
+
)
|
|
40
|
+
)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Output:
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
fr-CA
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Advanced use cases
|
|
50
|
+
|
|
51
|
+
### Match types
|
|
52
|
+
|
|
53
|
+
You may want to control exactly the behavior depending on the type of match. For example, you might want to display a language picker on your home page if the match is not satisfactory. In those cases, you will need to use the `{ returnMatchType: true }` option. It offers more visibility into the selection process while matching a locale:
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { MATCH_TYPES, resolveAcceptLanguage } from 'resolve-accept-language'
|
|
57
|
+
|
|
58
|
+
const { match, matchType } = resolveAcceptLanguage(
|
|
59
|
+
'fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001' as const,
|
|
60
|
+
['en-US', 'fr-CA'],
|
|
61
|
+
'fr-CA',
|
|
62
|
+
{ returnMatchType: true }
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
console.log(`A locale was matched: ${match}`)
|
|
66
|
+
|
|
67
|
+
if (matchType === MATCH_TYPES.locale) {
|
|
68
|
+
console.log('The match is locale-based')
|
|
69
|
+
} else if (matchType === MATCH_TYPES.languageSpecificLocale) {
|
|
70
|
+
console.log('The match is language specific locale-based')
|
|
71
|
+
} else if (matchType === MATCH_TYPES.language) {
|
|
72
|
+
console.log('The match is language-based')
|
|
73
|
+
} else if (matchType === MATCH_TYPES.relatedLocale) {
|
|
74
|
+
console.log('The match is related-locale-based')
|
|
75
|
+
} else if (matchType === MATCH_TYPES.languageCountry) {
|
|
76
|
+
console.log('The match is language country-based')
|
|
77
|
+
} else if (matchType === MATCH_TYPES.defaultLocale) {
|
|
78
|
+
console.log('The match is the default locale')
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Country match
|
|
83
|
+
|
|
84
|
+
There may be cases where it is preferred to perform a "country match" before falling back to the default locale match. For example:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
console.log(
|
|
88
|
+
resolveAcceptLanguage('af-ZA', ['en-US', 'zu-ZA'] as const, 'en-US', {
|
|
89
|
+
returnMatchType: true,
|
|
90
|
+
matchCountry: true,
|
|
91
|
+
})
|
|
92
|
+
)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Output:
|
|
96
|
+
|
|
97
|
+
```json
|
|
98
|
+
{ "match": "zu-ZA", "matchType": "country" }
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
In this case, the header prefers `af-ZA`, which shares the same country as `zu-ZA`. Instead of falling back to the default `en-US`, `zu-ZA` is matched.
|
|
102
|
+
|
|
103
|
+
This behavior is not set by default because, in most cases, the quality of the default locale is better than the translations. Performing a country match could potentially lower the quality of the selection. However, there may be cases where this is not true, which is why the `matchCountry` option exists.
|
|
104
|
+
|
|
105
|
+
## How does the resolver work?
|
|
106
|
+
|
|
107
|
+
As per RFC 4647, this package uses the "lookup" matching scheme. This means that it will always produce exactly one match for a given request.
|
|
108
|
+
|
|
109
|
+
The matching strategy follows these steps, in order:
|
|
110
|
+
|
|
111
|
+
1. Start with the default locale, as it's considered the highest quality content available.
|
|
112
|
+
2. Extract all locales and languages from the HTTP header, sorted by quality factor and position in the header. Each of these elements is referred to as a "directive".
|
|
113
|
+
3. Perform matching in several stages:
|
|
114
|
+
1. **Locale Match**: Look for an exact locale match in the directives.
|
|
115
|
+
2. **Language-Specific Locale Match**: If no locale match is found, look for a locale that matches the language of the directive.
|
|
116
|
+
3. **Language Match**: If no language-specific locale match is found, look for a locale that matches the language.
|
|
117
|
+
4. **Related-Locale Match**: If no language match is found, look for a locale that matches the language in the next round of directives.
|
|
118
|
+
5. **Language Country Match**: If no related-locale match is found, look for a locale that matches the default locale's language but in a country from a locale specified in a directive.
|
|
119
|
+
6. **Country Match**: If the option is enabled and no language country match is found, look for a locale that matches the country in the next round of directives.
|
|
120
|
+
7. **Default Locale Match**: If all else fails, fall back to the default locale.
|
|
121
|
+
|
|
122
|
+
Each stage only happens if the previous stage didn't find a match. This ensures the best possible match is found according to the given criteria.
|
|
123
|
+
|
|
124
|
+
## Why another `Accept-Language` package?
|
|
125
|
+
|
|
126
|
+
The `Accept-Language` header has been around since 1999. Like many other standards that deal with languages, the header is based
|
|
127
|
+
on BCP 47 language tags. Language tags can be as simple as `fr` (non-country specific French) or more complex, for example,
|
|
128
|
+
`sr-Latn-RS` would represent Latin script Serbian.
|
|
129
|
+
|
|
130
|
+
One of the main challenges is that BCP 47 language tags can be either overly simple or too complex. This is one of the problems this
|
|
131
|
+
library will try to address by focusing on locales identifiers using the `language`-`country` format instead of trying to provide
|
|
132
|
+
full BCP 47 language tags support. The main reasons for this:
|
|
133
|
+
|
|
134
|
+
- Using 2 letter language codes is rarely sufficient. Without being explicit about the targeted country for a given language, it is impossible to provide the right format for some content such as dates and numbers. Also, while languages are similar across countries, there are different ways to say the same thing. Our hypothesis is that by better targeting the audience, the user experience will improve.
|
|
135
|
+
- About 99% of all cases can be covered using the `language`-`country` format. We could possibly extend script support in the future given a valid use case, but in the meantime, our goal is to keep this library as simple as possible, while providing the best matches.
|
|
136
|
+
|
|
137
|
+
## Additional references
|
|
138
|
+
|
|
139
|
+
- Matching of Language Tags ([RFC 4647](https://tools.ietf.org/html/rfc4647))
|
|
140
|
+
- Tags for Identifying Languages ([RFC 4646](https://tools.ietf.org/html/rfc4646))
|
|
141
|
+
- The Accept-Language request-header field ([RFC 2616 section 14.4](https://tools.ietf.org/html/rfc2616#section-14.4))
|
|
142
|
+
- Quality values ([RFC 2616 section 3.9](https://tools.ietf.org/html/rfc2616#section-3.9))
|
package/dist/cjs/directives.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";function _array_like_to_array(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,a=new Array(r);t<r;t++)a[t]=e[t];return a}function _array_without_holes(e){if(Array.isArray(e))return _array_like_to_array(e)}function _iterable_to_array(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function _non_iterable_spread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _to_consumable_array(e){return _array_without_holes(e)||_iterable_to_array(e)||_unsupported_iterable_to_array(e)||_non_iterable_spread()}function _unsupported_iterable_to_array(e,r){if(e){if("string"==typeof e)return _array_like_to_array(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(t):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_array_like_to_array(e,r):void 0}}Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"getDirectives",{enumerable:!0,get:function(){return getDirectives}});var REGEX_DIRECTIVE=/^(([a-z]{2})(-([a-z]{2}|419))?)(;q=((1(\.0{0,3})?)|(0(\.\d{0,3})?)))?$/i,getDirective=function(e){var r=
|
|
1
|
+
"use strict";function _array_like_to_array(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,a=new Array(r);t<r;t++)a[t]=e[t];return a}function _array_without_holes(e){if(Array.isArray(e))return _array_like_to_array(e)}function _iterable_to_array(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function _non_iterable_spread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _to_consumable_array(e){return _array_without_holes(e)||_iterable_to_array(e)||_unsupported_iterable_to_array(e)||_non_iterable_spread()}function _unsupported_iterable_to_array(e,r){if(e){if("string"==typeof e)return _array_like_to_array(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(t):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_array_like_to_array(e,r):void 0}}Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"getDirectives",{enumerable:!0,get:function(){return getDirectives}});var REGEX_DIRECTIVE=/^(([a-z]{2})(-([a-z]{2}|419))?)(;q=((1(\.0{0,3})?)|(0(\.\d{0,3})?)))?$/i,getDirective=function(e){var r=REGEX_DIRECTIVE.exec(e);if(r){var t=r[2],a=r[4],o=r[6],n=t.toLowerCase(),i=a?a.toUpperCase():void 0;if("419"!==i||"es"===n){var s=Number(o),u=isNaN(s)?1:s;return{languageCode:n,countryCode:i,locale:i?"".concat(n,"-").concat(i):void 0,quality:u}}}},getDirectives=function(e){var r=[],t=0;e.split(",").forEach(function(e){var a=getDirective(e.trim());a&&(r.some(function(e){return e.languageCode===a.languageCode&&e.locale===a.locale})||(r.push(Object.assign(Object.assign({},a),{headerPosition:t})),t++))});var a=r.map(function(e){return e.locale}).indexOf("es-419");if(-1!==a){var o,n=r[a],i=["es-AR","es-CL","es-CO","es-CR","es-HN","es-MX","es-PE","es-US","es-UY","es-VE"].map(function(e){return Object.assign(Object.assign({},n),{locale:e})});(o=r).splice.apply(o,[a,1].concat(_to_consumable_array(i)))}return r.sort(function(e,r){var t=r.quality-e.quality;return t||e.headerPosition-r.headerPosition})};
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -6,19 +6,6 @@ export declare const MATCH_TYPES: {
|
|
|
6
6
|
};
|
|
7
7
|
/** Type to normalize the locale format. */
|
|
8
8
|
export type NormalizeLocale<Remainder extends string> = Remainder extends `${infer LanguageCode}-${infer CountryCode}` ? `${Lowercase<LanguageCode>}-${Uppercase<CountryCode>}` : Remainder;
|
|
9
|
-
/** Additional options to apply. */
|
|
10
|
-
type Options<WithMatchType extends boolean | undefined> = {
|
|
11
|
-
/** Should the match type be returned? */
|
|
12
|
-
returnMatchType?: WithMatchType;
|
|
13
|
-
/** Should the country of the locale be used for matching? */
|
|
14
|
-
matchCountry?: boolean;
|
|
15
|
-
};
|
|
16
|
-
type Result<Locales extends readonly string[], WithMatchType extends boolean | undefined> = WithMatchType extends true ? {
|
|
17
|
-
/** The best locale match. */
|
|
18
|
-
match: NormalizeLocale<Locales[number]>;
|
|
19
|
-
/** The type of match. */
|
|
20
|
-
matchType: MatchType;
|
|
21
|
-
} : NormalizeLocale<Locales[number]>;
|
|
22
9
|
/**
|
|
23
10
|
* Resolve the preferred locale from an HTTP `Accept-Language` header.
|
|
24
11
|
*
|
|
@@ -40,5 +27,14 @@ type Result<Locales extends readonly string[], WithMatchType extends boolean | u
|
|
|
40
27
|
* 'en-US'
|
|
41
28
|
* )
|
|
42
29
|
*/
|
|
43
|
-
export declare
|
|
44
|
-
|
|
30
|
+
export declare function resolveAcceptLanguage<Locales extends readonly string[]>(acceptLanguageHeader: string, locales: Locales, defaultLocale: Locales[number], options?: {
|
|
31
|
+
returnMatchType?: false;
|
|
32
|
+
matchCountry?: boolean;
|
|
33
|
+
}): NormalizeLocale<Locales[number]>;
|
|
34
|
+
export declare function resolveAcceptLanguage<Locales extends readonly string[]>(acceptLanguageHeader: string, locales: Locales, defaultLocale: Locales[number], options: {
|
|
35
|
+
returnMatchType: true;
|
|
36
|
+
matchCountry?: boolean;
|
|
37
|
+
}): {
|
|
38
|
+
match: NormalizeLocale<Locales[number]>;
|
|
39
|
+
matchType: MatchType;
|
|
40
|
+
};
|
package/dist/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";function _export(e,r){for(var t in r)Object.defineProperty(e,t,{enumerable:!0,get:Object.getOwnPropertyDescriptor(r,t).get})}Object.defineProperty(exports,"__esModule",{value:!0}),_export(exports,{get MATCH_TYPES(){return MATCH_TYPES},get resolveAcceptLanguage(){return resolveAcceptLanguage}});var _directives=require("./directives.js"),_locales=require("./locales.js");function _array_like_to_array(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,a=new Array(r);t<r;t++)a[t]=e[t];return a}function _array_without_holes(e){if(Array.isArray(e))return _array_like_to_array(e)}function _iterable_to_array(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function _non_iterable_spread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _to_consumable_array(e){return _array_without_holes(e)||_iterable_to_array(e)||_unsupported_iterable_to_array(e)||_non_iterable_spread()}function _unsupported_iterable_to_array(e,r){if(e){if("string"==typeof e)return _array_like_to_array(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(t):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_array_like_to_array(e,r):void 0}}var MATCH_TYPES={locale:"locale",languageSpecificLocale:"languageSpecificLocale",language:"language",relatedLocale:"relatedLocale",languageCountry:"languageCountry",country:"country",defaultLocale:"defaultLocale"}
|
|
1
|
+
"use strict";function _export(e,r){for(var t in r)Object.defineProperty(e,t,{enumerable:!0,get:Object.getOwnPropertyDescriptor(r,t).get})}Object.defineProperty(exports,"__esModule",{value:!0}),_export(exports,{get MATCH_TYPES(){return MATCH_TYPES},get resolveAcceptLanguage(){return resolveAcceptLanguage}});var _directives=require("./directives.js"),_locales=require("./locales.js");function _array_like_to_array(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,a=new Array(r);t<r;t++)a[t]=e[t];return a}function _array_without_holes(e){if(Array.isArray(e))return _array_like_to_array(e)}function _iterable_to_array(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function _non_iterable_spread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _to_consumable_array(e){return _array_without_holes(e)||_iterable_to_array(e)||_unsupported_iterable_to_array(e)||_non_iterable_spread()}function _unsupported_iterable_to_array(e,r){if(e){if("string"==typeof e)return _array_like_to_array(e,r);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(t):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_array_like_to_array(e,r):void 0}}var MATCH_TYPES={locale:"locale",languageSpecificLocale:"languageSpecificLocale",language:"language",relatedLocale:"relatedLocale",languageCountry:"languageCountry",country:"country",defaultLocale:"defaultLocale"};function resolveAcceptLanguage(e,r,t,a){if(r.forEach(function(e){if(!(0,_locales.isLocale)(e,!1))throw new Error("Invalid locale identifier '".concat(e,"'. A valid locale should follow the BCP 47 'language-country' format."))}),!(0,_locales.isLocale)(t,!1))throw new Error("Invalid default locale identifier '".concat(t,"'. A valid locale should follow the BCP 47 'language-country' format."));if(!r.some(function(e){return e.toLowerCase()===t.toLowerCase()}))throw new Error("The default locale '".concat(t,"' must be included in the locales array because it is used as a fallback when no match is found."));var o=new _locales.Locale(t),n=[o.identifier].concat(_to_consumable_array(r.map(function(e){return new _locales.Locale(e).identifier}))),l=function(){var r=new _locales.LocaleList(n),t=(0,_directives.getDirectives)(e),l=t.filter(function(e){return e.languageCode in r.languages}),i=!0,c=!1,u=void 0;try{for(var f,y=l[Symbol.iterator]();!(i=(f=y.next()).done);i=!0){var d=f.value,_=d.locale,v=d.languageCode;if(void 0===_){var s=!0,g=!1,h=void 0;try{for(var m,p=t[Symbol.iterator]();!(s=(m=p.next()).done);s=!0){var b=m.value;if(b.languageCode===v&&void 0!==b.locale&&b.locale in r.locales)return{match:b.locale,matchType:MATCH_TYPES.languageSpecificLocale}}}catch(e){g=!0,h=e}finally{try{s||null==p.return||p.return()}finally{if(g)throw h}}var C=!0,T=!1,w=void 0;try{for(var S,A=r.objects[Symbol.iterator]();!(C=(S=A.next()).done);C=!0){var L=S.value;if(L.languageCode===v)return{match:L.identifier,matchType:MATCH_TYPES.language}}}catch(e){T=!0,w=e}finally{try{C||null==A.return||A.return()}finally{if(T)throw w}}}else if(_ in r.locales)return{match:_,matchType:MATCH_TYPES.locale}}}catch(e){c=!0,u=e}finally{try{i||null==y.return||y.return()}finally{if(c)throw u}}var E=!0,P=!1,x=void 0;try{for(var M,j=l[Symbol.iterator]();!(E=(M=j.next()).done);E=!0){var H=M.value,Y=!0,O=!1,k=void 0;try{for(var I,q=r.objects[Symbol.iterator]();!(Y=(I=q.next()).done);Y=!0){var B=I.value;if(B.languageCode===H.languageCode)return{match:B.identifier,matchType:MATCH_TYPES.relatedLocale}}}catch(e){O=!0,k=e}finally{try{Y||null==q.return||q.return()}finally{if(O)throw k}}}}catch(e){P=!0,x=e}finally{try{E||null==j.return||j.return()}finally{if(P)throw x}}var D=r.objects.filter(function(e){return e.languageCode===o.languageCode&&e.identifier!==o.identifier}).map(function(e){return e.countryCode});if(D.length>0){var U=!0,$=!1,z=void 0;try{for(var F,G=t[Symbol.iterator]();!(U=(F=G.next()).done);U=!0){var J=F.value;if(void 0!==J.locale&&void 0!==J.countryCode&&-1!==D.indexOf(J.countryCode))return{match:"".concat(o.languageCode,"-").concat(J.countryCode),matchType:MATCH_TYPES.languageCountry}}}catch(e){$=!0,z=e}finally{try{U||null==G.return||G.return()}finally{if($)throw z}}}if(null==a?void 0:a.matchCountry){var K=!0,N=!1,Q=void 0;try{for(var R,V=t[Symbol.iterator]();!(K=(R=V.next()).done);K=!0){var W=R.value,X=!0,Z=!1,ee=void 0;try{for(var re,te=r.objects[Symbol.iterator]();!(X=(re=te.next()).done);X=!0){var ae=re.value;if(ae.countryCode===W.countryCode)return{match:ae.identifier,matchType:MATCH_TYPES.country}}}catch(e){Z=!0,ee=e}finally{try{X||null==te.return||te.return()}finally{if(Z)throw ee}}}}catch(e){N=!0,Q=e}finally{try{K||null==V.return||V.return()}finally{if(N)throw Q}}}return{match:o.identifier,matchType:MATCH_TYPES.defaultLocale}}();return(null==a?void 0:a.returnMatchType)?l:l.match}
|
package/dist/cjs/locales.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export declare class Locale {
|
|
|
15
15
|
*/
|
|
16
16
|
constructor(identifier: string);
|
|
17
17
|
}
|
|
18
|
+
/** A collection of locales with lookup maps for languages, countries, and locale identifiers. */
|
|
18
19
|
export declare class LocaleList {
|
|
19
20
|
/** A lookup of ISO 639-1 alpha-2 language codes. */
|
|
20
21
|
readonly languages: Record<string, boolean>;
|
|
@@ -36,7 +37,9 @@ export declare class LocaleList {
|
|
|
36
37
|
/**
|
|
37
38
|
* Is a given string a locale identifier following the BCP 47 `language`-`country` format.
|
|
38
39
|
*
|
|
39
|
-
* @param identifier - A potential locale
|
|
40
|
+
* @param identifier - A potential locale identifier to verify.
|
|
40
41
|
* @param caseNormalized - Should we verify if the identifier is using the case-normalized format?
|
|
42
|
+
*
|
|
43
|
+
* @returns `true` if the identifier is a valid locale, `false` otherwise.
|
|
41
44
|
*/
|
|
42
45
|
export declare const isLocale: (identifier: string, caseNormalized?: boolean) => boolean;
|
package/dist/esm/directives.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function _array_like_to_array(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,a=new Array(e);t<e;t++)a[t]=r[t];return a}function _array_without_holes(r){if(Array.isArray(r))return _array_like_to_array(r)}function _iterable_to_array(r){if("undefined"!=typeof Symbol&&null!=r[Symbol.iterator]||null!=r["@@iterator"])return Array.from(r)}function _non_iterable_spread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _to_consumable_array(r){return _array_without_holes(r)||_iterable_to_array(r)||_unsupported_iterable_to_array(r)||_non_iterable_spread()}function _unsupported_iterable_to_array(r,e){if(r){if("string"==typeof r)return _array_like_to_array(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(t):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_array_like_to_array(r,e):void 0}}var REGEX_DIRECTIVE=/^(([a-z]{2})(-([a-z]{2}|419))?)(;q=((1(\.0{0,3})?)|(0(\.\d{0,3})?)))?$/i,getDirective=function(r){var e=
|
|
1
|
+
function _array_like_to_array(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,a=new Array(e);t<e;t++)a[t]=r[t];return a}function _array_without_holes(r){if(Array.isArray(r))return _array_like_to_array(r)}function _iterable_to_array(r){if("undefined"!=typeof Symbol&&null!=r[Symbol.iterator]||null!=r["@@iterator"])return Array.from(r)}function _non_iterable_spread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _to_consumable_array(r){return _array_without_holes(r)||_iterable_to_array(r)||_unsupported_iterable_to_array(r)||_non_iterable_spread()}function _unsupported_iterable_to_array(r,e){if(r){if("string"==typeof r)return _array_like_to_array(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(t):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_array_like_to_array(r,e):void 0}}var REGEX_DIRECTIVE=/^(([a-z]{2})(-([a-z]{2}|419))?)(;q=((1(\.0{0,3})?)|(0(\.\d{0,3})?)))?$/i,getDirective=function(r){var e=REGEX_DIRECTIVE.exec(r);if(e){var t=e[2],a=e[4],o=e[6],n=t.toLowerCase(),i=a?a.toUpperCase():void 0;if("419"!==i||"es"===n){var l=Number(o),s=isNaN(l)?1:l;return{languageCode:n,countryCode:i,locale:i?"".concat(n,"-").concat(i):void 0,quality:s}}}};export var getDirectives=function(r){var e=[],t=0;r.split(",").forEach(function(r){var a=getDirective(r.trim());a&&(e.some(function(r){return r.languageCode===a.languageCode&&r.locale===a.locale})||(e.push(Object.assign(Object.assign({},a),{headerPosition:t})),t++))});var a=e.map(function(r){return r.locale}).indexOf("es-419");if(-1!==a){var o,n=e[a],i=["es-AR","es-CL","es-CO","es-CR","es-HN","es-MX","es-PE","es-US","es-UY","es-VE"].map(function(r){return Object.assign(Object.assign({},n),{locale:r})});(o=e).splice.apply(o,[a,1].concat(_to_consumable_array(i)))}return e.sort(function(r,e){var t=e.quality-r.quality;return t||r.headerPosition-e.headerPosition})};
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -6,19 +6,6 @@ export declare const MATCH_TYPES: {
|
|
|
6
6
|
};
|
|
7
7
|
/** Type to normalize the locale format. */
|
|
8
8
|
export type NormalizeLocale<Remainder extends string> = Remainder extends `${infer LanguageCode}-${infer CountryCode}` ? `${Lowercase<LanguageCode>}-${Uppercase<CountryCode>}` : Remainder;
|
|
9
|
-
/** Additional options to apply. */
|
|
10
|
-
type Options<WithMatchType extends boolean | undefined> = {
|
|
11
|
-
/** Should the match type be returned? */
|
|
12
|
-
returnMatchType?: WithMatchType;
|
|
13
|
-
/** Should the country of the locale be used for matching? */
|
|
14
|
-
matchCountry?: boolean;
|
|
15
|
-
};
|
|
16
|
-
type Result<Locales extends readonly string[], WithMatchType extends boolean | undefined> = WithMatchType extends true ? {
|
|
17
|
-
/** The best locale match. */
|
|
18
|
-
match: NormalizeLocale<Locales[number]>;
|
|
19
|
-
/** The type of match. */
|
|
20
|
-
matchType: MatchType;
|
|
21
|
-
} : NormalizeLocale<Locales[number]>;
|
|
22
9
|
/**
|
|
23
10
|
* Resolve the preferred locale from an HTTP `Accept-Language` header.
|
|
24
11
|
*
|
|
@@ -40,5 +27,14 @@ type Result<Locales extends readonly string[], WithMatchType extends boolean | u
|
|
|
40
27
|
* 'en-US'
|
|
41
28
|
* )
|
|
42
29
|
*/
|
|
43
|
-
export declare
|
|
44
|
-
|
|
30
|
+
export declare function resolveAcceptLanguage<Locales extends readonly string[]>(acceptLanguageHeader: string, locales: Locales, defaultLocale: Locales[number], options?: {
|
|
31
|
+
returnMatchType?: false;
|
|
32
|
+
matchCountry?: boolean;
|
|
33
|
+
}): NormalizeLocale<Locales[number]>;
|
|
34
|
+
export declare function resolveAcceptLanguage<Locales extends readonly string[]>(acceptLanguageHeader: string, locales: Locales, defaultLocale: Locales[number], options: {
|
|
35
|
+
returnMatchType: true;
|
|
36
|
+
matchCountry?: boolean;
|
|
37
|
+
}): {
|
|
38
|
+
match: NormalizeLocale<Locales[number]>;
|
|
39
|
+
matchType: MatchType;
|
|
40
|
+
};
|
package/dist/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function _array_like_to_array(r,e){(null==e||e>r.length)&&(e=r.length);for(var a=0,t=new Array(e);a<e;a++)t[a]=r[a];return t}function _array_without_holes(r){if(Array.isArray(r))return _array_like_to_array(r)}function _iterable_to_array(r){if("undefined"!=typeof Symbol&&null!=r[Symbol.iterator]||null!=r["@@iterator"])return Array.from(r)}function _non_iterable_spread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _to_consumable_array(r){return _array_without_holes(r)||_iterable_to_array(r)||_unsupported_iterable_to_array(r)||_non_iterable_spread()}function _unsupported_iterable_to_array(r,e){if(r){if("string"==typeof r)return _array_like_to_array(r,e);var a=Object.prototype.toString.call(r).slice(8,-1);return"Object"===a&&r.constructor&&(a=r.constructor.name),"Map"===a||"Set"===a?Array.from(a):"Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a)?_array_like_to_array(r,e):void 0}}import{getDirectives}from"./directives.js";import{isLocale,Locale,LocaleList}from"./locales.js";export var MATCH_TYPES={locale:"locale",languageSpecificLocale:"languageSpecificLocale",language:"language",relatedLocale:"relatedLocale",languageCountry:"languageCountry",country:"country",defaultLocale:"defaultLocale"};export
|
|
1
|
+
function _array_like_to_array(r,e){(null==e||e>r.length)&&(e=r.length);for(var a=0,t=new Array(e);a<e;a++)t[a]=r[a];return t}function _array_without_holes(r){if(Array.isArray(r))return _array_like_to_array(r)}function _iterable_to_array(r){if("undefined"!=typeof Symbol&&null!=r[Symbol.iterator]||null!=r["@@iterator"])return Array.from(r)}function _non_iterable_spread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _to_consumable_array(r){return _array_without_holes(r)||_iterable_to_array(r)||_unsupported_iterable_to_array(r)||_non_iterable_spread()}function _unsupported_iterable_to_array(r,e){if(r){if("string"==typeof r)return _array_like_to_array(r,e);var a=Object.prototype.toString.call(r).slice(8,-1);return"Object"===a&&r.constructor&&(a=r.constructor.name),"Map"===a||"Set"===a?Array.from(a):"Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a)?_array_like_to_array(r,e):void 0}}import{getDirectives}from"./directives.js";import{isLocale,Locale,LocaleList}from"./locales.js";export var MATCH_TYPES={locale:"locale",languageSpecificLocale:"languageSpecificLocale",language:"language",relatedLocale:"relatedLocale",languageCountry:"languageCountry",country:"country",defaultLocale:"defaultLocale"};export function resolveAcceptLanguage(r,e,a,t){if(e.forEach(function(r){if(!isLocale(r,!1))throw new Error("Invalid locale identifier '".concat(r,"'. A valid locale should follow the BCP 47 'language-country' format."))}),!isLocale(a,!1))throw new Error("Invalid default locale identifier '".concat(a,"'. A valid locale should follow the BCP 47 'language-country' format."));if(!e.some(function(r){return r.toLowerCase()===a.toLowerCase()}))throw new Error("The default locale '".concat(a,"' must be included in the locales array because it is used as a fallback when no match is found."));var o=new Locale(a),n=[o.identifier].concat(_to_consumable_array(e.map(function(r){return new Locale(r).identifier}))),l=function(){var e=new LocaleList(n),a=getDirectives(r),l=a.filter(function(r){return r.languageCode in e.languages}),i=!0,c=!1,u=void 0;try{for(var f,y=l[Symbol.iterator]();!(i=(f=y.next()).done);i=!0){var d=f.value,v=d.locale,_=d.languageCode;if(void 0===v){var h=!0,g=!1,s=void 0;try{for(var m,p=a[Symbol.iterator]();!(h=(m=p.next()).done);h=!0){var b=m.value;if(b.languageCode===_&&void 0!==b.locale&&b.locale in e.locales)return{match:b.locale,matchType:MATCH_TYPES.languageSpecificLocale}}}catch(r){g=!0,s=r}finally{try{h||null==p.return||p.return()}finally{if(g)throw s}}var C=!0,w=!1,T=void 0;try{for(var S,L=e.objects[Symbol.iterator]();!(C=(S=L.next()).done);C=!0){var A=S.value;if(A.languageCode===_)return{match:A.identifier,matchType:MATCH_TYPES.language}}}catch(r){w=!0,T=r}finally{try{C||null==L.return||L.return()}finally{if(w)throw T}}}else if(v in e.locales)return{match:v,matchType:MATCH_TYPES.locale}}}catch(r){c=!0,u=r}finally{try{i||null==y.return||y.return()}finally{if(c)throw u}}var E=!0,x=!1,M=void 0;try{for(var P,j=l[Symbol.iterator]();!(E=(P=j.next()).done);E=!0){var H=P.value,Y=!0,k=!1,I=void 0;try{for(var O,B=e.objects[Symbol.iterator]();!(Y=(O=B.next()).done);Y=!0){var D=O.value;if(D.languageCode===H.languageCode)return{match:D.identifier,matchType:MATCH_TYPES.relatedLocale}}}catch(r){k=!0,I=r}finally{try{Y||null==B.return||B.return()}finally{if(k)throw I}}}}catch(r){x=!0,M=r}finally{try{E||null==j.return||j.return()}finally{if(x)throw M}}var U=e.objects.filter(function(r){return r.languageCode===o.languageCode&&r.identifier!==o.identifier}).map(function(r){return r.countryCode});if(U.length>0){var $=!0,q=!1,z=void 0;try{for(var F,G=a[Symbol.iterator]();!($=(F=G.next()).done);$=!0){var J=F.value;if(void 0!==J.locale&&void 0!==J.countryCode&&-1!==U.indexOf(J.countryCode))return{match:"".concat(o.languageCode,"-").concat(J.countryCode),matchType:MATCH_TYPES.languageCountry}}}catch(r){q=!0,z=r}finally{try{$||null==G.return||G.return()}finally{if(q)throw z}}}if(null==t?void 0:t.matchCountry){var K=!0,N=!1,Q=void 0;try{for(var R,V=a[Symbol.iterator]();!(K=(R=V.next()).done);K=!0){var W=R.value,X=!0,Z=!1,rr=void 0;try{for(var er,ar=e.objects[Symbol.iterator]();!(X=(er=ar.next()).done);X=!0){var tr=er.value;if(tr.countryCode===W.countryCode)return{match:tr.identifier,matchType:MATCH_TYPES.country}}}catch(r){Z=!0,rr=r}finally{try{X||null==ar.return||ar.return()}finally{if(Z)throw rr}}}}catch(r){N=!0,Q=r}finally{try{K||null==V.return||V.return()}finally{if(N)throw Q}}}return{match:o.identifier,matchType:MATCH_TYPES.defaultLocale}}();return(null==t?void 0:t.returnMatchType)?l:l.match}
|
package/dist/esm/locales.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export declare class Locale {
|
|
|
15
15
|
*/
|
|
16
16
|
constructor(identifier: string);
|
|
17
17
|
}
|
|
18
|
+
/** A collection of locales with lookup maps for languages, countries, and locale identifiers. */
|
|
18
19
|
export declare class LocaleList {
|
|
19
20
|
/** A lookup of ISO 639-1 alpha-2 language codes. */
|
|
20
21
|
readonly languages: Record<string, boolean>;
|
|
@@ -36,7 +37,9 @@ export declare class LocaleList {
|
|
|
36
37
|
/**
|
|
37
38
|
* Is a given string a locale identifier following the BCP 47 `language`-`country` format.
|
|
38
39
|
*
|
|
39
|
-
* @param identifier - A potential locale
|
|
40
|
+
* @param identifier - A potential locale identifier to verify.
|
|
40
41
|
* @param caseNormalized - Should we verify if the identifier is using the case-normalized format?
|
|
42
|
+
*
|
|
43
|
+
* @returns `true` if the identifier is a valid locale, `false` otherwise.
|
|
41
44
|
*/
|
|
42
45
|
export declare const isLocale: (identifier: string, caseNormalized?: boolean) => boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "resolve-accept-language",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.2",
|
|
4
4
|
"description": "Resolve the preferred locale based on the value of an `Accept-Language` HTTP header.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"accept-language",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"dist"
|
|
44
44
|
],
|
|
45
45
|
"scripts": {
|
|
46
|
-
"build": "npm run check-nvm-node-version && npm run prettier-write && npm run eslint-fix && rm -Rf ./dist && tsc && npx tsx src/build-scripts/build.ts && npm run test",
|
|
46
|
+
"build": "npm run check-nvm-node-version && npm run prettier-write && npm run eslint-fix && npm run typecheck && rm -Rf ./dist && tsc && npx tsx src/build-scripts/build.ts && npm run test",
|
|
47
47
|
"check-nvm-node-version": "check-node-version --node $(node -p \"'>=' + require('fs').readFileSync('.nvmrc', 'utf-8').trim()\")",
|
|
48
48
|
"depcheck": "depcheck",
|
|
49
49
|
"eslint-fix": "eslint --fix",
|
|
@@ -53,38 +53,39 @@
|
|
|
53
53
|
"prettier-write": "prettier --write .",
|
|
54
54
|
"release": "dotenv -- release-it --only-version",
|
|
55
55
|
"test": "jest --coverage",
|
|
56
|
-
"test-node-version": "node -e \"var resolveAcceptLanguage = require('./dist/cjs/index.js').resolveAcceptLanguage; var result = resolveAcceptLanguage('fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001', ['en-US', 'fr-CA'], 'en-US'); console.log(result === 'fr-CA' ? 'success' : 'failure');\""
|
|
56
|
+
"test-node-version": "node -e \"var resolveAcceptLanguage = require('./dist/cjs/index.js').resolveAcceptLanguage; var result = resolveAcceptLanguage('fr-CA;q=0.01,en-CA;q=0.1,en-US;q=0.001', ['en-US', 'fr-CA'], 'en-US'); console.log(result === 'fr-CA' ? 'success' : 'failure');\"",
|
|
57
|
+
"typecheck": "find . -name tsconfig.json -not -path '*/node_modules/*' -not -path '*/dist/*' -not -path '*/.*/*' -print0 | xargs -0 -n1 -I{} npx tsc --noEmit -p {}"
|
|
57
58
|
},
|
|
58
59
|
"devDependencies": {
|
|
59
|
-
"@release-it/conventional-changelog": "
|
|
60
|
-
"@swc/core": "1.15.
|
|
60
|
+
"@release-it/conventional-changelog": "11.0.0",
|
|
61
|
+
"@swc/core": "1.15.33",
|
|
61
62
|
"@types/jest": "30.0.0",
|
|
62
|
-
"@types/node": "25.
|
|
63
|
+
"@types/node": "25.9.0",
|
|
63
64
|
"check-node-version": "4.2.1",
|
|
64
65
|
"depcheck": "1.4.7",
|
|
65
66
|
"dotenv-cli": "11.0.0",
|
|
66
|
-
"eslint": "10.
|
|
67
|
+
"eslint": "10.4.0",
|
|
67
68
|
"eslint-config-prettier": "10.1.8",
|
|
68
69
|
"eslint-import-resolver-typescript": "4.4.4",
|
|
69
70
|
"eslint-plugin-es-x": "9.6.0",
|
|
70
71
|
"eslint-plugin-import-x": "4.16.2",
|
|
71
|
-
"eslint-plugin-jest": "29.15.
|
|
72
|
-
"eslint-plugin-package-json": "
|
|
72
|
+
"eslint-plugin-jest": "29.15.2",
|
|
73
|
+
"eslint-plugin-package-json": "1.1.0",
|
|
73
74
|
"eslint-plugin-prefer-arrow-functions": "3.9.1",
|
|
74
75
|
"eslint-plugin-prettier": "5.5.5",
|
|
75
76
|
"eslint-plugin-tsdoc": "0.5.2",
|
|
76
77
|
"eslint-plugin-unicorn": "64.0.0",
|
|
77
78
|
"husky": "9.1.7",
|
|
78
|
-
"jest": "30.
|
|
79
|
-
"jiti": "2.
|
|
79
|
+
"jest": "30.4.2",
|
|
80
|
+
"jiti": "2.7.0",
|
|
80
81
|
"jsonc-eslint-parser": "3.1.0",
|
|
81
|
-
"npm-check-updates": "
|
|
82
|
-
"prettier": "3.8.
|
|
83
|
-
"release-it": "
|
|
84
|
-
"terser": "5.
|
|
82
|
+
"npm-check-updates": "22.2.0",
|
|
83
|
+
"prettier": "3.8.3",
|
|
84
|
+
"release-it": "20.0.1",
|
|
85
|
+
"terser": "5.47.1",
|
|
85
86
|
"ts-jest": "29.4.9",
|
|
86
|
-
"typescript": "6.0.
|
|
87
|
-
"typescript-eslint": "8.
|
|
87
|
+
"typescript": "6.0.3",
|
|
88
|
+
"typescript-eslint": "8.59.4"
|
|
88
89
|
},
|
|
89
90
|
"engines": {
|
|
90
91
|
"node": ">=4.0.0"
|