From be78fff991f6255ec2556acbe382691beb853d46 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Sat, 15 Nov 2025 03:20:24 +0900 Subject: [PATCH] fix: utilities exporting --- packages/h3/docs/functions/getCookieLocale.md | 21 ++++++------- .../h3/docs/functions/getHeaderLanguage.md | 20 ++++++------ .../h3/docs/functions/getHeaderLanguages.md | 20 ++++++------ packages/h3/docs/functions/getHeaderLocale.md | 26 +++++++++------- .../h3/docs/functions/getHeaderLocales.md | 26 +++++++++------- packages/h3/docs/functions/getPathLocale.md | 8 ++--- packages/h3/docs/functions/getQueryLocale.md | 8 ++--- packages/h3/docs/functions/setCookieLocale.md | 26 +++++++++------- packages/h3/docs/functions/tryCookieLocale.md | 10 +++--- packages/h3/docs/functions/tryHeaderLocale.md | 8 ++--- .../h3/docs/functions/tryHeaderLocales.md | 6 ++-- packages/h3/docs/functions/tryPathLocale.md | 8 ++--- packages/h3/docs/functions/tryQueryLocale.md | 8 ++--- packages/h3/src/index.ts | 16 +++++++++- .../hono/docs/functions/getCookieLocale.md | 22 ++++++------- .../hono/docs/functions/getHeaderLanguage.md | 22 ++++++------- .../hono/docs/functions/getHeaderLanguages.md | 22 ++++++------- .../hono/docs/functions/getHeaderLocale.md | 31 ++++++++++--------- .../hono/docs/functions/getHeaderLocales.md | 31 +++++++++++-------- packages/hono/docs/functions/getPathLocale.md | 8 ++--- .../hono/docs/functions/getQueryLocale.md | 8 ++--- .../hono/docs/functions/setCookieLocale.md | 29 +++++++++-------- .../hono/docs/functions/tryCookieLocale.md | 30 ++++++++++++++++++ .../hono/docs/functions/tryHeaderLocale.md | 30 ++++++++++++++++++ .../hono/docs/functions/tryHeaderLocales.md | 30 ++++++++++++++++++ packages/hono/docs/functions/tryPathLocale.md | 30 ++++++++++++++++++ .../hono/docs/functions/tryQueryLocale.md | 30 ++++++++++++++++++ packages/hono/docs/index.md | 5 +++ packages/hono/src/index.ts | 16 +++++++++- 29 files changed, 375 insertions(+), 180 deletions(-) create mode 100644 packages/hono/docs/functions/tryCookieLocale.md create mode 100644 packages/hono/docs/functions/tryHeaderLocale.md create mode 100644 packages/hono/docs/functions/tryHeaderLocales.md create mode 100644 packages/hono/docs/functions/tryPathLocale.md create mode 100644 packages/hono/docs/functions/tryQueryLocale.md diff --git a/packages/h3/docs/functions/getCookieLocale.md b/packages/h3/docs/functions/getCookieLocale.md index aebecee..e0996f6 100644 --- a/packages/h3/docs/functions/getCookieLocale.md +++ b/packages/h3/docs/functions/getCookieLocale.md @@ -7,7 +7,7 @@ # Function: getCookieLocale() ```ts -function getCookieLocale(event, __namedParameters?): Locale; +function getCookieLocale(request, options?): Locale; ``` get locale from cookie @@ -16,10 +16,8 @@ get locale from cookie | Parameter | Type | Description | | ------ | ------ | ------ | -| `event` | `H3Event` | The H3Event \| H3 event | -| `__namedParameters?` | \{ `lang?`: `string`; `name?`: `string`; \} | - | -| `__namedParameters.lang?` | `string` | - | -| `__namedParameters.name?` | `string` | - | +| `request` | `Request` | The Request \| request | +| `options?` | `CookieLocaleOptions` | The CookieLocaleOptions \| cookie locale options, `lang` option is `en-US` as default, you must specify the language tag with the [BCP 47 syntax](https://datatracker.ietf.org/doc/html/rfc4646#section-2.1). `name` option is `i18n_locale` as default. | ## Returns @@ -29,14 +27,15 @@ The locale that resolved from cookie ## Example -example for h3: +example for Web API request on Deno: ```ts -import { createApp, eventHandler } from 'h3' -import { getCookieLocale } from '@intlify/utils/h3' +import { getCookieLocale } from 'https://esm.sh/@intlify/utils/web' -app.use(eventHandler(event) => { - const locale = getCookieLocale(event) +Deno.serve({ + port: 8080, +}, (req) => { + const locale = getCookieLocale(req) console.log(locale) // output `Intl.Locale` instance // ... }) @@ -44,4 +43,4 @@ app.use(eventHandler(event) => { ## Throws -Throws a RangeError if `lang` option or cookie name value are not a well-formed BCP 47 language tag. +Throws a `RangeError` if `lang` option or cookie name value are not a well-formed BCP 47 language tag. diff --git a/packages/h3/docs/functions/getHeaderLanguage.md b/packages/h3/docs/functions/getHeaderLanguage.md index 49edaba..cb5c054 100644 --- a/packages/h3/docs/functions/getHeaderLanguage.md +++ b/packages/h3/docs/functions/getHeaderLanguage.md @@ -7,7 +7,7 @@ # Function: getHeaderLanguage() ```ts -function getHeaderLanguage(event, __namedParameters?): string; +function getHeaderLanguage(request, options?): string; ``` get language from header @@ -16,8 +16,8 @@ get language from header | Parameter | Type | Description | | ------ | ------ | ------ | -| `event` | `H3Event` | The H3Event \| H3 event | -| `__namedParameters?` | `HeaderOptions` | - | +| `request` | `Request` | The Request \| request | +| `options?` | `HeaderOptions` | The HeaderOptions \| header options object | ## Returns @@ -31,16 +31,16 @@ parse header string, default `accept-language`. if you use `accept-language`, th ## Example -example for h3: +example for Web API request on Deno: ```ts -import { createApp, eventHandler } from 'h3' -import { getAcceptLanguage } from '@intlify/utils/h3' +import { getAcceptLanguage } from 'https://esm.sh/@intlify/utils/web' -const app = createApp() -app.use(eventHandler(event) => { - const langTag = getHeaderLanguage(event) +Deno.serve({ + port: 8080, +}, (req) => { + const langTag = getHeaderLanguage(req) // ... - return `accepted language: ${langTag}` + return new Response(`accepted language: ${langTag}` }) ``` diff --git a/packages/h3/docs/functions/getHeaderLanguages.md b/packages/h3/docs/functions/getHeaderLanguages.md index 1f78638..7a718c5 100644 --- a/packages/h3/docs/functions/getHeaderLanguages.md +++ b/packages/h3/docs/functions/getHeaderLanguages.md @@ -7,7 +7,7 @@ # Function: getHeaderLanguages() ```ts -function getHeaderLanguages(event, __namedParameters?): string[]; +function getHeaderLanguages(request, options?): string[]; ``` get languages from header @@ -16,8 +16,8 @@ get languages from header | Parameter | Type | Description | | ------ | ------ | ------ | -| `event` | `H3Event` | The H3Event \| H3 event | -| `__namedParameters?` | `HeaderOptions` | - | +| `request` | `Request` | The Request \| request | +| `options?` | `HeaderOptions` | The HeaderOptions \| header options object. `name` option is `accept-language` as default. | ## Returns @@ -31,16 +31,16 @@ parse header string, default `accept-language` header ## Example -example for h3: +example for Web API request on Deno: ```ts -import { createApp, eventHandler } from 'h3' -import { getHeaderLanguages } from '@intlify/utils/h3' +import { getHeaderLanguages } from 'https://esm.sh/@intlify/utils/web' -const app = createApp() -app.use(eventHandler(event) => { - const langTags = getHeaderLanguages(event) +Deno.serve({ + port: 8080, +}, (req) => { + const langTags = getHeaderLanguages(req) // ... - return `accepted languages: ${acceptLanguages.join(', ')}` + return new Response(`accepted languages: ${langTags.join(', ')}` }) ``` diff --git a/packages/h3/docs/functions/getHeaderLocale.md b/packages/h3/docs/functions/getHeaderLocale.md index 58a555f..f7a256e 100644 --- a/packages/h3/docs/functions/getHeaderLocale.md +++ b/packages/h3/docs/functions/getHeaderLocale.md @@ -7,7 +7,7 @@ # Function: getHeaderLocale() ```ts -function getHeaderLocale(event, __namedParameters?): Locale; +function getHeaderLocale(request, options?): Locale; ``` get locale from header @@ -16,8 +16,8 @@ get locale from header | Parameter | Type | Description | | ------ | ------ | ------ | -| `event` | `H3Event` | The H3Event \| H3 event | -| `__namedParameters?` | `HeaderOptions` & `object` | - | +| `request` | `Request` | The Request \| request | +| `options?` | `HeaderOptions` & `object` | The HeaderOptions \| header options object. `lang` option is `en-US` as default, you must specify the language tag with the [BCP 47 syntax](https://datatracker.ietf.org/doc/html/rfc4646#section-2.1). `name` option is `accept-language` as default, and `parser` option is parseDefaultHeader as default. | ## Returns @@ -31,19 +31,21 @@ wrap language tag with Intl.Locale \| locale, languages tags will be parsed from ## Example -example for h3: - ```ts -import { createApp, eventHandler } from 'h3' -import { getHeaderLocale } from '@intlify/utils/h3' +example for Web API request on Bun: + +import { getHeaderLocale } from '@intlify/utils/web' -app.use(eventHandler(event) => { - const locale = getHeaderLocale(event) - // ... - return `accepted locale: ${locale.toString()}` +Bun.serve({ + port: 8080, + fetch(req) { + const locale = getHeaderLocale(req) + // ... + return new Response(`accpected locale: ${locale.toString()}`) + }, }) ``` ## Throws -Throws the RangeError if `lang` option or header are not a well-formed BCP 47 language tag. +Throws the `RangeError` if `lang` option or header are not a well-formed BCP 47 language tag. diff --git a/packages/h3/docs/functions/getHeaderLocales.md b/packages/h3/docs/functions/getHeaderLocales.md index 2c25a33..85e4b8d 100644 --- a/packages/h3/docs/functions/getHeaderLocales.md +++ b/packages/h3/docs/functions/getHeaderLocales.md @@ -7,7 +7,7 @@ # Function: getHeaderLocales() ```ts -function getHeaderLocales(event, __namedParameters?): Locale[]; +function getHeaderLocales(request, options?): Locale[]; ``` get locales from header @@ -16,8 +16,8 @@ get locales from header | Parameter | Type | Description | | ------ | ------ | ------ | -| `event` | `H3Event` | The H3Event \| H3 event | -| `__namedParameters?` | `HeaderOptions` | - | +| `request` | `Request` | The Request \| request | +| `options?` | `HeaderOptions` | The HeaderOptions \| header options object | ## Returns @@ -31,19 +31,21 @@ wrap language tags with Intl.Locale \| locale, languages tags will be parsed fro ## Example -example for h3: +example for Web API request on Bun: ```ts -import { createApp, eventHandler } from 'h3' -import { getHeaderLocales } from '@intlify/utils/h3' - -app.use(eventHandler(event) => { - const locales = getHeaderLocales(event) - // ... - return `accepted locales: ${locales.map(locale => locale.toString()).join(', ')}` +import { getHeaderLocales } from '@intlify/utils/web' + +Bun.serve({ + port: 8080, + fetch(req) { + const locales = getHeaderLocales(req) + // ... + return new Response(`accpected locales: ${locales.map(locale => locale.toString()).join(', ')}`) + }, }) ``` ## Throws -Throws the RangeError if header are not a well-formed BCP 47 language tag. +Throws the `RangeError` if header are not a well-formed BCP 47 language tag. diff --git a/packages/h3/docs/functions/getPathLocale.md b/packages/h3/docs/functions/getPathLocale.md index 640608c..375c7cc 100644 --- a/packages/h3/docs/functions/getPathLocale.md +++ b/packages/h3/docs/functions/getPathLocale.md @@ -7,7 +7,7 @@ # Function: getPathLocale() ```ts -function getPathLocale(event, __namedParameters?): Locale; +function getPathLocale(request, options?): Locale; ``` get the locale from the path @@ -16,8 +16,8 @@ get the locale from the path | Parameter | Type | Description | | ------ | ------ | ------ | -| `event` | `H3Event` | the H3Event \| H3 event | -| `__namedParameters?` | `PathOptions` | - | +| `request` | `Request` | the Request \| request | +| `options?` | `PathOptions` | the PathOptions \| path options object | ## Returns @@ -27,4 +27,4 @@ The locale that resolved from path ## Throws -Throws the RangeError if the language in the path, that is not a well-formed BCP 47 language tag. +Throws the `RangeError` if the language in the path, that is not a well-formed BCP 47 language tag. diff --git a/packages/h3/docs/functions/getQueryLocale.md b/packages/h3/docs/functions/getQueryLocale.md index 298d70a..0a62132 100644 --- a/packages/h3/docs/functions/getQueryLocale.md +++ b/packages/h3/docs/functions/getQueryLocale.md @@ -7,7 +7,7 @@ # Function: getQueryLocale() ```ts -function getQueryLocale(event, __namedParameters?): Locale; +function getQueryLocale(request, options?): Locale; ``` get the locale from the query @@ -16,8 +16,8 @@ get the locale from the query | Parameter | Type | Description | | ------ | ------ | ------ | -| `event` | `H3Event` | the H3Event \| H3 event | -| `__namedParameters?` | `QueryOptions` | - | +| `request` | `Request` | the Request \| request | +| `options?` | `QueryOptions` | The QueryOptions \| query options, `lang` option is `en-US` as default, `name` option is `locale` as default. | ## Returns @@ -27,4 +27,4 @@ The locale that resolved from query ## Throws -Throws the RangeError if the language in the query, that is not a well-formed BCP 47 language tag. +Throws the `RangeError` if the language in the query, that is not a well-formed BCP 47 language tag. diff --git a/packages/h3/docs/functions/setCookieLocale.md b/packages/h3/docs/functions/setCookieLocale.md index 6c3ecbc..afe029a 100644 --- a/packages/h3/docs/functions/setCookieLocale.md +++ b/packages/h3/docs/functions/setCookieLocale.md @@ -8,7 +8,7 @@ ```ts function setCookieLocale( - event, + response, locale, options?): void; ``` @@ -19,9 +19,9 @@ set locale to the response `Set-Cookie` header. | Parameter | Type | Description | | ------ | ------ | ------ | -| `event` | `H3Event` | The H3Event \| H3 event | +| `response` | `Response` | The Response \| response | | `locale` | `string` \| `Locale` | The locale value | -| `options?` | `CookieOptions` | The cookie options, `name` option is `i18n_locale` as default, and `path` option is `/` as default. | +| `options?` | `CookieOptions` | The CookieOptions \| cookie options, `name` option is `i18n_locale` as default | ## Returns @@ -29,18 +29,22 @@ set locale to the response `Set-Cookie` header. ## Example -example for h3: +example for Web API response on Bun: ```ts -import { createApp, eventHandler } from 'h3' -import { getCookieLocale } from '@intlify/utils/h3' - -app.use(eventHandler(event) => { - setCookieLocale(event, 'ja-JP') - // ... +import { setCookieLocale } from '@intlify/utils/web' + +Bun.serve({ + port: 8080, + fetch(req) { + const res = new Response('こんにちは、世界!') + setCookieLocale(res, 'ja-JP') + // ... + return res + }, }) ``` ## Throws -Throws the SyntaxError if `locale` is invalid. +Throws the `SyntaxError` if `locale` is invalid. diff --git a/packages/h3/docs/functions/tryCookieLocale.md b/packages/h3/docs/functions/tryCookieLocale.md index a2a4c6a..44a44af 100644 --- a/packages/h3/docs/functions/tryCookieLocale.md +++ b/packages/h3/docs/functions/tryCookieLocale.md @@ -7,7 +7,7 @@ # Function: tryCookieLocale() ```ts -function tryCookieLocale(event, __namedParameters?): Locale | null; +function tryCookieLocale(request, options?): Locale | null; ``` try to get locale from cookie @@ -16,10 +16,8 @@ try to get locale from cookie | Parameter | Type | Description | | ------ | ------ | ------ | -| `event` | `H3Event` | The H3Event \| H3 event | -| `__namedParameters?` | \{ `lang?`: `string`; `name?`: `string`; \} | - | -| `__namedParameters.lang?` | `string` | - | -| `__namedParameters.name?` | `string` | - | +| `request` | `Request` | The Request \| request | +| `options?` | `CookieLocaleOptions` | The CookieLocaleOptions \| cookie locale options | ## Returns @@ -29,4 +27,4 @@ The locale that resolved from cookie. if `lang` option or cookie name value are ## Description -Unlike [getCookieLocale](getCookieLocale.md), this function does not throw an error if the locale cannot be obtained, this function returns `null`. +Unlike [`getCookieLocale`](getCookieLocale.md), this function does not throw an error if the locale cannot be obtained, this function returns `null`. diff --git a/packages/h3/docs/functions/tryHeaderLocale.md b/packages/h3/docs/functions/tryHeaderLocale.md index 01995e2..dee76f7 100644 --- a/packages/h3/docs/functions/tryHeaderLocale.md +++ b/packages/h3/docs/functions/tryHeaderLocale.md @@ -7,7 +7,7 @@ # Function: tryHeaderLocale() ```ts -function tryHeaderLocale(event, __namedParameters?): Locale | null; +function tryHeaderLocale(request, options?): Locale | null; ``` try to get locale from header @@ -16,14 +16,14 @@ try to get locale from header | Parameter | Type | Description | | ------ | ------ | ------ | -| `event` | `H3Event` | The H3Event \| H3 event | -| `__namedParameters?` | `HeaderOptions` & `object` | - | +| `request` | `Request` | The Request \| request | +| `options?` | `HeaderOptions` & `object` | The HeaderOptions \| header options object | ## Returns `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 header are not a well-formed BCP 47 language tag, return `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`. ## Description diff --git a/packages/h3/docs/functions/tryHeaderLocales.md b/packages/h3/docs/functions/tryHeaderLocales.md index c6dbb70..f079f0d 100644 --- a/packages/h3/docs/functions/tryHeaderLocales.md +++ b/packages/h3/docs/functions/tryHeaderLocales.md @@ -7,7 +7,7 @@ # Function: tryHeaderLocales() ```ts -function tryHeaderLocales(event, __namedParameters?): Locale[] | null; +function tryHeaderLocales(request, options?): Locale[] | null; ``` try to get locales from header @@ -16,8 +16,8 @@ try to get locales from header | Parameter | Type | Description | | ------ | ------ | ------ | -| `event` | `H3Event` | The H3Event \| H3 event | -| `__namedParameters?` | `HeaderOptions` | - | +| `request` | `Request` | The Request \| request | +| `options?` | `HeaderOptions` | The HeaderOptions \| header options object | ## Returns diff --git a/packages/h3/docs/functions/tryPathLocale.md b/packages/h3/docs/functions/tryPathLocale.md index 6b90de8..fa8a1bc 100644 --- a/packages/h3/docs/functions/tryPathLocale.md +++ b/packages/h3/docs/functions/tryPathLocale.md @@ -7,7 +7,7 @@ # Function: tryPathLocale() ```ts -function tryPathLocale(event, __namedParameters?): Locale | null; +function tryPathLocale(request, options?): Locale | null; ``` try to get the locale from the path @@ -16,8 +16,8 @@ try to get the locale from the path | Parameter | Type | Description | | ------ | ------ | ------ | -| `event` | `H3Event` | the H3Event \| H3 event | -| `__namedParameters?` | `PathOptions` | - | +| `request` | `Request` | the Request \| request | +| `options?` | `PathOptions` | the PathOptions \| path options object | ## Returns @@ -27,4 +27,4 @@ The locale that resolved from path. if the language in the path, that is not a w ## Description -Unlike [getPathLocale](getPathLocale.md), this function does not throw an error if the locale cannot be obtained, this function returns `null`. +Unlike [`getPathLocale`](getPathLocale.md), this function does not throw an error if the locale cannot be obtained, this function returns `null`. diff --git a/packages/h3/docs/functions/tryQueryLocale.md b/packages/h3/docs/functions/tryQueryLocale.md index e496761..4de9ea4 100644 --- a/packages/h3/docs/functions/tryQueryLocale.md +++ b/packages/h3/docs/functions/tryQueryLocale.md @@ -7,7 +7,7 @@ # Function: tryQueryLocale() ```ts -function tryQueryLocale(event, __namedParameters?): Locale | null; +function tryQueryLocale(request, options?): Locale | null; ``` try to get the locale from the query @@ -16,8 +16,8 @@ try to get the locale from the query | Parameter | Type | Description | | ------ | ------ | ------ | -| `event` | `H3Event` | the H3Event \| H3 event | -| `__namedParameters?` | `QueryOptions` | - | +| `request` | `Request` | the Request \| request | +| `options?` | `QueryOptions` | The QueryOptions \| query options, `lang` option is `en-US` as default, `name` option is `locale` as default. | ## Returns @@ -27,4 +27,4 @@ The locale that resolved from query. if the language in the query, that is not a ## Description -Unlike [getQueryLocale](getQueryLocale.md), this function does not throw an error if the locale cannot be obtained, this function returns `null`. +Unlike [`getQueryLocale`](getQueryLocale.md), this function does not throw an error if the locale cannot be obtained, this function returns `null`. diff --git a/packages/h3/src/index.ts b/packages/h3/src/index.ts index 3791314..27e538b 100644 --- a/packages/h3/src/index.ts +++ b/packages/h3/src/index.ts @@ -19,7 +19,21 @@ import { import { getHeaderLocale } from '@intlify/utils' import { toWebRequest } from 'h3' -export * from '@intlify/utils/h3' +export { + getCookieLocale, + getHeaderLanguage, + getHeaderLanguages, + getHeaderLocale, + getHeaderLocales, + getPathLocale, + getQueryLocale, + setCookieLocale, + tryCookieLocale, + tryHeaderLocale, + tryHeaderLocales, + tryPathLocale, + tryQueryLocale +} from '@intlify/utils' export type { CoreContext } from '@intlify/core' diff --git a/packages/hono/docs/functions/getCookieLocale.md b/packages/hono/docs/functions/getCookieLocale.md index 345b288..1f91f23 100644 --- a/packages/hono/docs/functions/getCookieLocale.md +++ b/packages/hono/docs/functions/getCookieLocale.md @@ -7,7 +7,7 @@ # Function: getCookieLocale() ```ts -function getCookieLocale(context, __namedParameters?): Locale; +function getCookieLocale(request, options?): Locale; ``` get locale from cookie @@ -16,10 +16,8 @@ get locale from cookie | Parameter | Type | Description | | ------ | ------ | ------ | -| `context` | `Context` | A Context \| Hono context | -| `__namedParameters?` | \{ `lang?`: `string`; `name?`: `string`; \} | - | -| `__namedParameters.lang?` | `string` | - | -| `__namedParameters.name?` | `string` | - | +| `request` | `Request` | The Request \| request | +| `options?` | `CookieLocaleOptions` | The CookieLocaleOptions \| cookie locale options, `lang` option is `en-US` as default, you must specify the language tag with the [BCP 47 syntax](https://datatracker.ietf.org/doc/html/rfc4646#section-2.1). `name` option is `i18n_locale` as default. | ## Returns @@ -29,15 +27,15 @@ The locale that resolved from cookie ## Example -example for Hono: +example for Web API request on Deno: ```ts -import { Hono } from 'hono' -import { getCookieLocale } from '@intlify/utils/hono' +import { getCookieLocale } from 'https://esm.sh/@intlify/utils/web' -const app = new Hono() -app.use('/', c => { - const locale = getCookieLocale(c) +Deno.serve({ + port: 8080, +}, (req) => { + const locale = getCookieLocale(req) console.log(locale) // output `Intl.Locale` instance // ... }) @@ -45,4 +43,4 @@ app.use('/', c => { ## Throws -Throws a RangeError if `lang` option or cookie name value are not a well-formed BCP 47 language tag. +Throws a `RangeError` if `lang` option or cookie name value are not a well-formed BCP 47 language tag. diff --git a/packages/hono/docs/functions/getHeaderLanguage.md b/packages/hono/docs/functions/getHeaderLanguage.md index caf52ee..e44605b 100644 --- a/packages/hono/docs/functions/getHeaderLanguage.md +++ b/packages/hono/docs/functions/getHeaderLanguage.md @@ -7,7 +7,7 @@ # Function: getHeaderLanguage() ```ts -function getHeaderLanguage(context, __namedParameters?): string; +function getHeaderLanguage(request, options?): string; ``` get language from header @@ -16,14 +16,14 @@ get language from header | Parameter | Type | Description | | ------ | ------ | ------ | -| `context` | `Context` | A Context \| Hono context | -| `__namedParameters?` | `HeaderOptions` | - | +| `request` | `Request` | The Request \| request | +| `options?` | `HeaderOptions` | The HeaderOptions \| header options object | ## Returns `string` -A **first language tag** of header, if header is not exists, or `*` (any language), return empty string. +The **first language tag** of header, if header is not exists, or `*` (any language), return empty string. ## Description @@ -31,16 +31,16 @@ parse header string, default `accept-language`. if you use `accept-language`, th ## Example -example for Hone: +example for Web API request on Deno: ```ts -import { Hono } from 'hono' -import { getHeaderLanguage } from '@intlify/utils/hono' +import { getAcceptLanguage } from 'https://esm.sh/@intlify/utils/web' -const app = new Hono() -app.use('/', c => { - const langTag = getHeaderLanguage(c) +Deno.serve({ + port: 8080, +}, (req) => { + const langTag = getHeaderLanguage(req) // ... - return c.text(`accepted language: ${langTag}`) + return new Response(`accepted language: ${langTag}` }) ``` diff --git a/packages/hono/docs/functions/getHeaderLanguages.md b/packages/hono/docs/functions/getHeaderLanguages.md index 4f75671..76edfd6 100644 --- a/packages/hono/docs/functions/getHeaderLanguages.md +++ b/packages/hono/docs/functions/getHeaderLanguages.md @@ -7,7 +7,7 @@ # Function: getHeaderLanguages() ```ts -function getHeaderLanguages(context, __namedParameters?): string[]; +function getHeaderLanguages(request, options?): string[]; ``` get languages from header @@ -16,14 +16,14 @@ get languages from header | Parameter | Type | Description | | ------ | ------ | ------ | -| `context` | `Context` | A Context \| Hono context | -| `__namedParameters?` | `HeaderOptions` | - | +| `request` | `Request` | The Request \| request | +| `options?` | `HeaderOptions` | The HeaderOptions \| header options object. `name` option is `accept-language` as default. | ## Returns `string`[] -An array of language tags, if you use `accept-language` header and `*` (any language) or empty string is detected, return an empty array. +The array of language tags, if you use `accept-language` header and `*` (any language) or empty string is detected, return an empty array. ## Description @@ -31,16 +31,16 @@ parse header string, default `accept-language` header ## Example -example for Hono +example for Web API request on Deno: ```ts -import { Hono } from 'hono' -import { getHeaderLanguages } from '@intlify/utils/hono' +import { getHeaderLanguages } from 'https://esm.sh/@intlify/utils/web' -const app = new Hono() -app.use('/', c => { - const langTags = getHeaderLanguages(c) +Deno.serve({ + port: 8080, +}, (req) => { + const langTags = getHeaderLanguages(req) // ... - return c.text(`accepted languages: ${acceptLanguages.join(', ')}`) + return new Response(`accepted languages: ${langTags.join(', ')}` }) ``` diff --git a/packages/hono/docs/functions/getHeaderLocale.md b/packages/hono/docs/functions/getHeaderLocale.md index e476fcb..c902d45 100644 --- a/packages/hono/docs/functions/getHeaderLocale.md +++ b/packages/hono/docs/functions/getHeaderLocale.md @@ -7,7 +7,7 @@ # Function: getHeaderLocale() ```ts -function getHeaderLocale(context, __namedParameters?): Locale; +function getHeaderLocale(request, options?): Locale; ``` get locale from header @@ -16,14 +16,14 @@ get locale from header | Parameter | Type | Description | | ------ | ------ | ------ | -| `context` | `Context` | A Context \| Hono context | -| `__namedParameters?` | `HeaderOptions` & `object` | - | +| `request` | `Request` | The Request \| request | +| `options?` | `HeaderOptions` & `object` | The HeaderOptions \| header options object. `lang` option is `en-US` as default, you must specify the language tag with the [BCP 47 syntax](https://datatracker.ietf.org/doc/html/rfc4646#section-2.1). `name` option is `accept-language` as default, and `parser` option is parseDefaultHeader as default. | ## Returns `Locale` -A first locale that resolved from header string. if you use `accept-language` header and `*` (any language) or empty string is detected, return `en-US`. +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`. ## Description @@ -31,20 +31,21 @@ wrap language tag with Intl.Locale \| locale, languages tags will be parsed from ## Example -example for Hono: - ```ts -import { Hono } from 'hono' -import { getHeaderLocale } from '@intlify/utils/hono' - -const app = new Hono() -app.use('/', c => { - const locale = getHeaderLocale(c) - // ... - return c.text(`accepted language: ${locale.toString()}`) +example for Web API request on Bun: + +import { getHeaderLocale } from '@intlify/utils/web' + +Bun.serve({ + port: 8080, + fetch(req) { + const locale = getHeaderLocale(req) + // ... + return new Response(`accpected locale: ${locale.toString()}`) + }, }) ``` ## Throws -Throws the RangeError if `lang` option or header are not a well-formed BCP 47 language tag. +Throws the `RangeError` if `lang` option or header are not a well-formed BCP 47 language tag. diff --git a/packages/hono/docs/functions/getHeaderLocales.md b/packages/hono/docs/functions/getHeaderLocales.md index 90bfefc..5526b3f 100644 --- a/packages/hono/docs/functions/getHeaderLocales.md +++ b/packages/hono/docs/functions/getHeaderLocales.md @@ -7,7 +7,7 @@ # Function: getHeaderLocales() ```ts -function getHeaderLocales(context, __namedParameters?): Locale[]; +function getHeaderLocales(request, options?): Locale[]; ``` get locales from header @@ -16,14 +16,14 @@ get locales from header | Parameter | Type | Description | | ------ | ------ | ------ | -| `context` | `Context` | A Context \| Hono context | -| `__namedParameters?` | `HeaderOptions` | - | +| `request` | `Request` | The Request \| request | +| `options?` | `HeaderOptions` | The HeaderOptions \| header options object | ## Returns `Locale`[] -Some locales that wrapped from header, if you use `accept-language` header and `*` (any language) or empty string is detected, return an empty array. +The locales that wrapped from header, if you use `accept-language` header and `*` (any language) or empty string is detected, return an empty array. ## Description @@ -31,16 +31,21 @@ wrap language tags with Intl.Locale \| locale, languages tags will be parsed fro ## Example -example for Hono: +example for Web API request on Bun: ```ts -import { Hono } from 'hono' -import { getHeaderLocales } from '@intlify/utils/hono' - -const app = new Hono() -app.use('/', c => { - const locales = getHeaderLocales(c) - // ... - return c.text(`accepted locales: ${locales.map(locale => locale.toString()).join(', ')}`) +import { getHeaderLocales } from '@intlify/utils/web' + +Bun.serve({ + port: 8080, + fetch(req) { + const locales = getHeaderLocales(req) + // ... + return new Response(`accpected locales: ${locales.map(locale => locale.toString()).join(', ')}`) + }, }) ``` + +## Throws + +Throws the `RangeError` if header are not a well-formed BCP 47 language tag. diff --git a/packages/hono/docs/functions/getPathLocale.md b/packages/hono/docs/functions/getPathLocale.md index 9c6a91f..b6c6577 100644 --- a/packages/hono/docs/functions/getPathLocale.md +++ b/packages/hono/docs/functions/getPathLocale.md @@ -7,7 +7,7 @@ # Function: getPathLocale() ```ts -function getPathLocale(context, __namedParameters?): Locale; +function getPathLocale(request, options?): Locale; ``` get the locale from the path @@ -16,8 +16,8 @@ get the locale from the path | Parameter | Type | Description | | ------ | ------ | ------ | -| `context` | `Context` | A Context \| Hono context | -| `__namedParameters?` | `PathOptions` | - | +| `request` | `Request` | the Request \| request | +| `options?` | `PathOptions` | the PathOptions \| path options object | ## Returns @@ -27,4 +27,4 @@ The locale that resolved from path ## Throws -Throws the RangeError if the language in the path, that is not a well-formed BCP 47 language tag. +Throws the `RangeError` if the language in the path, that is not a well-formed BCP 47 language tag. diff --git a/packages/hono/docs/functions/getQueryLocale.md b/packages/hono/docs/functions/getQueryLocale.md index ca0978d..bc5c447 100644 --- a/packages/hono/docs/functions/getQueryLocale.md +++ b/packages/hono/docs/functions/getQueryLocale.md @@ -7,7 +7,7 @@ # Function: getQueryLocale() ```ts -function getQueryLocale(context, __namedParameters?): Locale; +function getQueryLocale(request, options?): Locale; ``` get the locale from the query @@ -16,8 +16,8 @@ get the locale from the query | Parameter | Type | Description | | ------ | ------ | ------ | -| `context` | `Context` | A Context \| Hono context | -| `__namedParameters?` | `QueryOptions` | - | +| `request` | `Request` | the Request \| request | +| `options?` | `QueryOptions` | The QueryOptions \| query options, `lang` option is `en-US` as default, `name` option is `locale` as default. | ## Returns @@ -27,4 +27,4 @@ The locale that resolved from query ## Throws -Throws the RangeError if the language in the query, that is not a well-formed BCP 47 language tag. +Throws the `RangeError` if the language in the query, that is not a well-formed BCP 47 language tag. diff --git a/packages/hono/docs/functions/setCookieLocale.md b/packages/hono/docs/functions/setCookieLocale.md index f7fe4bb..07c6a66 100644 --- a/packages/hono/docs/functions/setCookieLocale.md +++ b/packages/hono/docs/functions/setCookieLocale.md @@ -8,7 +8,7 @@ ```ts function setCookieLocale( - context, + response, locale, options?): void; ``` @@ -19,9 +19,9 @@ set locale to the response `Set-Cookie` header. | Parameter | Type | Description | | ------ | ------ | ------ | -| `context` | `Context` | A Context \| Hono context | -| `locale` | `string` \| `Locale` | A locale value | -| `options?` | `CookieOptions` & `object` | A cookie options, `name` option is `i18n_locale` as default, and `path` option is `/` as default. | +| `response` | `Response` | The Response \| response | +| `locale` | `string` \| `Locale` | The locale value | +| `options?` | `CookieOptions` | The CookieOptions \| cookie options, `name` option is `i18n_locale` as default | ## Returns @@ -29,19 +29,22 @@ set locale to the response `Set-Cookie` header. ## Example -example for Hono: +example for Web API response on Bun: ```ts -import { Hono } from 'hono' -import { setCookieLocale } from '@intlify/utils/hono' - -const app = new Hono() -app.use('/', c => { - setCookieLocale(c, 'ja-JP') - // ... +import { setCookieLocale } from '@intlify/utils/web' + +Bun.serve({ + port: 8080, + fetch(req) { + const res = new Response('こんにちは、世界!') + setCookieLocale(res, 'ja-JP') + // ... + return res + }, }) ``` ## Throws -Throws the SyntaxError if `locale` is invalid. +Throws the `SyntaxError` if `locale` is invalid. diff --git a/packages/hono/docs/functions/tryCookieLocale.md b/packages/hono/docs/functions/tryCookieLocale.md new file mode 100644 index 0000000..9358d4b --- /dev/null +++ b/packages/hono/docs/functions/tryCookieLocale.md @@ -0,0 +1,30 @@ +[**@intlify/hono**](../index.md) + +*** + +[@intlify/hono](../index.md) / tryCookieLocale + +# Function: tryCookieLocale() + +```ts +function tryCookieLocale(request, options?): Locale | null; +``` + +try to get locale from cookie + +## Parameters + +| Parameter | Type | Description | +| ------ | ------ | ------ | +| `request` | `Request` | The Request \| request | +| `options?` | `CookieLocaleOptions` | The CookieLocaleOptions \| cookie locale options | + +## Returns + +`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`. + +## Description + +Unlike [`getCookieLocale`](getCookieLocale.md), this function does not throw an error if the locale cannot be obtained, this function returns `null`. diff --git a/packages/hono/docs/functions/tryHeaderLocale.md b/packages/hono/docs/functions/tryHeaderLocale.md new file mode 100644 index 0000000..f72fb02 --- /dev/null +++ b/packages/hono/docs/functions/tryHeaderLocale.md @@ -0,0 +1,30 @@ +[**@intlify/hono**](../index.md) + +*** + +[@intlify/hono](../index.md) / tryHeaderLocale + +# Function: tryHeaderLocale() + +```ts +function tryHeaderLocale(request, options?): Locale | null; +``` + +try to get locale from header + +## Parameters + +| Parameter | Type | Description | +| ------ | ------ | ------ | +| `request` | `Request` | The Request \| request | +| `options?` | `HeaderOptions` & `object` | The HeaderOptions \| header options object | + +## Returns + +`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`. + +## Description + +wrap language tag with Intl.Locale \| locale, languages tags will be parsed from `accept-language` header as default. Unlike [getHeaderLocale](getHeaderLocale.md), this function does not throw an error if the locale cannot be obtained, this function returns `null`. diff --git a/packages/hono/docs/functions/tryHeaderLocales.md b/packages/hono/docs/functions/tryHeaderLocales.md new file mode 100644 index 0000000..55a1fb5 --- /dev/null +++ b/packages/hono/docs/functions/tryHeaderLocales.md @@ -0,0 +1,30 @@ +[**@intlify/hono**](../index.md) + +*** + +[@intlify/hono](../index.md) / tryHeaderLocales + +# Function: tryHeaderLocales() + +```ts +function tryHeaderLocales(request, options?): Locale[] | null; +``` + +try to get locales from header + +## Parameters + +| Parameter | Type | Description | +| ------ | ------ | ------ | +| `request` | `Request` | The Request \| request | +| `options?` | `HeaderOptions` | The HeaderOptions \| header options object | + +## Returns + +`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`. + +## Description + +wrap language tags with Intl.Locale \| locale, languages tags will be parsed from `accept-language` header as default. Unlike [getHeaderLocales](getHeaderLocales.md), this function does not throw an error if the locale cannot be obtained, this function returns `null`. diff --git a/packages/hono/docs/functions/tryPathLocale.md b/packages/hono/docs/functions/tryPathLocale.md new file mode 100644 index 0000000..fbff1c4 --- /dev/null +++ b/packages/hono/docs/functions/tryPathLocale.md @@ -0,0 +1,30 @@ +[**@intlify/hono**](../index.md) + +*** + +[@intlify/hono](../index.md) / tryPathLocale + +# Function: tryPathLocale() + +```ts +function tryPathLocale(request, options?): Locale | null; +``` + +try to get the locale from the path + +## Parameters + +| Parameter | Type | Description | +| ------ | ------ | ------ | +| `request` | `Request` | the Request \| request | +| `options?` | `PathOptions` | the PathOptions \| path options object | + +## Returns + +`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`. + +## Description + +Unlike [`getPathLocale`](getPathLocale.md), this function does not throw an error if the locale cannot be obtained, this function returns `null`. diff --git a/packages/hono/docs/functions/tryQueryLocale.md b/packages/hono/docs/functions/tryQueryLocale.md new file mode 100644 index 0000000..13d0bbe --- /dev/null +++ b/packages/hono/docs/functions/tryQueryLocale.md @@ -0,0 +1,30 @@ +[**@intlify/hono**](../index.md) + +*** + +[@intlify/hono](../index.md) / tryQueryLocale + +# Function: tryQueryLocale() + +```ts +function tryQueryLocale(request, options?): Locale | null; +``` + +try to get the locale from the query + +## Parameters + +| Parameter | Type | Description | +| ------ | ------ | ------ | +| `request` | `Request` | the Request \| request | +| `options?` | `QueryOptions` | The QueryOptions \| query options, `lang` option is `en-US` as default, `name` option is `locale` as default. | + +## Returns + +`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`. + +## Description + +Unlike [`getQueryLocale`](getQueryLocale.md), this function does not throw an error if the locale cannot be obtained, this function returns `null`. diff --git a/packages/hono/docs/index.md b/packages/hono/docs/index.md index fc06582..1a9c396 100644 --- a/packages/hono/docs/index.md +++ b/packages/hono/docs/index.md @@ -20,6 +20,11 @@ Internationalization middleware & utilities for hono | [getPathLocale](functions/getPathLocale.md) | get the locale from the path | | [getQueryLocale](functions/getQueryLocale.md) | get the locale from the query | | [setCookieLocale](functions/setCookieLocale.md) | set locale to the response `Set-Cookie` header. | +| [tryCookieLocale](functions/tryCookieLocale.md) | try to get locale from cookie | +| [tryHeaderLocale](functions/tryHeaderLocale.md) | try to get locale from header | +| [tryHeaderLocales](functions/tryHeaderLocales.md) | try to get locales from header | +| [tryPathLocale](functions/tryPathLocale.md) | try to get the locale from the path | +| [tryQueryLocale](functions/tryQueryLocale.md) | try to get the locale from the query | | [useTranslation](functions/useTranslation.md) | use translation function in event handler | ## Interfaces diff --git a/packages/hono/src/index.ts b/packages/hono/src/index.ts index f6a3120..9a3eaf5 100644 --- a/packages/hono/src/index.ts +++ b/packages/hono/src/index.ts @@ -12,7 +12,21 @@ import { translate as _translate, createCoreContext, NOT_REOSLVED } from '@intlify/core' import { getHeaderLocale } from '@intlify/utils' -export * from '@intlify/utils/hono' +export { + getCookieLocale, + getHeaderLanguage, + getHeaderLanguages, + getHeaderLocale, + getHeaderLocales, + getPathLocale, + getQueryLocale, + setCookieLocale, + tryCookieLocale, + tryHeaderLocale, + tryHeaderLocales, + tryPathLocale, + tryQueryLocale +} from '@intlify/utils' export type { CoreContext } from '@intlify/core'