@@ -101,9 +101,9 @@ const app = new Hono()
101101// install middleware with `app.use`
102102app .use (' *' , i18nMiddleware )
103103
104- app .get (' /' , c => {
104+ app .get (' /' , async c => {
105105 // use `useTranslation` in handler
106- const t = useTranslation (c )
106+ const t = await useTranslation (c )
107107 return c .text (t (' hello' , { name: ' hono' }) + ` \n ` )
108108})
109109
@@ -139,6 +139,54 @@ const middleware = defineI18nMiddleware({
139139})
140140```
141141
142+ You can make that function asynchronous. This is useful when loading resources along with locale detection.
143+
144+ <!-- eslint-disable markdown/no-missing-label-refs -- NOTE(kazupon): ignore github alert -->
145+
146+ > [ !NOTE]
147+ > The case which a synchronous function returns a promise is not supported. you need to use ` async function ` .
148+
149+ <!-- eslint-enable markdown/no-missing-label-refs -- NOTE(kazupon): ignore github alert -->
150+
151+ ``` ts
152+ import { Hono } from ' hono'
153+ import { defineI18nMiddleware , getCookieLocale } from ' @intlify/hono'
154+
155+ import type { Context } from ' hono'
156+ import type { DefineLocaleMessage , CoreContext } from ' @intlify/h3'
157+
158+ const loader = (path : string ) => import (path ).then (m => m .default )
159+ const messages: Record <string , () => ReturnType <typeof loader >> = {
160+ en : () => loader (' ./locales/en.json' ),
161+ ja : () => loader (' ./locales/ja.json' )
162+ }
163+
164+ // define custom locale detector and lazy loading
165+ const localeDetector = async (
166+ ctx : Context ,
167+ i18n : CoreContext <string , DefineLocaleMessage >
168+ ): Promise <string > => {
169+ // detect locale
170+ const locale = getCookieLocale (ctx .req .raw ).toString ()
171+
172+ // resource lazy loading
173+ const loader = messages [locale ]
174+ if (loader && ! i18n .messages [locale ]) {
175+ const message = await loader ()
176+ i18n .messages [locale ] = message
177+ }
178+
179+ return locale
180+ }
181+
182+ const middleware = defineI18nMiddleware ({
183+ // set your custom locale detector
184+ locale: localeDetector
185+ // something options
186+ // ...
187+ })
188+ ```
189+
142190## 🧩 Type-safe resources
143191
144192<!-- eslint-disable markdown/no-missing-label-refs -- NOTE(kazupon): ignore github alert -->
@@ -234,12 +282,12 @@ You can `useTranslation` set the type parameter to the resource schema you want
234282the part of example:
235283
236284` ` ` ts
237- app.get(' /' , c => {
285+ app.get(' /' , async c => {
238286 type ResourceSchema = {
239287 hello: string
240288 }
241289 // set resource schema as type parameter
242- const t = useTranslation< ResourceSchema>( c)
290+ const t = await useTranslation< ResourceSchema>( c)
243291 // you can completion when you type ` t(' `
244292 return c.json(t(' hello' , { name: ' hono' }))
245293})
@@ -270,8 +318,8 @@ declare module '@intlify/hono' {
270318 export interface DefineLocaleMessage extends ResourceSchema {}
271319}
272320
273- app.get(' /' , c => {
274- const t = useTranslation(c)
321+ app.get(' /' , async c => {
322+ const t = await useTranslation(c)
275323 // you can completion when you type `t(' `
276324 return c.json(t(' hello' , { name: ' hono' }))
277325})
0 commit comments