@@ -107,6 +107,145 @@ const middleware = defineI18nMiddleware({
107
107
})
108
108
```
109
109
110
+ ## 🧩 Type-safe resources
111
+
112
+ > [ !WARNING]
113
+ > ** This is experimental feature (inspired from [ vue-i18n] ( https://vue-i18n.intlify.dev/guide/advanced/typescript.html#typescript-support ) ).**
114
+ > We would like to get feedback from you 🙂.
115
+
116
+ > [ !NOTE]
117
+ > The exeample code is [ here] ( ./playground/typesafe-schema )
118
+
119
+ You can support the type-safe resources with schema using TypeScript on ` defineI18nMiddleware ` options.
120
+
121
+ Locale messages resource:
122
+
123
+ ``` ts
124
+ export default {
125
+ hello: ' hello, {name}!'
126
+ }
127
+ ```
128
+
129
+ your application code:
130
+
131
+ ``` ts
132
+ import { defineI18nMiddleware } from ' @intlify/h3'
133
+ import { createApp } from ' h3'
134
+ import en from ' ./locales/en.ts'
135
+
136
+ // define resource schema, as 'en' is master resource schema
137
+ type ResourceSchema = typeof en
138
+
139
+ const middleware = defineI18nMiddleware <[ResourceSchema ], ' en' | ' ja' >({
140
+ messages: {
141
+ en: { hello: ' Hello, {name}' },
142
+ },
143
+ // something options
144
+ // ...
145
+ })
146
+
147
+ const app = createApp ({ ... middleware })
148
+ // someting your implementation code ...
149
+ // ...
150
+ ```
151
+
152
+ Result of type checking with ` tsc ` :
153
+
154
+ ``` sh
155
+ npx tsc --noEmit
156
+ index.ts:13:3 - error TS2741: Property ' ja' is missing in type ' { en: { hello: string; }; }' but required in type ' { en: ResourceSchema; ja: ResourceSchema; }' .
157
+
158
+ 13 messages: {
159
+ ~ ~~~~~~~
160
+
161
+ ../../node_modules/@intlify/core/node_modules/@intlify/core-base/dist/core-base.d.ts:125:5
162
+ 125 messages? : {
163
+ ~ ~~~~~~~
164
+ 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<...>>'
165
+
166
+
167
+ Found 1 error in index.ts:13
168
+ ` ` `
169
+
170
+ 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.
171
+
172
+ ! [Type-safe resources](assets/typesafe-schema.png)
173
+
174
+
175
+ # # 🖌️ Resource keys completion
176
+
177
+ > [! WARNING]
178
+ > ** This is experimental feature (inspired from [vue-i18n](https://vue-i18n.intlify.dev/guide/advanced/typescript.html#typescript-support)).**
179
+ > We would like to get feedback from you 🙂.
180
+
181
+ > [! NOTE]
182
+ > Resource Keys completion can be used if you are using [Visual Studio Code](https://code.visualstudio.com/)
183
+
184
+ You can completion resources key on translation function with ` useTranslation` .
185
+
186
+ ! [Key Completion](assets/key-completion.gif)
187
+
188
+ resource keys completion has twe ways.
189
+
190
+ # ## Type parameter for `useTranslation`
191
+
192
+ > [! NOTE]
193
+ > The exeample code is [here](./playground/local-schema)
194
+
195
+ You can ` useTranslation` set the type parameter to the resource schema you want to key completion of the translation function.
196
+
197
+ the part of example:
198
+ ` ` ` ts
199
+ const router = createRouter ()
200
+ router.get(
201
+ ' /' ,
202
+ eventHandler(( event) => {
203
+ type ResourceSchema = {
204
+ hello: string
205
+ }
206
+ // set resource schema as type parameter
207
+ const t = useTranslation<ResourceSchema>(event)
208
+ // you can completion when you type `t('`
209
+ return t('hello', { name: 'h3 ' })
210
+ }),
211
+ )
212
+ ```
213
+
214
+ ### define global resource schema with `declare module '@intlify/h3 '`
215
+
216
+ > [! NOTE]
217
+ > The exeample code is [here](./ playground/ global- schema)
218
+
219
+ You can do resource key completion with the translation function using the typescript `declare module`.
220
+
221
+ the part of example:
222
+ ```ts
223
+ import en from './locales/en.ts'
224
+
225
+ // 'en' resource is master schema
226
+ type ResourceSchema = typeof en
227
+
228
+ // you can put the type extending with `declare module` as global resource schema
229
+ declare module '@intlify/h3 ' {
230
+ // extend `DefineLocaleMessage` with `ResourceSchema`
231
+ export interface DefineLocaleMessage extends ResourceSchema {}
232
+ }
233
+
234
+ const router = createRouter()
235
+ router.get(
236
+ '/',
237
+ eventHandler((event) => {
238
+ const t = useTranslation(event)
239
+ // you can completion when you type `t('`
240
+ return t('hello', { name: 'h3 ' })
241
+ }),
242
+ )
243
+
244
+ ```
245
+
246
+ The advantage of this way is that it is not necessary to specify the resource schema in the `useTranslation` type parameter.
247
+
248
+
110
249
## 🛠️ Utilites & Helpers
111
250
112
251
`@intlify/h3 ` has a concept of composable utilities & helpers.
0 commit comments