From ab1094ef9cf4e22fc2974a7376f56cebcacc55a6 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Fri, 21 Nov 2025 20:10:33 +0900 Subject: [PATCH 1/2] breaking(hono): change `defineI18nMiddleware` to `defineIntlifyMiddleware` --- packages/hono/README.md | 40 ++++---- ...ddleware.md => defineIntlifyMiddleware.md} | 18 ++-- .../detectLocaleFromAcceptLanguageHeader.md | 10 +- .../hono/docs/functions/getDetectorLocale.md | 14 +-- .../hono/docs/functions/useTranslation.md | 18 ++-- packages/hono/docs/index.md | 2 +- packages/hono/playground/basic/index.ts | 6 +- .../hono/playground/global-schema/index.ts | 6 +- .../hono/playground/local-schema/index.ts | 6 +- .../hono/playground/typesafe-schema/index.ts | 6 +- packages/hono/spec/integration.spec.ts | 22 ++--- packages/hono/src/index.test-d.ts | 8 +- packages/hono/src/index.test.ts | 20 ++-- packages/hono/src/index.ts | 98 +++++++++---------- 14 files changed, 137 insertions(+), 137 deletions(-) rename packages/hono/docs/functions/{defineI18nMiddleware.md => defineIntlifyMiddleware.md} (83%) diff --git a/packages/hono/README.md b/packages/hono/README.md index f15e1c0..53fb193 100644 --- a/packages/hono/README.md +++ b/packages/hono/README.md @@ -72,18 +72,18 @@ app.get('/', c => { ### Translation -If you want to use translation, you need to install middleware. As a result, you can use `useTranslation` within the handler: +If you want to use translation, you need to install `intlify` middleware. As a result, you can use `useTranslation` within the handler: ```ts import { Hono } from 'hono' import { - defineI18nMiddleware, + defineIntlifyMiddleware, detectLocaleFromAcceptLanguageHeader, useTranslation } from '@intlify/hono' // define middleware with vue-i18n like options -const i18nMiddleware = defineI18nMiddleware({ +const intlify = defineIntlifyMiddleware({ // detect locale with `accept-language` header locale: detectLocaleFromAcceptLanguageHeader, // resource messages @@ -101,8 +101,8 @@ const i18nMiddleware = defineI18nMiddleware({ const app = new Hono() -// install middleware with `app.use` -app.use('*', i18nMiddleware) +// install intlify middleware with `app.use` +app.use('*', intlify) app.get('/', async c => { // use `useTranslation` in handler @@ -121,21 +121,21 @@ example for detecting locale from url query, and get locale with `getDetectorLoc ```ts import { Hono } from 'hono' -import { defineI18nMiddleware, getQueryLocale, getDetectorLocale } from '@intlify/hono' +import { defineIntlifyMiddleware, getQueryLocale, getDetectorLocale } from '@intlify/hono' import type { Context } from 'hono' const DEFAULT_LOCALE = 'en' // define custom locale detector -const localeDetector = (ctx: Context): string => { +const localeDetector = (c: Context): string => { try { - return getQueryLocale(ctx.req.raw).toString() + return getQueryLocale(c.req.raw).toString() } catch { return DEFAULT_LOCALE } } -const i18nMiddleware = defineI18nMiddleware({ +const intlify = defineIntlifyMiddleware({ // set your custom locale detector locale: localeDetector // something options @@ -143,10 +143,10 @@ const i18nMiddleware = defineI18nMiddleware({ }) const app = new Hono() -app.use('*', i18nMiddleware) -app.get('/', async ctx => { - const locale = await getDetectorLocale(ctx) - return ctx.text(`Current Locale: ${locale.language}`) +app.use('*', intlify) +app.get('/', async c => { + const locale = await getDetectorLocale(c) + return c.text(`Current Locale: ${locale.language}`) }) ``` @@ -161,7 +161,7 @@ You can make that function asynchronous. This is useful when loading resources a ```ts import { Hono } from 'hono' -import { defineI18nMiddleware, getCookieLocale } from '@intlify/hono' +import { defineIntlifyMiddleware, getCookieLocale } from '@intlify/hono' import type { Context } from 'hono' import type { DefineLocaleMessage, CoreContext } from '@intlify/h3' @@ -174,23 +174,23 @@ const messages: Record ReturnType> = { // define custom locale detector and lazy loading const localeDetector = async ( - ctx: Context, - i18n: CoreContext + c: Context, + intlify: CoreContext ): Promise => { // detect locale - const locale = getCookieLocale(ctx.req.raw).toString() + const locale = getCookieLocale(c.req.raw).toString() // resource lazy loading const loader = messages[locale] - if (loader && !i18n.messages[locale]) { + if (loader && !intlify.messages[locale]) { const message = await loader() - i18n.messages[locale] = message + intlify.messages[locale] = message } return locale } -const middleware = defineI18nMiddleware({ +const intlify = defineIntlifyMiddleware({ // set your custom locale detector locale: localeDetector // something options diff --git a/packages/hono/docs/functions/defineI18nMiddleware.md b/packages/hono/docs/functions/defineIntlifyMiddleware.md similarity index 83% rename from packages/hono/docs/functions/defineI18nMiddleware.md rename to packages/hono/docs/functions/defineIntlifyMiddleware.md index 64dbe81..a9f1ca6 100644 --- a/packages/hono/docs/functions/defineI18nMiddleware.md +++ b/packages/hono/docs/functions/defineIntlifyMiddleware.md @@ -2,15 +2,15 @@ *** -[@intlify/hono](../index.md) / defineI18nMiddleware +[@intlify/hono](../index.md) / defineIntlifyMiddleware -# Function: defineI18nMiddleware() +# Function: defineIntlifyMiddleware() ```ts -function defineI18nMiddleware(options): MiddlewareHandler; +function defineIntlifyMiddleware(options): MiddlewareHandler; ``` -define i18n middleware for Hono +define intlify middleware for Hono ## Type Parameters @@ -25,13 +25,13 @@ define i18n middleware for Hono | Parameter | Type | Description | | ------ | ------ | ------ | -| `options` | `Options` | An i18n options like vue-i18n [`createI18n`]([https://vue-i18n.intlify.dev/guide/#javascript](https://vue-i18n.intlify.dev/guide/#javascript)), which are passed to `createCoreContext` of `@intlify/core`, see about details [`CoreOptions` of `@intlify/core`](https://github.com/intlify/vue-i18n-next/blob/6a9947dd3e0fe90de7be9c87ea876b8779998de5/packages/core-base/src/context.ts#L196-L216) | +| `options` | `Options` | An intlify options like vue-i18n [`createI18n`]([https://vue-i18n.intlify.dev/guide/#javascript](https://vue-i18n.intlify.dev/guide/#javascript)), which are passed to `createCoreContext` of `@intlify/core`, see about details [`CoreOptions` of `@intlify/core`](https://github.com/intlify/vue-i18n-next/blob/6a9947dd3e0fe90de7be9c87ea876b8779998de5/packages/core-base/src/context.ts#L196-L216) | ## Returns `MiddlewareHandler` -A defined i18n middleware +A defined intlify middleware ## Description @@ -41,9 +41,9 @@ Define the middleware to be specified for Hono [`app.use`]([https://hono.dev/gui ```js import { Hono } from 'hono' -import { defineI18nMiddleware } from '@intlify/hono' +import { defineIntlifyMiddleware } from '@intlify/hono' -const i18nMiddleware = defineI18nMiddleware({ +const intlify = defineIntlifyMiddleware({ messages: { en: { hello: 'Hello {name}!', @@ -59,5 +59,5 @@ const i18nMiddleware = defineI18nMiddleware({ }) const app = new Hono() -app.use('*', i18nMiddleware) +app.use('*', intlify) ``` diff --git a/packages/hono/docs/functions/detectLocaleFromAcceptLanguageHeader.md b/packages/hono/docs/functions/detectLocaleFromAcceptLanguageHeader.md index d812a83..2d1ab35 100644 --- a/packages/hono/docs/functions/detectLocaleFromAcceptLanguageHeader.md +++ b/packages/hono/docs/functions/detectLocaleFromAcceptLanguageHeader.md @@ -7,7 +7,7 @@ # Function: detectLocaleFromAcceptLanguageHeader() ```ts -function detectLocaleFromAcceptLanguageHeader(ctx): string; +function detectLocaleFromAcceptLanguageHeader(c): string; ``` locale detection with `Accept-Language` header @@ -16,7 +16,7 @@ locale detection with `Accept-Language` header | Parameter | Type | Description | | ------ | ------ | ------ | -| `ctx` | `Context` | A Hono context | +| `c` | `Context` | A Hono context | ## Returns @@ -28,9 +28,9 @@ A locale string, which will be detected of **first** from `Accept-Language` head ```js import { Hono } from 'hono' -import { defineI18nMiddleware, detectLocaleFromAcceptLanguageHeader } from '@intlify/hono' +import { defineIntlifyMiddleware, detectLocaleFromAcceptLanguageHeader } from '@intlify/hono' -const i18nMiddleware = defineI18nMiddleware({ +const intlify = defineIntlifyMiddleware({ messages: { en: { hello: 'Hello {name}!', @@ -43,5 +43,5 @@ const i18nMiddleware = defineI18nMiddleware({ }) const app = new Hono() -app.use('*', i18nMiddleware) +app.use('*', intlify) ``` diff --git a/packages/hono/docs/functions/getDetectorLocale.md b/packages/hono/docs/functions/getDetectorLocale.md index 9e0d31f..93952d2 100644 --- a/packages/hono/docs/functions/getDetectorLocale.md +++ b/packages/hono/docs/functions/getDetectorLocale.md @@ -7,7 +7,7 @@ # Function: getDetectorLocale() ```ts -function getDetectorLocale(ctx): Promise; +function getDetectorLocale(c): Promise; ``` get a locale which is detected with locale detector. @@ -16,26 +16,26 @@ get a locale which is detected with locale detector. | Parameter | Type | Description | | ------ | ------ | ------ | -| `ctx` | `Context` | A Hono context | +| `c` | `Context` | A Hono context | ## Returns -`Promise`\<`Intl.Locale`\> +`Promise`\<`Locale`\> Return an `Intl.Locale` instance representing the detected locale ## Description -The locale obtainable via this function comes from the locale detector specified in the `locale` option of the [defineI18nMiddleware](defineI18nMiddleware.md). +The locale obtainable via this function comes from the locale detector specified in the `locale` option of the [defineIntlifyMiddleware](defineIntlifyMiddleware.md). ## Example ```js app.get( '/', - async ctx => { - const locale = await getDetectorLocale(ctx) - return ctx.text(`Current Locale: ${locale.language}`) + async c => { + const locale = await getDetectorLocale(c) + return c.text(`Current Locale: ${locale.language}`) }, ) ``` diff --git a/packages/hono/docs/functions/useTranslation.md b/packages/hono/docs/functions/useTranslation.md index 05ec8e4..1f8e4eb 100644 --- a/packages/hono/docs/functions/useTranslation.md +++ b/packages/hono/docs/functions/useTranslation.md @@ -7,7 +7,7 @@ # Function: useTranslation() ```ts -function useTranslation(ctx): Promise(c): Promise; hello: string; nest: { @@ -38,7 +38,7 @@ use translation function in handler | Parameter | Type | Description | | ------ | ------ | ------ | -| `ctx` | `HonoContext` | A Hono context | +| `c` | `HonoContext` | A Hono context | ## Returns @@ -59,15 +59,15 @@ use translation function in handler \}; \}\>\>\>\> -Return a translation function, which can be translated with i18n resource messages +Return a translation function, which can be translated with intlify resource messages ## Example ```js import { Hono } from 'hono' -import { defineI18nMiddleware } from '@intlify/hono' +import { defineIntlifyMiddleware } from '@intlify/hono' -const i18nMiddleware = defineI18nMiddleware({ +const intlify = defineIntlifyMiddleware({ messages: { en: { hello: 'Hello {name}!', @@ -79,11 +79,11 @@ const i18nMiddleware = defineI18nMiddleware({ }) const app = new Hono() -app.use('*', i18nMiddleware) +app.use('*', intlify) // setup other middlewares ... -app.get('/', async (ctx) => { - const t = await useTranslation(ctx) - return ctx.text(t('hello', { name: 'hono' })) +app.get('/', async (c) => { + const t = await useTranslation(c) + return c.text(t('hello', { name: 'hono' })) }) ``` diff --git a/packages/hono/docs/index.md b/packages/hono/docs/index.md index 9ef2ebd..a42998f 100644 --- a/packages/hono/docs/index.md +++ b/packages/hono/docs/index.md @@ -10,7 +10,7 @@ Internationalization middleware & utilities for Hono | Function | Description | | ------ | ------ | -| [defineI18nMiddleware](functions/defineI18nMiddleware.md) | define i18n middleware for Hono | +| [defineIntlifyMiddleware](functions/defineIntlifyMiddleware.md) | define intlify middleware for Hono | | [detectLocaleFromAcceptLanguageHeader](functions/detectLocaleFromAcceptLanguageHeader.md) | locale detection with `Accept-Language` header | | [getCookieLocale](functions/getCookieLocale.md) | get locale from cookie | | [getDetectorLocale](functions/getDetectorLocale.md) | get a locale which is detected with locale detector. | diff --git a/packages/hono/playground/basic/index.ts b/packages/hono/playground/basic/index.ts index cc4bc21..d9e13ca 100644 --- a/packages/hono/playground/basic/index.ts +++ b/packages/hono/playground/basic/index.ts @@ -1,12 +1,12 @@ import { Hono } from 'hono' import { serve } from 'srvx' import { - defineI18nMiddleware, + defineIntlifyMiddleware, detectLocaleFromAcceptLanguageHeader, useTranslation } from '../../src/index.ts' // `@inlify/hono` -const i18n = defineI18nMiddleware({ +const intlify = defineIntlifyMiddleware({ locale: detectLocaleFromAcceptLanguageHeader, messages: { en: { @@ -19,7 +19,7 @@ const i18n = defineI18nMiddleware({ }) const app: Hono = new Hono() -app.use('*', i18n) +app.use('*', intlify) app.get('/', async c => { const t = await useTranslation(c) return c.text(t('hello', { name: 'hono' }) + `\n`) diff --git a/packages/hono/playground/global-schema/index.ts b/packages/hono/playground/global-schema/index.ts index 9980a18..b9c949d 100644 --- a/packages/hono/playground/global-schema/index.ts +++ b/packages/hono/playground/global-schema/index.ts @@ -1,6 +1,6 @@ import { Hono } from 'hono' import { - defineI18nMiddleware, + defineIntlifyMiddleware, detectLocaleFromAcceptLanguageHeader, useTranslation } from '../../src/index.ts' // in your project, `import { ... } from '@inlify/hono'` @@ -17,7 +17,7 @@ declare module '../../src/index.ts' { export interface DefineLocaleMessage extends ResourceSchema {} } -const i18n = defineI18nMiddleware({ +const intlify = defineIntlifyMiddleware({ locale: detectLocaleFromAcceptLanguageHeader, messages: { en, @@ -26,7 +26,7 @@ const i18n = defineI18nMiddleware({ }) const app: Hono = new Hono() -app.use('*', i18n) +app.use('*', intlify) app.get('/', async c => { const t = await useTranslation(c) return c.text(t('hello', { name: 'hono' })) diff --git a/packages/hono/playground/local-schema/index.ts b/packages/hono/playground/local-schema/index.ts index 4395b5d..a30dfc0 100644 --- a/packages/hono/playground/local-schema/index.ts +++ b/packages/hono/playground/local-schema/index.ts @@ -1,6 +1,6 @@ import { Hono } from 'hono' import { - defineI18nMiddleware, + defineIntlifyMiddleware, detectLocaleFromAcceptLanguageHeader, useTranslation } from '../../src/index.ts' // in your project, `import { ... } from '@inlify/hono'` @@ -8,7 +8,7 @@ import { import en from './locales/en.ts' import ja from './locales/ja.ts' -const i18n = defineI18nMiddleware({ +const intlify = defineIntlifyMiddleware({ locale: detectLocaleFromAcceptLanguageHeader, messages: { en, @@ -17,7 +17,7 @@ const i18n = defineI18nMiddleware({ }) const app: Hono = new Hono() -app.use('*', i18n) +app.use('*', intlify) app.get('/', async c => { type ResourceSchema = { hello: string diff --git a/packages/hono/playground/typesafe-schema/index.ts b/packages/hono/playground/typesafe-schema/index.ts index 91f5154..90d83b4 100644 --- a/packages/hono/playground/typesafe-schema/index.ts +++ b/packages/hono/playground/typesafe-schema/index.ts @@ -1,6 +1,6 @@ // in your project, `import { ... } from '@inlify/hono'` import { Hono } from 'hono' -import { defineI18nMiddleware } from '../../src/index.ts' +import { defineIntlifyMiddleware } from '../../src/index.ts' // define resource schema type ResourceSchema = { @@ -10,7 +10,7 @@ type ResourceSchema = { // you can specify resource schema and locales to type parameter. // - first type parameter: resource schema // - second type parameter: locales -const i18n = defineI18nMiddleware<[ResourceSchema], 'en' | 'ja'>({ +const intlify = defineIntlifyMiddleware<[ResourceSchema], 'en' | 'ja'>({ messages: { en: { hello: 'Hello, {name}' }, // you can see the type error, when you will comment out the below `ja` resource @@ -21,7 +21,7 @@ const i18n = defineI18nMiddleware<[ResourceSchema], 'en' | 'ja'>({ }) const app: Hono = new Hono() -app.use('*', i18n) +app.use('*', intlify) // something your implementation code ... // ... diff --git a/packages/hono/spec/integration.spec.ts b/packages/hono/spec/integration.spec.ts index cc19a44..25933f0 100644 --- a/packages/hono/spec/integration.spec.ts +++ b/packages/hono/spec/integration.spec.ts @@ -2,7 +2,7 @@ import { Hono } from 'hono' import { afterEach, describe, expect, test, vi } from 'vitest' import { delay as sleep } from '../../shared/src/index.ts' import { - defineI18nMiddleware, + defineIntlifyMiddleware, detectLocaleFromAcceptLanguageHeader, getQueryLocale, useTranslation @@ -19,7 +19,7 @@ afterEach(() => { }) test('translation', async () => { - const i18nMiddleware = defineI18nMiddleware({ + const intlify = defineIntlifyMiddleware({ locale: detectLocaleFromAcceptLanguageHeader, messages: { en: { @@ -31,7 +31,7 @@ test('translation', async () => { } }) app = new Hono() - app.use('*', i18nMiddleware) + app.use('*', intlify) app.get('/', async c => { const t = await useTranslation(c) return c.json({ message: t('hello', { name: 'hono' }) }) @@ -58,7 +58,7 @@ describe('custom locale detection', () => { } } - const i18nMiddleware = defineI18nMiddleware({ + const intlify = defineIntlifyMiddleware({ locale: localeDetector, messages: { en: { @@ -70,7 +70,7 @@ describe('custom locale detection', () => { } }) app = new Hono() - app.use('*', i18nMiddleware) + app.use('*', intlify) app.get('/', async c => { const t = await useTranslation(c) return c.json({ message: t('hello', { name: 'hono' }) }) @@ -101,7 +101,7 @@ describe('custom locale detection', () => { return locale } - const i18nMiddleware = defineI18nMiddleware({ + const intlify = defineIntlifyMiddleware({ locale: localeDetector, messages: { en: { @@ -114,7 +114,7 @@ describe('custom locale detection', () => { }) app = new Hono() - app.use('*', i18nMiddleware) + app.use('*', intlify) app.get('/', async c => { const t = await useTranslation(c) return c.json({ message: t('hello', { name: 'hono' }) }) @@ -137,8 +137,8 @@ describe('custom locale detection', () => { test('detect with async parallel loading', async () => { // async locale detector - const localeDetector = async (ctx: Context, i18n: CoreContext) => { - const locale = getQueryLocale(ctx.req.raw).toString() + const localeDetector = async (c: Context, i18n: CoreContext) => { + const locale = getQueryLocale(c.req.raw).toString() await sleep(100) const loader = messages[locale] if (loader && !i18n.messages[locale]) { @@ -148,7 +148,7 @@ describe('custom locale detection', () => { return locale } - const i18nMiddleware = defineI18nMiddleware({ + const intlify = defineIntlifyMiddleware({ locale: localeDetector, messages: { en: { @@ -158,7 +158,7 @@ describe('custom locale detection', () => { }) app = new Hono() - app.use('*', i18nMiddleware) + app.use('*', intlify) app.use('/', async c => { await sleep(100) const t = await useTranslation(c) diff --git a/packages/hono/src/index.test-d.ts b/packages/hono/src/index.test-d.ts index 1772780..3ff898f 100644 --- a/packages/hono/src/index.test-d.ts +++ b/packages/hono/src/index.test-d.ts @@ -1,20 +1,20 @@ import { expectTypeOf, test } from 'vitest' -import { defineI18nMiddleware } from './index.ts' +import { defineIntlifyMiddleware } from './index.ts' import type { MiddlewareHandler } from 'hono' -test('defineI18nMiddleware', () => { +test('defineIntlifyMiddleware', () => { const _en = { hello: 'worked' } // @ts-expect-error -- FIXME type ResourceSchema = typeof _en // eslint-disable-line @typescript-eslint/no-unused-vars, unused-imports/no-unused-vars -- NOTE: for type testing - const middleware = defineI18nMiddleware({ + const intlify = defineIntlifyMiddleware({ messages: { en: { hello: 'world' }, ja: { hello: '世界' } } }) - expectTypeOf(middleware).toExtend() + expectTypeOf(intlify).toExtend() }) diff --git a/packages/hono/src/index.test.ts b/packages/hono/src/index.test.ts index 7921100..bf33be1 100644 --- a/packages/hono/src/index.test.ts +++ b/packages/hono/src/index.test.ts @@ -5,7 +5,7 @@ import type { LocaleDetector } from '@intlify/core' import type { Context } from 'hono' import { - defineI18nMiddleware, + defineIntlifyMiddleware, detectLocaleFromAcceptLanguageHeader, getDetectorLocale, useTranslation @@ -25,8 +25,8 @@ test('detectLocaleFromAcceptLanguageHeader', () => { expect(detectLocaleFromAcceptLanguageHeader(mockContext)).toBe('en-US') }) -test('defineI18nMiddleware', () => { - const middleware = defineI18nMiddleware({ +test('defineIntlifyMiddleware', () => { + const intlify = defineIntlifyMiddleware({ locale: detectLocaleFromAcceptLanguageHeader, messages: { en: { @@ -37,13 +37,13 @@ test('defineI18nMiddleware', () => { } } }) - expect(typeof middleware).toBe('function') + expect(typeof intlify).toBe('function') }) describe('useTranslation', () => { test('basic', async () => { /** - * setup `defineI18nMiddleware` emulates + * setup `defineIntlifyMiddleware` emulates */ const context = createCoreContext({ locale: detectLocaleFromAcceptLanguageHeader, @@ -65,9 +65,9 @@ describe('useTranslation', () => { } }, get: (key: string) => { - if (key === 'i18n') { + if (key === 'intlify') { return context - } else if (key === 'i18nLocaleDetector') { + } else if (key === 'intlifyLocaleDetector') { const locale = context.locale as unknown return (locale as LocaleDetector).bind(null, mockContext) } @@ -97,7 +97,7 @@ describe('useTranslation', () => { test('getDetectorLocale', async () => { /** - * setup `defineI18nMiddleware` emulates + * setup `defineIntlifyMiddleware` emulates */ const context = createCoreContext({ locale: detectLocaleFromAcceptLanguageHeader @@ -111,9 +111,9 @@ test('getDetectorLocale', async () => { } }, get: (key: string) => { - if (key === 'i18n') { + if (key === 'intlify') { return context - } else if (key === 'i18nLocaleDetector') { + } else if (key === 'intlifyLocaleDetector') { const locale = context.locale as unknown return (locale as LocaleDetector).bind(null, mockContext) } diff --git a/packages/hono/src/index.ts b/packages/hono/src/index.ts index 5fe0c40..cae0e42 100644 --- a/packages/hono/src/index.ts +++ b/packages/hono/src/index.ts @@ -51,8 +51,8 @@ import type { TranslationFunction } from '../../shared/src/types.ts' declare module 'hono' { interface ContextVariableMap { - i18n?: CoreContext - i18nLocaleDetector?: LocaleDetector + intlify?: CoreContext + intlifyLocaleDetector?: LocaleDetector } } @@ -86,22 +86,22 @@ type DefaultLocaleMessageSchema< export interface DefineLocaleMessage extends LocaleMessage {} /** - * define i18n middleware for Hono + * define intlify middleware for Hono * * @description * Define the middleware to be specified for Hono [`app.use`]({@link https://hono.dev/guides/middleware}) * - * @param options - An i18n options like vue-i18n [`createI18n`]({@link https://vue-i18n.intlify.dev/guide/#javascript}), which are passed to `createCoreContext` of `@intlify/core`, see about details [`CoreOptions` of `@intlify/core`](https://github.com/intlify/vue-i18n-next/blob/6a9947dd3e0fe90de7be9c87ea876b8779998de5/packages/core-base/src/context.ts#L196-L216) + * @param options - An intlify options like vue-i18n [`createI18n`]({@link https://vue-i18n.intlify.dev/guide/#javascript}), which are passed to `createCoreContext` of `@intlify/core`, see about details [`CoreOptions` of `@intlify/core`](https://github.com/intlify/vue-i18n-next/blob/6a9947dd3e0fe90de7be9c87ea876b8779998de5/packages/core-base/src/context.ts#L196-L216) * - * @returns A defined i18n middleware + * @returns A defined intlify middleware * * @example * * ```js * import { Hono } from 'hono' - * import { defineI18nMiddleware } from '@intlify/hono' + * import { defineIntlifyMiddleware } from '@intlify/hono' * - * const i18nMiddleware = defineI18nMiddleware({ + * const intlify = defineIntlifyMiddleware({ * messages: { * en: { * hello: 'Hello {name}!', @@ -117,10 +117,10 @@ export interface DefineLocaleMessage extends LocaleMessage {} * }) * * const app = new Hono() - * app.use('*', i18nMiddleware) + * app.use('*', intlify) * ``` */ -export function defineI18nMiddleware< +export function defineIntlifyMiddleware< Schema = DefaultLocaleMessageSchema, Locales = string, Message = string, @@ -130,51 +130,51 @@ export function defineI18nMiddleware< LocaleParams > = CoreOptions, LocaleParams> >(options: Options): MiddlewareHandler { - const i18n = createCoreContext(options as unknown as CoreOptions) - const orgLocale = i18n.locale + const intlify = createCoreContext(options as unknown as CoreOptions) + const orgLocale = intlify.locale let staticLocaleDetector: LocaleDetector | null = null if (typeof orgLocale === 'string') { console.warn( - `defineI18nMiddleware 'locale' option is static ${orgLocale} locale! you should specify dynamic locale detector function.` + `defineIntlifyMiddleware 'locale' option is static ${orgLocale} locale! you should specify dynamic locale detector function.` ) staticLocaleDetector = () => orgLocale } - const getLocaleDetector = (ctx: Context, i18n: CoreContext): LocaleDetector => { + const getLocaleDetector = (ctx: Context, intlify: CoreContext): LocaleDetector => { return typeof orgLocale === 'function' - ? orgLocale.bind(null, ctx, i18n) + ? orgLocale.bind(null, ctx, intlify) : staticLocaleDetector == null ? detectLocaleFromAcceptLanguageHeader.bind(null, ctx) - : staticLocaleDetector.bind(null, ctx, i18n) + : staticLocaleDetector.bind(null, ctx, intlify) } return async (ctx: Context, next: Next) => { - const detector = getLocaleDetector(ctx, i18n as CoreContext) - i18n.locale = detector - ctx.set('i18nLocaleDetector', detector) - ctx.set('i18n', i18n as CoreContext) + const detector = getLocaleDetector(ctx, intlify as CoreContext) + intlify.locale = detector + ctx.set('intlifyLocaleDetector', detector) + ctx.set('intlify', intlify as CoreContext) await next() - i18n.locale = orgLocale - ctx.set('i18n', undefined) - ctx.set('i18nLocaleDetector', undefined) + intlify.locale = orgLocale + ctx.set('intlify', undefined) + ctx.set('intlifyLocaleDetector', undefined) } } /** * locale detection with `Accept-Language` header * - * @param ctx - A Hono context + * @param c - A Hono context * @returns A locale string, which will be detected of **first** from `Accept-Language` header * * @example * ```js * import { Hono } from 'hono' - * import { defineI18nMiddleware, detectLocaleFromAcceptLanguageHeader } from '@intlify/hono' + * import { defineIntlifyMiddleware, detectLocaleFromAcceptLanguageHeader } from '@intlify/hono' * - * const i18nMiddleware = defineI18nMiddleware({ + * const intlify = defineIntlifyMiddleware({ * messages: { * en: { * hello: 'Hello {name}!', @@ -187,24 +187,24 @@ export function defineI18nMiddleware< * }) * * const app = new Hono() - * app.use('*', i18nMiddleware) + * app.use('*', intlify) * ``` */ -export const detectLocaleFromAcceptLanguageHeader = (ctx: Context): Locale => - getHeaderLocale(ctx.req.raw).toString() +export const detectLocaleFromAcceptLanguageHeader = (c: Context): Locale => + getHeaderLocale(c.req.raw).toString() async function getLocaleAndIntlifyContext(ctx: Context): Promise<[string, CoreContext]> { - const intlify = ctx.get('i18n') + const intlify = ctx.get('intlify') if (intlify == null) { throw new Error( - 'middleware not initialized, please setup `app.use` with the middleware obtained with `defineI18nMiddleware`' + 'middleware not initialized, please setup `app.use` with the middleware obtained with `defineIntlifyMiddleware`' ) } - const localeDetector = ctx.get('i18nLocaleDetector') + const localeDetector = ctx.get('intlifyLocaleDetector') if (localeDetector == null) { throw new Error( - 'locale detector not found in context, please make sure that the i18n middleware is correctly set up' + 'locale detector not found in context, please make sure that the intlify middleware is correctly set up' ) } @@ -218,15 +218,15 @@ async function getLocaleAndIntlifyContext(ctx: Context): Promise<[string, CoreCo /** * use translation function in handler * - * @param ctx - A Hono context - * @returns Return a translation function, which can be translated with i18n resource messages + * @param c - A Hono context + * @returns Return a translation function, which can be translated with intlify resource messages * * @example * ```js * import { Hono } from 'hono' - * import { defineI18nMiddleware } from '@intlify/hono' + * import { defineIntlifyMiddleware } from '@intlify/hono' * - * const i18nMiddleware = defineI18nMiddleware({ + * const intlify = defineIntlifyMiddleware({ * messages: { * en: { * hello: 'Hello {name}!', @@ -238,20 +238,20 @@ async function getLocaleAndIntlifyContext(ctx: Context): Promise<[string, CoreCo * }) * * const app = new Hono() - * app.use('*', i18nMiddleware) + * app.use('*', intlify) * // setup other middlewares ... * - * app.get('/', async (ctx) => { - * const t = await useTranslation(ctx) - * return ctx.text(t('hello', { name: 'hono' })) + * app.get('/', async (c) => { + * const t = await useTranslation(c) + * return c.text(t('hello', { name: 'hono' })) * }) * ``` */ export async function useTranslation< Schema extends Record = {}, // eslint-disable-line @typescript-eslint/no-explicit-any -- NOTE(kazupon): generic type HonoContext extends Context = Context ->(ctx: HonoContext): Promise> { - const [locale, intlify] = await getLocaleAndIntlifyContext(ctx) +>(c: HonoContext): Promise> { + const [locale, intlify] = await getLocaleAndIntlifyContext(c) intlify.locale = locale function translate(key: string, ...args: unknown[]): string { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call -- NOTE(kazupon): generic type @@ -278,24 +278,24 @@ export async function useTranslation< /** * get a locale which is detected with locale detector. * - * @description The locale obtainable via this function comes from the locale detector specified in the `locale` option of the {@link defineI18nMiddleware}. + * @description The locale obtainable via this function comes from the locale detector specified in the `locale` option of the {@link defineIntlifyMiddleware}. * * @example * ```js * app.get( * '/', - * async ctx => { - * const locale = await getDetectorLocale(ctx) - * return ctx.text(`Current Locale: ${locale.language}`) + * async c => { + * const locale = await getDetectorLocale(c) + * return c.text(`Current Locale: ${locale.language}`) * }, * ) * ``` * - * @param ctx - A Hono context + * @param c - A Hono context * * @returns Return an {@linkcode Intl.Locale} instance representing the detected locale */ -export async function getDetectorLocale(ctx: Context): Promise { - const [locale] = await getLocaleAndIntlifyContext(ctx) +export async function getDetectorLocale(c: Context): Promise { + const [locale] = await getLocaleAndIntlifyContext(c) return new Intl.Locale(locale) } From 0411f8d97e6de7d0fbf9bceb82cf06dd213448d7 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Fri, 21 Nov 2025 20:19:38 +0900 Subject: [PATCH 2/2] docs: fix --- packages/hono/docs/functions/getDetectorLocale.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/hono/docs/functions/getDetectorLocale.md b/packages/hono/docs/functions/getDetectorLocale.md index 93952d2..03a982e 100644 --- a/packages/hono/docs/functions/getDetectorLocale.md +++ b/packages/hono/docs/functions/getDetectorLocale.md @@ -7,7 +7,7 @@ # Function: getDetectorLocale() ```ts -function getDetectorLocale(c): Promise; +function getDetectorLocale(c): Promise; ``` get a locale which is detected with locale detector. @@ -20,7 +20,7 @@ get a locale which is detected with locale detector. ## Returns -`Promise`\<`Locale`\> +`Promise`\<`Intl.Locale`\> Return an `Intl.Locale` instance representing the detected locale