Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions deno/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ export type CookieOptions = CookieSerializeOptions & {
name?: string
}

/**
* Cookie locale options type
*/
export type CookieLocaleOptions = {
/**
* The default language tag
*/
lang?: string
/**
* Cookie name
*/
name?: string
}

/**
* Header options type
*/
Expand Down Expand Up @@ -154,13 +168,15 @@ export function parseDefaultHeader(input: string): string[] {
* get languages from header with getter function
*
* @param getter - the header string getter function
* @param options - the {@link HeaderOptions | header options}
*
* @returns The array of language tags
*/
export function getHeaderLanguagesWithGetter(
getter: () => string | null | undefined,
{ name = ACCEPT_LANGUAGE_HEADER, parser = parseDefaultHeader }: HeaderOptions = {}
options: HeaderOptions = {}
): string[] {
const { name = ACCEPT_LANGUAGE_HEADER, parser = parseDefaultHeader } = options
const langString = getter()
return langString
? name === ACCEPT_LANGUAGE_HEADER
Expand Down Expand Up @@ -247,34 +263,27 @@ export type PathOptions = {
* get the language from the path
*
* @param path - the target path
* @param options.lang - the language tag, which is as default `'en-US'`. optional
* @param options.parser - the path language parser, optional
* @param options - the {@link PathOptions | path options} object
*
* @returns the language that is parsed by the path language parser, if the language is not detected, return a `options.lang` value
*/
export function getPathLanguage(
path: string | URL,
{ lang = DEFAULT_LANG_TAG, parser = pathLanguageParser }: PathOptions = {}
): string {
export function getPathLanguage(path: string | URL, options: PathOptions = {}): string {
const { lang = DEFAULT_LANG_TAG, parser = pathLanguageParser } = options
return (parser || pathLanguageParser)(path) || lang
}

/**
* get the locale from the path
*
* @param path - the target path
* @param options.lang - the language tag, which is as default `'en-US'`. optional
* @param options.parser - the path language parser, optional
* @param options - the {@link PathOptions | path options} object
*
* @throws {RangeError} Throws the {@link RangeError} if the language in the path, that is not a well-formed BCP 47 language tag.
*
* @returns The locale that resolved from path
*/
export function getPathLocale(
path: string | URL,
{ lang = DEFAULT_LANG_TAG, parser = pathLanguageParser }: PathOptions = {}
): Intl.Locale {
return new Intl.Locale(getPathLanguage(path, { lang, parser }))
export function getPathLocale(path: string | URL, options: PathOptions = {}): Intl.Locale {
return new Intl.Locale(getPathLanguage(path, options))
}

function getURLSearchParams(input: string | URL | URLSearchParams): URLSearchParams {
Expand Down Expand Up @@ -322,6 +331,7 @@ export function getQueryLanguage(
* get the locale from the query
*
* @param query - the target query
* @param options - The {@link QueryOptions | query options}
* @param options.lang - the language tag, which is as default `'en-US'`. optional
* @param options.name - the query param name, default `'locale'`. optional
*
Expand All @@ -331,7 +341,8 @@ export function getQueryLanguage(
*/
export function getQueryLocale(
query: string | URL | URLSearchParams,
{ lang = DEFAULT_LANG_TAG, name = 'locale' }: QueryOptions = {}
options: QueryOptions = {}
): Intl.Locale {
const { lang = DEFAULT_LANG_TAG, name = 'locale' } = options
return new Intl.Locale(getQueryLanguage(query, { lang, name }))
}
3 changes: 1 addition & 2 deletions deno/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export function toLocale(val: string | Intl.Locale): Intl.Locale {
*/
export function validateLangTag(lang: string): boolean {
try {
// @ts-ignore NOTE: https://github.com/microsoft/TypeScript/pull/56079
Intl.getCanonicalLocales(lang)
return true
} catch {
Expand Down Expand Up @@ -93,7 +92,7 @@ export function parseAcceptLanguage(value: string): string[] {
* ```ts
* const oldLangName = 'en_US'
* const langTag = normalizeLanguageName(oldLangName)
* conosle.log(langTag) // en-US
* console.log(langTag) // en-US
* ```
*
* @param langName - The target language name
Expand Down
Loading
Loading