You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+5Lines changed: 5 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -146,6 +146,11 @@ You can do `import { ... } from '@intlify/utils'` the above utilities
146
146
-`setCookieLocale`
147
147
-`getPathLocale`
148
148
-`getQueryLocale`
149
+
-`tryHeaderLocales`
150
+
-`tryHeaderLocale`
151
+
-`tryCookieLocale`
152
+
-`tryPathLocale`
153
+
-`tryQueryLocale`
149
154
150
155
The about utilies functions accpet Web APIs such as [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) and [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) that is supported by JS environments (such as Deno, Bun, and Browser)
Copy file name to clipboardExpand all lines: deno/web.ts
+120Lines changed: 120 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -111,6 +111,8 @@ export function getHeaderLanguage(
111
111
* @param {HeaderOptions['name']} options.name The header name, which is as default `accept-language`.
112
112
* @param {HeaderOptions['parser']} options.parser The parser function, which is as default {@link parseDefaultHeader}. If you are specifying more than one in your own format, you need a parser.
113
113
*
114
+
* @throws {RangeError} Throws the {@link RangeError} if header are not a well-formed BCP 47 language tag.
115
+
*
114
116
* @returns {Array<Intl.Locale>} The locales that wrapped from header, if you use `accept-language` header and `*` (any language) or empty string is detected, return an empty array.
115
117
*/
116
118
exportfunctiongetHeaderLocales(
@@ -126,6 +128,31 @@ export function getHeaderLocales(
126
128
})
127
129
}
128
130
131
+
/**
132
+
* try to get locales from header
133
+
*
134
+
* @description wrap language tags with {@link Intl.Locale | locale}, languages tags will be parsed from `accept-language` header as default. Unlike {@link getHeaderLocales}, this function does not throw an error if the locale cannot be obtained, this function returns `null`.
135
+
*
136
+
* @param {Request} request The {@link Request | request}
137
+
* @param {HeaderOptions['name']} options.name The header name, which is as default `accept-language`.
138
+
* @param {HeaderOptions['parser']} options.parser The parser function, which is as default {@link parseDefaultHeader}. If you are specifying more than one in your own format, you need a parser.
139
+
*
140
+
* @returns {Array<Intl.Locale> | null} The locales that wrapped from header, if you use `accept-language` header and `*` (any language) or empty string is detected, return an empty array. if header are not a well-formed BCP 47 language tag, return `null`.
141
+
*/
142
+
exportfunctiontryHeaderLocales(
143
+
request: Request,
144
+
{
145
+
name =ACCEPT_LANGUAGE_HEADER,
146
+
parser =parseDefaultHeader,
147
+
}: HeaderOptions={},
148
+
): Intl.Locale[]|null{
149
+
try{
150
+
returngetHeaderLocales(request,{ name, parser })
151
+
}catch{
152
+
returnnull
153
+
}
154
+
}
155
+
129
156
/**
130
157
* get locale from header
131
158
*
@@ -165,6 +192,33 @@ export function getHeaderLocale(
* @description wrap language tag with {@link Intl.Locale | locale}, languages tags will be parsed from `accept-language` header as default. Unlike {@link getHeaderLocale}, this function does not throw an error if the locale cannot be obtained, this function returns `null`.
199
+
*
200
+
* @param {Request} request The {@link Request | request}
201
+
* @param {string} options.lang The default language tag, Optional. default value is `en-US`. You must specify the language tag with the {@link https://datatracker.ietf.org/doc/html/rfc4646#section-2.1 | BCP 47 syntax}.
202
+
* @param {HeaderOptions['name']} options.name The header name, which is as default `accept-language`.
203
+
* @param {HeaderOptions['parser']} options.parser The parser function, which is as default {@link parseDefaultHeader}. If you are specifying more than one in your own format, you need a parser.
204
+
*
205
+
* @returns {Intl.Locale | null} The first locale that resolved from header string. if you use `accept-language` header and `*` (any language) or empty string is detected, return `en-US`. if `lang` option or header are not a well-formed BCP 47 language tag, return `null`.
@@ -203,6 +257,28 @@ export function getCookieLocale(
203
257
returngetLocaleWithGetter(getter)
204
258
}
205
259
260
+
/**
261
+
* try to get locale from cookie
262
+
*
263
+
* @description Unlike {@link getCookieLocale}, this function does not throw an error if the locale cannot be obtained, this function returns `null`.
264
+
*
265
+
* @param {Request} request The {@link Request | request}
266
+
* @param {string} options.lang The default language tag, default is `en-US`. You must specify the language tag with the {@link https://datatracker.ietf.org/doc/html/rfc4646#section-2.1 | BCP 47 syntax}.
267
+
* @param {string} options.name The cookie name, default is `i18n_locale`
268
+
*
269
+
* @returns {Intl.Locale | null} The locale that resolved from cookie. if `lang` option or cookie name value are not a well-formed BCP 47 language tag, return `null`.
270
+
*/
271
+
exportfunctiontryCookieLocale(
272
+
request: Request,
273
+
{ lang =DEFAULT_LANG_TAG, name =DEFAULT_COOKIE_NAME}={},
274
+
): Intl.Locale|null{
275
+
try{
276
+
returngetCookieLocale(request,{ lang, name })
277
+
}catch{
278
+
returnnull
279
+
}
280
+
}
281
+
206
282
/**
207
283
* set locale to the response `Set-Cookie` header.
208
284
*
@@ -264,6 +340,28 @@ export function getPathLocale(
* @description Unlike {@link getPathLocale}, this function does not throw an error if the locale cannot be obtained, this function returns `null`.
347
+
*
348
+
* @param {Request} request the {@link Request | request}
349
+
* @param {PathOptions['lang']} options.lang the language tag, which is as default `'en-US'`. optional
350
+
* @param {PathOptions['parser']} options.parser the path language parser, default {@link pathLanguageParser}, optional
351
+
*
352
+
* @returns {Intl.Locale | null} The locale that resolved from path. if the language in the path, that is not a well-formed BCP 47 language tag, return `null`.
353
+
*/
354
+
exportfunctiontryPathLocale(
355
+
request: Request,
356
+
{ lang =DEFAULT_LANG_TAG, parser =pathLanguageParser}: PathOptions={},
357
+
): Intl.Locale|null{
358
+
try{
359
+
returngetPathLocale(request,{ lang, parser })
360
+
}catch{
361
+
returnnull
362
+
}
363
+
}
364
+
267
365
/**
268
366
* get the locale from the query
269
367
*
@@ -282,6 +380,28 @@ export function getQueryLocale(
282
380
return_getQueryLocale(newURL(request.url),{ lang, name })
283
381
}
284
382
383
+
/**
384
+
* try to get the locale from the query
385
+
*
386
+
* @description Unlike {@link getQueryLocale}, this function does not throw an error if the locale cannot be obtained, this function returns `null`.
387
+
*
388
+
* @param {Request} request the {@link Request | request}
389
+
* @param {QueryOptions['lang']} options.lang the language tag, which is as default `'en-US'`. optional
* @returns {Intl.Locale | null} The locale that resolved from query. if the language in the query, that is not a well-formed BCP 47 language tag, return `null`.
393
+
*/
394
+
exportfunctiontryQueryLocale(
395
+
request: Request,
396
+
{ lang =DEFAULT_LANG_TAG, name ='locale'}: QueryOptions={},
0 commit comments