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
53 changes: 53 additions & 0 deletions src/h3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
parseDefaultHeader,
validateLocale
} from './http.ts'
import { warnOnce } from './utils.ts'

import type { H3Event } from 'h3'
import type {
Expand Down Expand Up @@ -61,6 +62,10 @@ import type {
* @returns The array of language tags, if you use `accept-language` header and `*` (any language) or empty string is detected, return an empty array.
*/
export function getHeaderLanguages(event: H3Event, options: HeaderOptions = {}): string[] {
warnOnce(
'`getHeaderLanguages` of `@intlify/utils/h3` is deprecated in v2. Use `getHeaderLanguages` of `@intlify/utils` instead.'
)

const { name = ACCEPT_LANGUAGE_HEADER } = options
const getter = () => {
const headers = getHeaders(event)
Expand Down Expand Up @@ -95,6 +100,10 @@ export function getHeaderLanguages(event: H3Event, options: HeaderOptions = {}):
* @returns The **first language tag** of header, if header is not exists, or `*` (any language), return empty string.
*/
export function getHeaderLanguage(event: H3Event, options: HeaderOptions = {}): string {
warnOnce(
'`getHeaderLanguage` of `@intlify/utils/h3` is deprecated in v2. Use `getHeaderLanguage` of `@intlify/utils` instead.'
)

return getHeaderLanguages(event, options)[0] || ''
}

Expand Down Expand Up @@ -125,6 +134,10 @@ export function getHeaderLanguage(event: H3Event, options: HeaderOptions = {}):
* @returns The locales that wrapped from header, if you use `accept-language` header and `*` (any language) or empty string is detected, return an empty array.
*/
export function getHeaderLocales(event: H3Event, options: HeaderOptions = {}): Intl.Locale[] {
warnOnce(
'`getHeaderLocales` of `@intlify/utils/h3` is deprecated in v2. Use `getHeaderLocales` of `@intlify/utils` instead.'
)

return mapToLocaleFromLanguageTag(getHeaderLanguages, event, options)
}

Expand All @@ -142,6 +155,10 @@ export function tryHeaderLocales(
event: H3Event,
options: HeaderOptions = {}
): Intl.Locale[] | null {
warnOnce(
'`tryHeaderLocales` of `@intlify/utils/h3` is deprecated in v2. Use `tryHeaderLocales` of `@intlify/utils` instead.'
)

try {
return getHeaderLocales(event, options)
} catch {
Expand Down Expand Up @@ -179,6 +196,10 @@ export function getHeaderLocale(
event: H3Event,
options: HeaderOptions & { lang?: string } = {}
): Intl.Locale {
warnOnce(
'`getHeaderLocale` of `@intlify/utils/h3` is deprecated in v2. Use `getHeaderLocale` of `@intlify/utils` instead.'
)

const {
lang = DEFAULT_LANG_TAG,
name = ACCEPT_LANGUAGE_HEADER,
Expand All @@ -201,6 +222,10 @@ export function tryHeaderLocale(
event: H3Event,
options: HeaderOptions & { lang?: string } = {}
): Intl.Locale | null {
warnOnce(
'`tryHeaderLocale` of `@intlify/utils/h3` is deprecated in v2. Use `tryHeaderLocale` of `@intlify/utils` instead.'
)

try {
return getHeaderLocale(event, options)
} catch {
Expand Down Expand Up @@ -233,6 +258,10 @@ export function tryHeaderLocale(
* @returns The locale that resolved from cookie
*/
export function getCookieLocale(event: H3Event, options: CookieLocaleOptions = {}): Intl.Locale {
warnOnce(
'`getCookieLocale` of `@intlify/utils/h3` is deprecated in v2. Use `getCookieLocale` of `@intlify/utils` instead.'
)

const { lang = DEFAULT_LANG_TAG, name = DEFAULT_COOKIE_NAME } = options
return getLocaleWithGetter(() => getCookie(event, name) || lang)
}
Expand All @@ -251,6 +280,10 @@ export function tryCookieLocale(
event: H3Event,
options: CookieLocaleOptions = {}
): Intl.Locale | null {
warnOnce(
'`tryCookieLocale` of `@intlify/utils/h3` is deprecated in v2. Use `tryCookieLocale` of `@intlify/utils` instead.'
)

try {
return getCookieLocale(event, options)
} catch {
Expand Down Expand Up @@ -285,6 +318,10 @@ export function setCookieLocale(
locale: string | Intl.Locale,
options: CookieOptions = {}
): void {
warnOnce(
'`setCookieLocale` of `@intlify/utils/h3` is deprecated in v2. Use `setCookieLocale` of `@intlify/utils` instead.'
)

const { name = DEFAULT_COOKIE_NAME } = options
validateLocale(locale)
setCookie(event, name, locale.toString(), options)
Expand All @@ -301,6 +338,10 @@ export function setCookieLocale(
* @returns The locale that resolved from path
*/
export function getPathLocale(event: H3Event, options: PathOptions = {}): Intl.Locale {
warnOnce(
'`getPathLocale` of `@intlify/utils/h3` is deprecated in v2. Use `getPathLocale` of `@intlify/utils` instead.'
)

return _getPathLocale(getRequestURL(event), options)
}

Expand All @@ -315,6 +356,10 @@ export function getPathLocale(event: H3Event, options: PathOptions = {}): Intl.L
* @returns The locale that resolved from path. if the language in the path, that is not a well-formed BCP 47 language tag, return `null`.
*/
export function tryPathLocale(event: H3Event, options: PathOptions = {}): Intl.Locale | null {
warnOnce(
'`tryPathLocale` of `@intlify/utils/h3` is deprecated in v2. Use `tryPathLocale` of `@intlify/utils` instead.'
)

try {
return getPathLocale(event, options)
} catch {
Expand All @@ -333,6 +378,10 @@ export function tryPathLocale(event: H3Event, options: PathOptions = {}): Intl.L
* @returns The locale that resolved from query
*/
export function getQueryLocale(event: H3Event, options: QueryOptions = {}): Intl.Locale {
warnOnce(
'`getQueryLocale` of `@intlify/utils/h3` is deprecated in v2. Use `getQueryLocale` of `@intlify/utils` instead.'
)

const { lang = DEFAULT_LANG_TAG, name = 'locale' } = options
return _getQueryLocale(getRequestURL(event), { lang, name })
}
Expand All @@ -348,6 +397,10 @@ export function getQueryLocale(event: H3Event, options: QueryOptions = {}): Intl
* @returns The locale that resolved from query. if the language in the query, that is not a well-formed BCP 47 language tag, return `null`.
*/
export function tryQueryLocale(event: H3Event, options: QueryOptions = {}): Intl.Locale | null {
warnOnce(
'`tryQueryLocale` of `@intlify/utils/h3` is deprecated in v2. Use `tryQueryLocale` of `@intlify/utils` instead.'
)

const { lang = DEFAULT_LANG_TAG, name = 'locale' } = options
try {
return getQueryLocale(event, { lang, name })
Expand Down
52 changes: 52 additions & 0 deletions src/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
parseDefaultHeader,
validateLocale
} from './http.ts'
import { warnOnce } from './utils.ts'

import type { Context } from 'hono'
import type { CookieLocaleOptions, HeaderOptions, PathOptions, QueryOptions } from './http.ts'
Expand Down Expand Up @@ -57,6 +58,10 @@ type CookieOptions = Parameters<typeof setCookie>[3] & { name?: string }
* @returns An array of language tags, if you use `accept-language` header and `*` (any language) or empty string is detected, return an empty array.
*/
export function getHeaderLanguages(context: Context, options: HeaderOptions = {}): string[] {
warnOnce(
'`getHeaderLanguages` of `@intlify/utils/hono` is deprecated in v2. Use `getHeaderLanguages` of `@intlify/utils` instead.'
)

const { name = ACCEPT_LANGUAGE_HEADER } = options
return getHeaderLanguagesWithGetter(() => context.req.header(name), options)
}
Expand Down Expand Up @@ -87,6 +92,10 @@ export function getHeaderLanguages(context: Context, options: HeaderOptions = {}
* @returns A **first language tag** of header, if header is not exists, or `*` (any language), return empty string.
*/
export function getHeaderLanguage(context: Context, options: HeaderOptions = {}): string {
warnOnce(
'`getHeaderLanguage` of `@intlify/utils/hono` is deprecated in v2. Use `getHeaderLanguage` of `@intlify/utils` instead.'
)

return getHeaderLanguages(context, options)[0] || ''
}

Expand Down Expand Up @@ -118,6 +127,10 @@ export function getHeaderLanguage(context: Context, options: HeaderOptions = {})
* @returns Some locales that wrapped from header, if you use `accept-language` header and `*` (any language) or empty string is detected, return an empty array.
*/
export function getHeaderLocales(context: Context, options: HeaderOptions = {}): Intl.Locale[] {
warnOnce(
'`getHeaderLocales` of `@intlify/utils/hono` is deprecated in v2. Use `getHeaderLocales` of `@intlify/utils` instead.'
)

return mapToLocaleFromLanguageTag(getHeaderLanguages, context, options)
}

Expand All @@ -135,6 +148,9 @@ export function tryHeaderLocales(
context: Context,
options: HeaderOptions = {}
): Intl.Locale[] | null {
warnOnce(
'`tryHeaderLocales` of `@intlify/utils/hono` is deprecated in v2. Use `tryHeaderLocales` of `@intlify/utils` instead.'
)
try {
return getHeaderLocales(context, options)
} catch {
Expand Down Expand Up @@ -173,6 +189,10 @@ export function getHeaderLocale(
context: Context,
options: HeaderOptions & { lang?: string } = {}
): Intl.Locale {
warnOnce(
'`getHeaderLocale` of `@intlify/utils/hono` is deprecated in v2. Use `getHeaderLocale` of `@intlify/utils` instead.'
)

const {
lang = DEFAULT_LANG_TAG,
name = ACCEPT_LANGUAGE_HEADER,
Expand All @@ -195,6 +215,10 @@ export function tryHeaderLocale(
context: Context,
options: HeaderOptions & { lang?: string } = {}
): Intl.Locale | null {
warnOnce(
'`tryHeaderLocale` of `@intlify/utils/hono` is deprecated in v2. Use `tryHeaderLocale` of `@intlify/utils` instead.'
)

try {
return getHeaderLocale(context, options)
} catch {
Expand Down Expand Up @@ -228,6 +252,10 @@ export function tryHeaderLocale(
* @returns The locale that resolved from cookie
*/
export function getCookieLocale(context: Context, options: CookieLocaleOptions = {}): Intl.Locale {
warnOnce(
'`getCookieLocale` of `@intlify/utils/hono` is deprecated in v2. Use `getCookieLocale` of `@intlify/utils` instead.'
)

const { lang = DEFAULT_LANG_TAG, name = DEFAULT_COOKIE_NAME } = options
return getLocaleWithGetter(() => getCookie(context, name) || lang)
}
Expand All @@ -246,6 +274,10 @@ export function tryCookieLocale(
context: Context,
options: CookieLocaleOptions = {}
): Intl.Locale | null {
warnOnce(
'`tryCookieLocale` of `@intlify/utils/hono` is deprecated in v2. Use `tryCookieLocale` of `@intlify/utils` instead.'
)

try {
return getCookieLocale(context, options)
} catch {
Expand Down Expand Up @@ -281,6 +313,10 @@ export function setCookieLocale(
locale: string | Intl.Locale,
options: CookieOptions = {}
): void {
warnOnce(
'`setCookieLocale` of `@intlify/utils/hono` is deprecated in v2. Use `setCookieLocale` of `@intlify/utils` instead.'
)

const { name = DEFAULT_COOKIE_NAME } = options
validateLocale(locale)
setCookie(context, name, locale.toString(), options)
Expand All @@ -297,6 +333,10 @@ export function setCookieLocale(
* @returns The locale that resolved from path
*/
export function getPathLocale(context: Context, options: PathOptions = {}): Intl.Locale {
warnOnce(
'`getPathLocale` of `@intlify/utils/hono` is deprecated in v2. Use `getPathLocale` of `@intlify/utils` instead.'
)

return _getPathLocale(new URL(context.req.url), options)
}

Expand All @@ -311,6 +351,10 @@ export function getPathLocale(context: Context, options: PathOptions = {}): Intl
* @returns The locale that resolved from path. if the language in the path, that is not a well-formed BCP 47 language tag, return `null`.
*/
export function tryPathLocale(context: Context, options: PathOptions = {}): Intl.Locale | null {
warnOnce(
'`tryPathLocale` of `@intlify/utils/hono` is deprecated in v2. Use `tryPathLocale` of `@intlify/utils` instead.'
)

try {
return getPathLocale(context, options)
} catch {
Expand All @@ -329,6 +373,10 @@ export function tryPathLocale(context: Context, options: PathOptions = {}): Intl
* @returns The locale that resolved from query
*/
export function getQueryLocale(context: Context, options: QueryOptions = {}): Intl.Locale {
warnOnce(
'`getQueryLocale` of `@intlify/utils/hono` is deprecated in v2. Use `getQueryLocale` of `@intlify/utils` instead.'
)

const { lang = DEFAULT_LANG_TAG, name = 'locale' } = options
return _getQueryLocale(new URL(context.req.url), { lang, name })
}
Expand All @@ -344,6 +392,10 @@ export function getQueryLocale(context: Context, options: QueryOptions = {}): In
* @returns The locale that resolved from query. if the language in the query, that is not a well-formed BCP 47 language tag, return `null`.
*/
export function tryQueryLocale(context: Context, options: QueryOptions = {}): Intl.Locale | null {
warnOnce(
'`tryQueryLocale` of `@intlify/utils/hono` is deprecated in v2. Use `tryQueryLocale` of `@intlify/utils` instead.'
)

const { lang = DEFAULT_LANG_TAG, name = 'locale' } = options
try {
return getQueryLocale(context, { lang, name })
Expand Down
28 changes: 28 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @author kazuya kawaguchi (a.k.a. kazupon)
* @license MIT
*/

function warn(msg: string, err?: Error): void {
if (typeof console !== 'undefined') {
console.warn(`[intlify/utils] ` + msg)
/* istanbul ignore if */
if (err) {
console.warn(err.stack)
}
}
}

const hasWarned: Record<string, boolean> = {}

/**
* Warns a message only once.
*
* @param msg - The warning message
*/
export function warnOnce(msg: string): void {
if (!hasWarned[msg]) {
hasWarned[msg] = true
warn(msg)
}
}
Loading