Skip to content

Commit fbabab4

Browse files
committed
fix(h3): change plugin name
1 parent e61db68 commit fbabab4

File tree

8 files changed

+23
-136
lines changed

8 files changed

+23
-136
lines changed

packages/h3/README.md

Lines changed: 8 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,18 @@ app.get('/', event => {
7676

7777
### Translation
7878

79-
If you want to use translation, you need to install plugin. As a result, you can use `useTranslation` within the handler:
79+
If you want to use translation, you need to install `intlify` plugin. As a result, you can use `useTranslation` within the handler:
8080

8181
```ts
8282
import { createServer } from 'node:http'
8383
import { H3, toNodeListener } from 'h3'
84-
import { plugin as i18n, detectLocaleFromAcceptLanguageHeader, useTranslation } from '@intlify/h3'
84+
import { intlify, detectLocaleFromAcceptLanguageHeader, useTranslation } from '@intlify/h3'
8585

86-
// install plugin with `H3` constructor
86+
// install `intlify` plugin with `H3` constructor
8787
const app = new H3({
8888
plugins: [
8989
// configure plugin options
90-
i18n({
90+
intlify({
9191
// detect locale with `accept-language` header
9292
locale: detectLocaleFromAcceptLanguageHeader,
9393
// resource messages
@@ -150,7 +150,7 @@ example for detecting locale from url query:
150150

151151
```ts
152152
import { H3 } from 'h3'
153-
import { plugin as i18n, getQueryLocale } from '@intlify/h3'
153+
import { intlify, getQueryLocale } from '@intlify/h3'
154154

155155
import type { H3Event } from 'h3'
156156

@@ -161,7 +161,7 @@ const localeDetector = (event: H3Event): string => {
161161

162162
const app = new H3({
163163
plugins: [
164-
i18n({
164+
intlify({
165165
// set your custom locale detector
166166
locale: localeDetector
167167
// something options
@@ -182,7 +182,7 @@ You can make that function asynchronous. This is useful when loading resources a
182182

183183
```ts
184184
import { H3 } from 'h3'
185-
import { plugin as i18n, getCookieLocale } from '@intlify/h3'
185+
import { intlify, getCookieLocale } from '@intlify/h3'
186186

187187
import type { H3Event } from 'h3'
188188
import type { DefineLocaleMessage, CoreContext } from '@intlify/h3'
@@ -213,7 +213,7 @@ const localeDetector = async (
213213

214214
const app = new H3({
215215
plugins: [
216-
i18n({
216+
intlify({
217217
// set your custom locale detector
218218
locale: localeDetector
219219
// something options
@@ -223,77 +223,6 @@ const app = new H3({
223223
})
224224
```
225225

226-
## 🧩 Type-safe resources
227-
228-
<!-- eslint-disable markdown/no-missing-label-refs -- NOTE(kazupon): ignore github alert -->
229-
230-
> [!WARNING]
231-
> **This is experimental feature (inspired from [vue-i18n](https://vue-i18n.intlify.dev/guide/advanced/typescript.html#typescript-support)).**
232-
> We would like to get feedback from you 🙂.
233-
234-
> [!NOTE]
235-
> The example code is [here](https://github.com/intlify/h3/tree/main/playground/typesafe-schema)
236-
237-
<!-- eslint-enable markdown/no-missing-label-refs -- NOTE(kazupon): ignore github alert -->
238-
239-
You can support the type-safe resources with schema using TypeScript on `defineI18nMiddleware` options.
240-
241-
Locale messages resource:
242-
243-
```ts
244-
export default {
245-
hello: 'hello, {name}!'
246-
}
247-
```
248-
249-
your application code:
250-
251-
```ts
252-
import { defineI18nMiddleware } from '@intlify/h3'
253-
import { H3 } from 'h3'
254-
import en from './locales/en.ts'
255-
256-
// define resource schema, as 'en' is master resource schema
257-
type ResourceSchema = typeof en
258-
259-
const i18nMiddleware = defineI18nMiddleware<[ResourceSchema], 'en' | 'ja'>({
260-
messages: {
261-
en: { hello: 'Hello, {name}' }
262-
}
263-
// something options
264-
// ...
265-
})
266-
267-
const app = new H3()
268-
app.use(i18nMiddleware.onRequest)
269-
app.use(i18nMiddleware.onResponse)
270-
271-
// something your implementation code ...
272-
// ...
273-
```
274-
275-
Result of type checking with `tsc`:
276-
277-
```sh
278-
npx tsc --noEmit
279-
index.ts:13:3 - error TS2741: Property 'ja' is missing in type '{ en: { hello: string; }; }' but required in type '{ en: ResourceSchema; ja: ResourceSchema; }'.
280-
281-
13 messages: {
282-
~~~~~~~~
283-
284-
../../node_modules/@intlify/core/node_modules/@intlify/core-base/dist/core-base.d.ts:125:5
285-
125 messages?: {
286-
~~~~~~~~
287-
The expected type comes from property 'messages' which is declared here on type 'CoreOptions<string, { message: ResourceSchema; datetime: DateTimeFormat; number: NumberFormat; }, { messages: "en"; datetimeFormats: "en"; numberFormats: "en"; } | { ...; }, ... 8 more ..., NumberFormats<...>>'
288-
289-
290-
Found 1 error in index.ts:13
291-
```
292-
293-
If you are using [Visual Studio Code](https://code.visualstudio.com/) as an editor, you can notice that there is a resource definition omission in the editor with the following error before you run the typescript compilation.
294-
295-
![Type-safe resources](assets/typesafe-schema.png)
296-
297226
## 🖌️ Resource keys completion
298227

299228
<!-- eslint-disable markdown/no-missing-label-refs -- NOTE(kazupon): ignore github alert -->

packages/h3/playground/basic/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import { H3 } from 'h3'
22
import { serve } from 'srvx'
3-
import {
4-
detectLocaleFromAcceptLanguageHeader,
5-
plugin as i18n,
6-
useTranslation
7-
} from '../../src/index.ts' // `@intlify/h3`
3+
import { detectLocaleFromAcceptLanguageHeader, intlify, useTranslation } from '../../src/index.ts' // `@intlify/h3`
84

95
const app = new H3({
106
plugins: [
11-
i18n({
7+
intlify({
128
locale: detectLocaleFromAcceptLanguageHeader,
139
messages: {
1410
en: {

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { H3 } from 'h3'
22
import { serve } from 'srvx'
3-
import {
4-
detectLocaleFromAcceptLanguageHeader,
5-
plugin as i18n,
6-
useTranslation
7-
} from '../../src/index.ts' // in your project, `import { ... } from '@intlify/h3'`
3+
import { detectLocaleFromAcceptLanguageHeader, intlify, useTranslation } from '../../src/index.ts' // in your project, `import { ... } from '@intlify/h3'`
84

95
import en from './locales/en.ts'
106
import ja from './locales/ja.ts'
@@ -20,7 +16,7 @@ declare module '../../src/index.ts' {
2016

2117
const app = new H3({
2218
plugins: [
23-
i18n({
19+
intlify({
2420
locale: detectLocaleFromAcceptLanguageHeader,
2521
messages: {
2622
en,

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import { H3 } from 'h3'
22
import { serve } from 'srvx'
3-
import {
4-
detectLocaleFromAcceptLanguageHeader,
5-
plugin as i18n,
6-
useTranslation
7-
} from '../../src/index.ts' // in your project, `import { ... } from '@intlify/h3'`
3+
import { detectLocaleFromAcceptLanguageHeader, intlify, useTranslation } from '../../src/index.ts' // in your project, `import { ... } from '@intlify/h3'`
84

95
import en from './locales/en.ts'
106
import ja from './locales/ja.ts'
117

128
const app = new H3({
139
plugins: [
14-
i18n({
10+
intlify({
1511
locale: detectLocaleFromAcceptLanguageHeader,
1612
messages: {
1713
en,

packages/h3/playground/typesafe-schema/index.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

packages/h3/playground/typesafe-schema/locales/en.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/h3/spec/integration.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { delay as sleep } from '../../shared/src/index.ts'
55
import {
66
detectLocaleFromAcceptLanguageHeader,
77
getQueryLocale,
8-
plugin as i18n,
8+
intlify,
99
useTranslation
1010
} from '../src/index.ts'
1111

@@ -22,7 +22,7 @@ afterEach(() => {
2222
test('translation', async () => {
2323
app = new H3({
2424
plugins: [
25-
i18n({
25+
intlify({
2626
locale: detectLocaleFromAcceptLanguageHeader,
2727
messages: {
2828
en: {
@@ -62,7 +62,7 @@ describe('custom locale detection', () => {
6262

6363
app = new H3({
6464
plugins: [
65-
i18n({
65+
intlify({
6666
locale: localeDetector,
6767
messages: {
6868
en: {
@@ -115,7 +115,7 @@ describe('custom locale detection', () => {
115115

116116
app = new H3({
117117
plugins: [
118-
i18n({
118+
intlify({
119119
locale: localeDetector,
120120
messages: {
121121
en: {
@@ -156,21 +156,21 @@ describe('custom locale detection', () => {
156156
// async locale detector
157157
const localeDetector = async (
158158
event: H3Event,
159-
i18n: CoreContext<string, DefineLocaleMessage>
159+
intlify: CoreContext<string, DefineLocaleMessage>
160160
) => {
161161
const locale = getQueryLocale(event.req).toString()
162162
await sleep(100)
163163
const loader = messages[locale]
164-
if (loader && !i18n.messages[locale]) {
164+
if (loader && !intlify.messages[locale]) {
165165
const message = await loader()
166-
i18n.messages[locale] = message
166+
intlify.messages[locale] = message
167167
}
168168
return locale
169169
}
170170

171171
app = new H3({
172172
plugins: [
173-
i18n({
173+
intlify({
174174
locale: localeDetector,
175175
messages: {
176176
en: {

packages/h3/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export type I18nPluginOptions<
119119
* ]
120120
* })
121121
*/
122-
export const plugin = definePlugin<I18nPluginOptions>((h3, options) => {
122+
export const intlify = definePlugin<I18nPluginOptions>((h3, options) => {
123123
const { onRequest, onResponse } = defineI18nMiddleware(options)
124124
h3.use(onRequest)
125125
h3.use(onResponse)

0 commit comments

Comments
 (0)