|
4 | 4 | [![npm downloads][npm-downloads-src]][npm-downloads-href] |
5 | 5 | [![CI][ci-src]][ci-href] |
6 | 6 |
|
7 | | -Internationalization middleware & utilities for h3 |
| 7 | +Internationalization middleware & utilities for h3 (and therfore also for Nitro, which is using h3) |
8 | 8 |
|
9 | 9 | ## 🌟 Features |
10 | 10 |
|
@@ -33,7 +33,7 @@ pnpm add @intlify/h3 |
33 | 33 | bun add @intlify/h3 |
34 | 34 | ``` |
35 | 35 |
|
36 | | -## 🚀 Usage |
| 36 | +## 🚀 Usage (h3) |
37 | 37 |
|
38 | 38 | ```ts |
39 | 39 | import { createServer } from 'node:http' |
@@ -78,6 +78,36 @@ app.use(router) |
78 | 78 | createServer(toNodeListener(app)).listen(3000) |
79 | 79 | ``` |
80 | 80 |
|
| 81 | +## 🚀 Usage (Nitro) |
| 82 | +For usage with [Nitro](https://nitro.build/) you need to create a plugin instead, create file `plugins/i18n.ts`: |
| 83 | + |
| 84 | +```ts |
| 85 | +import { defineNitroPlugin } from 'nitropack/runtime'; |
| 86 | +import { |
| 87 | + defineI18nMiddleware, |
| 88 | + detectLocaleFromAcceptLanguageHeader, |
| 89 | +} from '@intlify/h3'; |
| 90 | + |
| 91 | +export default defineNitroPlugin((nitroApp) => { |
| 92 | + const { onRequest, onAfterResponse } = defineI18nMiddleware({ |
| 93 | + // detect locale with `accept-language` header |
| 94 | + locale: detectLocaleFromAcceptLanguageHeader, |
| 95 | + // resource messages |
| 96 | + messages: { |
| 97 | + en: { |
| 98 | + hello: 'Hello {name}!', |
| 99 | + }, |
| 100 | + ja: { |
| 101 | + hello: 'こんにちは、{name}!', |
| 102 | + }, |
| 103 | + }, |
| 104 | + }); |
| 105 | + |
| 106 | + nitroApp.hooks.hook('request', onRequest); |
| 107 | + nitroApp.hooks.hook('afterResponse', onAfterResponse); |
| 108 | +}); |
| 109 | +``` |
| 110 | + |
81 | 111 | ## 🛠️ Custom locale detection |
82 | 112 |
|
83 | 113 | You can detect locale with your custom logic from current `H3Event`. |
|
0 commit comments