Skip to content

Commit 8acf9a0

Browse files
committed
refactor(h3): change middleware name
1 parent 67a9999 commit 8acf9a0

File tree

11 files changed

+66
-94
lines changed

11 files changed

+66
-94
lines changed

packages/h3/README.md

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -117,34 +117,6 @@ app.get('/', async event => {
117117
createServer(toNodeListener(app)).listen(3000)
118118
```
119119

120-
## 🚀 Usage (Nitro)
121-
122-
For usage with [Nitro](https://nitro.build/) you need to create a plugin instead, create file `plugins/i18n.ts`:
123-
124-
```ts
125-
import { defineNitroPlugin } from 'nitropack/runtime'
126-
import { defineI18nMiddleware, detectLocaleFromAcceptLanguageHeader } from '@intlify/h3'
127-
128-
export default defineNitroPlugin(nitroApp => {
129-
const { onRequest, onAfterResponse } = defineI18nMiddleware({
130-
// detect locale with `accept-language` header
131-
locale: detectLocaleFromAcceptLanguageHeader,
132-
// resource messages
133-
messages: {
134-
en: {
135-
hello: 'Hello {name}!'
136-
},
137-
ja: {
138-
hello: 'こんにちは、{name}!'
139-
}
140-
}
141-
})
142-
143-
nitroApp.hooks.hook('request', onRequest)
144-
nitroApp.hooks.hook('afterResponse', onAfterResponse)
145-
})
146-
```
147-
148120
## 🛠️ Custom locale detection
149121

150122
You can detect locale with your custom logic from current `H3Event`.
@@ -204,16 +176,16 @@ const messages: Record<string, () => ReturnType<typeof loader>> = {
204176
// define custom locale detector and lazy loading
205177
const localeDetector = async (
206178
event: H3Event,
207-
i18n: CoreContext<string, DefineLocaleMessage>
179+
intlify: CoreContext<string, DefineLocaleMessage>
208180
): Promise<string> => {
209181
// detect locale
210182
const locale = getCookieLocale(event.req).toString()
211183

212184
// resource lazy loading
213185
const loader = messages[locale]
214-
if (loader && !i18n.messages[locale]) {
186+
if (loader && !intlify.messages[locale]) {
215187
const message = await loader()
216-
i18n.messages[locale] = message
188+
intlify.messages[locale] = message
217189
}
218190

219191
return locale

packages/h3/docs/functions/detectLocaleFromAcceptLanguageHeader.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ A locale string, which will be detected of **first** from `Accept-Language` head
2828

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

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

4545
const app = new H3()
46-
.use(i18nMiddleware.onRequest)
47-
.use(i18nMiddleware.onResponse)
46+
.use(intlifyMiddleware.onRequest)
47+
.use(intlifyMiddleware.onResponse)
4848
```

packages/h3/docs/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ Internationalization middleware & utilities for h3
3838
| Interface | Description |
3939
| ------ | ------ |
4040
| [DefineLocaleMessage](interfaces/DefineLocaleMessage.md) | The type definition of Locale Message for `@intlify/h3` package |
41-
| [I18nMiddleware](interfaces/I18nMiddleware.md) | Internationalization middleware for H3 |
41+
| [IntlifyMiddleware](interfaces/IntlifyMiddleware.md) | Internationalization middleware for H3 |
4242

4343
## Type Aliases
4444

4545
| Type Alias | Description |
4646
| ------ | ------ |
4747
| [CoreContext](type-aliases/CoreContext.md) | - |
48-
| [I18nPluginOptions](type-aliases/I18nPluginOptions.md) | Internationalization plugin options for H3 |
48+
| [IntlifyPluginOptions](type-aliases/IntlifyPluginOptions.md) | Internationalization plugin options for H3 |

packages/h3/docs/interfaces/I18nMiddleware.md renamed to packages/h3/docs/interfaces/IntlifyMiddleware.md

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

33
***
44

5-
[@intlify/h3](../index.md) / I18nMiddleware
5+
[@intlify/h3](../index.md) / IntlifyMiddleware
66

7-
# Interface: I18nMiddleware
7+
# Interface: IntlifyMiddleware
88

99
Internationalization middleware for H3
1010

packages/h3/docs/type-aliases/I18nPluginOptions.md renamed to packages/h3/docs/type-aliases/IntlifyPluginOptions.md

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

33
***
44

5-
[@intlify/h3](../index.md) / I18nPluginOptions
5+
[@intlify/h3](../index.md) / IntlifyPluginOptions
66

7-
# Type Alias: I18nPluginOptions\<Schema, Locales, Message\>
7+
# Type Alias: IntlifyPluginOptions\<Schema, Locales, Message\>
88

99
```ts
10-
type I18nPluginOptions<Schema, Locales, Message> = CoreOptions<Message, SchemaParams<Schema, Message>, LocaleParams<Locales>>;
10+
type IntlifyPluginOptions<Schema, Locales, Message> = CoreOptions<Message, SchemaParams<Schema, Message>, LocaleParams<Locales>>;
1111
```
1212

1313
Internationalization plugin options for H3

packages/h3/docs/variables/intlify.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Internationalization plugin for H3
1616

1717
| Parameter | Type |
1818
| ------ | ------ |
19-
| `options` | [`I18nPluginOptions`](../type-aliases/I18nPluginOptions.md) |
19+
| `options` | [`IntlifyPluginOptions`](../type-aliases/IntlifyPluginOptions.md) |
2020

2121
## Returns
2222

@@ -26,11 +26,11 @@ Internationalization plugin for H3
2626

2727
```ts
2828
import { H3 } from 'h3'
29-
import { plugin as i18n } from '@intlify/h3'
29+
import { intlify } from '@intlify/h3'
3030

3131
const app = new H3({
3232
plugins: [
33-
i18n({
33+
intlify({
3434
messages: {
3535
en: {
3636
hello: 'Hello {name}!',

packages/h3/src/index.test-d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expectTypeOf, test } from 'vitest'
2-
import { defineI18nMiddleware, useTranslation } from './index.ts'
2+
import { defineIntlifyMiddleware, useTranslation } from './index.ts'
33

44
import type { H3Event } from 'h3'
55

@@ -18,12 +18,12 @@ declare module './index.ts' {
1818
}
1919
}
2020

21-
test('defineI18nMiddleware', () => {
21+
test('defineIntlifyMiddleware', () => {
2222
const _en = {
2323
hello: 'worked'
2424
}
2525
type ResourceSchema = typeof _en
26-
defineI18nMiddleware<[ResourceSchema], 'en' | 'ja'>({
26+
defineIntlifyMiddleware<[ResourceSchema], 'en' | 'ja'>({
2727
messages: {
2828
en: { hello: 'world' },
2929
ja: { hello: '世界' }

packages/h3/src/index.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { createCoreContext } from '@intlify/core'
22
import { describe, expect, test } from 'vitest'
3-
import { SYMBOL_I18N, SYMBOL_I18N_LOCALE } from './symbols.ts'
3+
import { SYMBOL_INTLIFY, SYMBOL_INTLIFY_LOCALE } from './symbols.ts'
44

55
import {
6-
defineI18nMiddleware,
6+
defineIntlifyMiddleware,
77
detectLocaleFromAcceptLanguageHeader,
88
getDetectorLocale,
99
useTranslation
@@ -23,8 +23,8 @@ test('detectLocaleFromAcceptLanguageHeader', () => {
2323
expect(detectLocaleFromAcceptLanguageHeader(eventMock)).toBe('en-US')
2424
})
2525

26-
test('defineI18nMiddleware', () => {
27-
const middleware = defineI18nMiddleware({
26+
test('defineIntlifyMiddleware', () => {
27+
const middleware = defineIntlifyMiddleware({
2828
locale: detectLocaleFromAcceptLanguageHeader,
2929
messages: {
3030
en: {
@@ -62,14 +62,14 @@ describe('useTranslation', () => {
6262
}
6363
},
6464
context: {
65-
[SYMBOL_I18N]: context as CoreContext
65+
[SYMBOL_INTLIFY]: context as CoreContext
6666
}
6767
} as H3Event
6868
const locale = context.locale as unknown
6969
const bindLocaleDetector = (locale as LocaleDetector).bind(null, eventMock)
7070
// @ts-ignore ignore type error because this is test
7171
context.locale = bindLocaleDetector
72-
eventMock.context[SYMBOL_I18N_LOCALE] = bindLocaleDetector
72+
eventMock.context[SYMBOL_INTLIFY_LOCALE] = bindLocaleDetector
7373

7474
// test `useTranslation`
7575
const t = await useTranslation(eventMock)
@@ -101,14 +101,14 @@ test('getDetectorLocale', async () => {
101101
}
102102
},
103103
context: {
104-
[SYMBOL_I18N]: context as CoreContext
104+
[SYMBOL_INTLIFY]: context as CoreContext
105105
}
106106
} as H3Event
107107
const _locale = context.locale as unknown
108108
const bindLocaleDetector = (_locale as LocaleDetector).bind(null, eventMock)
109109
// @ts-ignore ignore type error because this is test
110110
context.locale = bindLocaleDetector
111-
eventMock.context[SYMBOL_I18N_LOCALE] = bindLocaleDetector
111+
eventMock.context[SYMBOL_INTLIFY_LOCALE] = bindLocaleDetector
112112

113113
const locale = await getDetectorLocale(eventMock)
114114
expect(locale.language).toEqual('ja')

0 commit comments

Comments
 (0)