Skip to content

Commit 9bf6668

Browse files
authored
breaking(hono): change defineI18nMiddleware to defineIntlifyMiddleware (#45)
* breaking(hono): change `defineI18nMiddleware` to `defineIntlifyMiddleware` * docs: fix
1 parent 552b5c0 commit 9bf6668

File tree

14 files changed

+136
-136
lines changed

14 files changed

+136
-136
lines changed

packages/hono/README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,18 @@ app.get('/', c => {
7272

7373
### Translation
7474

75-
If you want to use translation, you need to install middleware. As a result, you can use `useTranslation` within the handler:
75+
If you want to use translation, you need to install `intlify` middleware. As a result, you can use `useTranslation` within the handler:
7676

7777
```ts
7878
import { Hono } from 'hono'
7979
import {
80-
defineI18nMiddleware,
80+
defineIntlifyMiddleware,
8181
detectLocaleFromAcceptLanguageHeader,
8282
useTranslation
8383
} from '@intlify/hono'
8484

8585
// define middleware with vue-i18n like options
86-
const i18nMiddleware = defineI18nMiddleware({
86+
const intlify = defineIntlifyMiddleware({
8787
// detect locale with `accept-language` header
8888
locale: detectLocaleFromAcceptLanguageHeader,
8989
// resource messages
@@ -101,8 +101,8 @@ const i18nMiddleware = defineI18nMiddleware({
101101

102102
const app = new Hono()
103103

104-
// install middleware with `app.use`
105-
app.use('*', i18nMiddleware)
104+
// install intlify middleware with `app.use`
105+
app.use('*', intlify)
106106

107107
app.get('/', async c => {
108108
// use `useTranslation` in handler
@@ -121,32 +121,32 @@ example for detecting locale from url query, and get locale with `getDetectorLoc
121121

122122
```ts
123123
import { Hono } from 'hono'
124-
import { defineI18nMiddleware, getQueryLocale, getDetectorLocale } from '@intlify/hono'
124+
import { defineIntlifyMiddleware, getQueryLocale, getDetectorLocale } from '@intlify/hono'
125125
import type { Context } from 'hono'
126126

127127
const DEFAULT_LOCALE = 'en'
128128

129129
// define custom locale detector
130-
const localeDetector = (ctx: Context): string => {
130+
const localeDetector = (c: Context): string => {
131131
try {
132-
return getQueryLocale(ctx.req.raw).toString()
132+
return getQueryLocale(c.req.raw).toString()
133133
} catch {
134134
return DEFAULT_LOCALE
135135
}
136136
}
137137

138-
const i18nMiddleware = defineI18nMiddleware({
138+
const intlify = defineIntlifyMiddleware({
139139
// set your custom locale detector
140140
locale: localeDetector
141141
// something options
142142
// ...
143143
})
144144

145145
const app = new Hono()
146-
app.use('*', i18nMiddleware)
147-
app.get('/', async ctx => {
148-
const locale = await getDetectorLocale(ctx)
149-
return ctx.text(`Current Locale: ${locale.language}`)
146+
app.use('*', intlify)
147+
app.get('/', async c => {
148+
const locale = await getDetectorLocale(c)
149+
return c.text(`Current Locale: ${locale.language}`)
150150
})
151151
```
152152

@@ -161,7 +161,7 @@ You can make that function asynchronous. This is useful when loading resources a
161161

162162
```ts
163163
import { Hono } from 'hono'
164-
import { defineI18nMiddleware, getCookieLocale } from '@intlify/hono'
164+
import { defineIntlifyMiddleware, getCookieLocale } from '@intlify/hono'
165165

166166
import type { Context } from 'hono'
167167
import type { DefineLocaleMessage, CoreContext } from '@intlify/h3'
@@ -174,23 +174,23 @@ const messages: Record<string, () => ReturnType<typeof loader>> = {
174174

175175
// define custom locale detector and lazy loading
176176
const localeDetector = async (
177-
ctx: Context,
178-
i18n: CoreContext<string, DefineLocaleMessage>
177+
c: Context,
178+
intlify: CoreContext<string, DefineLocaleMessage>
179179
): Promise<string> => {
180180
// detect locale
181-
const locale = getCookieLocale(ctx.req.raw).toString()
181+
const locale = getCookieLocale(c.req.raw).toString()
182182

183183
// resource lazy loading
184184
const loader = messages[locale]
185-
if (loader && !i18n.messages[locale]) {
185+
if (loader && !intlify.messages[locale]) {
186186
const message = await loader()
187-
i18n.messages[locale] = message
187+
intlify.messages[locale] = message
188188
}
189189

190190
return locale
191191
}
192192

193-
const middleware = defineI18nMiddleware({
193+
const intlify = defineIntlifyMiddleware({
194194
// set your custom locale detector
195195
locale: localeDetector
196196
// something options

packages/hono/docs/functions/defineI18nMiddleware.md renamed to packages/hono/docs/functions/defineIntlifyMiddleware.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
***
44

5-
[@intlify/hono](../index.md) / defineI18nMiddleware
5+
[@intlify/hono](../index.md) / defineIntlifyMiddleware
66

7-
# Function: defineI18nMiddleware()
7+
# Function: defineIntlifyMiddleware()
88

99
```ts
10-
function defineI18nMiddleware<Schema, Locales, Message, Options>(options): MiddlewareHandler;
10+
function defineIntlifyMiddleware<Schema, Locales, Message, Options>(options): MiddlewareHandler;
1111
```
1212

13-
define i18n middleware for Hono
13+
define intlify middleware for Hono
1414

1515
## Type Parameters
1616

@@ -25,13 +25,13 @@ define i18n middleware for Hono
2525

2626
| Parameter | Type | Description |
2727
| ------ | ------ | ------ |
28-
| `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) |
28+
| `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) |
2929

3030
## Returns
3131

3232
`MiddlewareHandler`
3333

34-
A defined i18n middleware
34+
A defined intlify middleware
3535

3636
## Description
3737

@@ -41,9 +41,9 @@ Define the middleware to be specified for Hono [`app.use`]([https://hono.dev/gui
4141

4242
```js
4343
import { Hono } from 'hono'
44-
import { defineI18nMiddleware } from '@intlify/hono'
44+
import { defineIntlifyMiddleware } from '@intlify/hono'
4545

46-
const i18nMiddleware = defineI18nMiddleware({
46+
const intlify = defineIntlifyMiddleware({
4747
messages: {
4848
en: {
4949
hello: 'Hello {name}!',
@@ -59,5 +59,5 @@ const i18nMiddleware = defineI18nMiddleware({
5959
})
6060

6161
const app = new Hono()
62-
app.use('*', i18nMiddleware)
62+
app.use('*', intlify)
6363
```

packages/hono/docs/functions/detectLocaleFromAcceptLanguageHeader.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Function: detectLocaleFromAcceptLanguageHeader()
88

99
```ts
10-
function detectLocaleFromAcceptLanguageHeader(ctx): string;
10+
function detectLocaleFromAcceptLanguageHeader(c): string;
1111
```
1212

1313
locale detection with `Accept-Language` header
@@ -16,7 +16,7 @@ locale detection with `Accept-Language` header
1616

1717
| Parameter | Type | Description |
1818
| ------ | ------ | ------ |
19-
| `ctx` | `Context` | A Hono context |
19+
| `c` | `Context` | A Hono context |
2020

2121
## Returns
2222

@@ -28,9 +28,9 @@ A locale string, which will be detected of **first** from `Accept-Language` head
2828

2929
```js
3030
import { Hono } from 'hono'
31-
import { defineI18nMiddleware, detectLocaleFromAcceptLanguageHeader } from '@intlify/hono'
31+
import { defineIntlifyMiddleware, detectLocaleFromAcceptLanguageHeader } from '@intlify/hono'
3232

33-
const i18nMiddleware = defineI18nMiddleware({
33+
const intlify = defineIntlifyMiddleware({
3434
messages: {
3535
en: {
3636
hello: 'Hello {name}!',
@@ -43,5 +43,5 @@ const i18nMiddleware = defineI18nMiddleware({
4343
})
4444

4545
const app = new Hono()
46-
app.use('*', i18nMiddleware)
46+
app.use('*', intlify)
4747
```

packages/hono/docs/functions/getDetectorLocale.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Function: getDetectorLocale()
88

99
```ts
10-
function getDetectorLocale(ctx): Promise<Intl.Locale>;
10+
function getDetectorLocale(c): Promise<Intl.Locale>;
1111
```
1212

1313
get a locale which is detected with locale detector.
@@ -16,7 +16,7 @@ get a locale which is detected with locale detector.
1616

1717
| Parameter | Type | Description |
1818
| ------ | ------ | ------ |
19-
| `ctx` | `Context` | A Hono context |
19+
| `c` | `Context` | A Hono context |
2020

2121
## Returns
2222

@@ -26,16 +26,16 @@ Return an `Intl.Locale` instance representing the detected locale
2626

2727
## Description
2828

29-
The locale obtainable via this function comes from the locale detector specified in the `locale` option of the [defineI18nMiddleware](defineI18nMiddleware.md).
29+
The locale obtainable via this function comes from the locale detector specified in the `locale` option of the [defineIntlifyMiddleware](defineIntlifyMiddleware.md).
3030

3131
## Example
3232

3333
```js
3434
app.get(
3535
'/',
36-
async ctx => {
37-
const locale = await getDetectorLocale(ctx)
38-
return ctx.text(`Current Locale: ${locale.language}`)
36+
async c => {
37+
const locale = await getDetectorLocale(c)
38+
return c.text(`Current Locale: ${locale.language}`)
3939
},
4040
)
4141
```

packages/hono/docs/functions/useTranslation.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Function: useTranslation()
88

99
```ts
10-
function useTranslation<Schema, HonoContext>(ctx): Promise<TranslationFunction<Schema, DefineLocaleMessage, ResolveResourceKeys<Schema, DefineLocaleMessage, RemoveIndexSignature<{
10+
function useTranslation<Schema, HonoContext>(c): Promise<TranslationFunction<Schema, DefineLocaleMessage, ResolveResourceKeys<Schema, DefineLocaleMessage, RemoveIndexSignature<{
1111
[key: string]: LocaleMessageValue<string>;
1212
hello: string;
1313
nest: {
@@ -38,7 +38,7 @@ use translation function in handler
3838

3939
| Parameter | Type | Description |
4040
| ------ | ------ | ------ |
41-
| `ctx` | `HonoContext` | A Hono context |
41+
| `c` | `HonoContext` | A Hono context |
4242

4343
## Returns
4444

@@ -59,15 +59,15 @@ use translation function in handler
5959
\};
6060
\}\>\>\>\>
6161

62-
Return a translation function, which can be translated with i18n resource messages
62+
Return a translation function, which can be translated with intlify resource messages
6363

6464
## Example
6565

6666
```js
6767
import { Hono } from 'hono'
68-
import { defineI18nMiddleware } from '@intlify/hono'
68+
import { defineIntlifyMiddleware } from '@intlify/hono'
6969

70-
const i18nMiddleware = defineI18nMiddleware({
70+
const intlify = defineIntlifyMiddleware({
7171
messages: {
7272
en: {
7373
hello: 'Hello {name}!',
@@ -79,11 +79,11 @@ const i18nMiddleware = defineI18nMiddleware({
7979
})
8080

8181
const app = new Hono()
82-
app.use('*', i18nMiddleware)
82+
app.use('*', intlify)
8383
// setup other middlewares ...
8484

85-
app.get('/', async (ctx) => {
86-
const t = await useTranslation(ctx)
87-
return ctx.text(t('hello', { name: 'hono' }))
85+
app.get('/', async (c) => {
86+
const t = await useTranslation(c)
87+
return c.text(t('hello', { name: 'hono' }))
8888
})
8989
```

packages/hono/docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Internationalization middleware & utilities for Hono
1010

1111
| Function | Description |
1212
| ------ | ------ |
13-
| [defineI18nMiddleware](functions/defineI18nMiddleware.md) | define i18n middleware for Hono |
13+
| [defineIntlifyMiddleware](functions/defineIntlifyMiddleware.md) | define intlify middleware for Hono |
1414
| [detectLocaleFromAcceptLanguageHeader](functions/detectLocaleFromAcceptLanguageHeader.md) | locale detection with `Accept-Language` header |
1515
| [getCookieLocale](functions/getCookieLocale.md) | get locale from cookie |
1616
| [getDetectorLocale](functions/getDetectorLocale.md) | get a locale which is detected with locale detector. |

packages/hono/playground/basic/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Hono } from 'hono'
22
import { serve } from 'srvx'
33
import {
4-
defineI18nMiddleware,
4+
defineIntlifyMiddleware,
55
detectLocaleFromAcceptLanguageHeader,
66
useTranslation
77
} from '../../src/index.ts' // `@inlify/hono`
88

9-
const i18n = defineI18nMiddleware({
9+
const intlify = defineIntlifyMiddleware({
1010
locale: detectLocaleFromAcceptLanguageHeader,
1111
messages: {
1212
en: {
@@ -19,7 +19,7 @@ const i18n = defineI18nMiddleware({
1919
})
2020

2121
const app: Hono = new Hono()
22-
app.use('*', i18n)
22+
app.use('*', intlify)
2323
app.get('/', async c => {
2424
const t = await useTranslation(c)
2525
return c.text(t('hello', { name: 'hono' }) + `\n`)

packages/hono/playground/global-schema/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Hono } from 'hono'
22
import {
3-
defineI18nMiddleware,
3+
defineIntlifyMiddleware,
44
detectLocaleFromAcceptLanguageHeader,
55
useTranslation
66
} from '../../src/index.ts' // in your project, `import { ... } from '@inlify/hono'`
@@ -17,7 +17,7 @@ declare module '../../src/index.ts' {
1717
export interface DefineLocaleMessage extends ResourceSchema {}
1818
}
1919

20-
const i18n = defineI18nMiddleware({
20+
const intlify = defineIntlifyMiddleware({
2121
locale: detectLocaleFromAcceptLanguageHeader,
2222
messages: {
2323
en,
@@ -26,7 +26,7 @@ const i18n = defineI18nMiddleware({
2626
})
2727

2828
const app: Hono = new Hono()
29-
app.use('*', i18n)
29+
app.use('*', intlify)
3030
app.get('/', async c => {
3131
const t = await useTranslation(c)
3232
return c.text(t('hello', { name: 'hono' }))

packages/hono/playground/local-schema/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { Hono } from 'hono'
22
import {
3-
defineI18nMiddleware,
3+
defineIntlifyMiddleware,
44
detectLocaleFromAcceptLanguageHeader,
55
useTranslation
66
} from '../../src/index.ts' // in your project, `import { ... } from '@inlify/hono'`
77

88
import en from './locales/en.ts'
99
import ja from './locales/ja.ts'
1010

11-
const i18n = defineI18nMiddleware({
11+
const intlify = defineIntlifyMiddleware({
1212
locale: detectLocaleFromAcceptLanguageHeader,
1313
messages: {
1414
en,
@@ -17,7 +17,7 @@ const i18n = defineI18nMiddleware({
1717
})
1818

1919
const app: Hono = new Hono()
20-
app.use('*', i18n)
20+
app.use('*', intlify)
2121
app.get('/', async c => {
2222
type ResourceSchema = {
2323
hello: string

0 commit comments

Comments
 (0)