Skip to content

Commit 5a7dc74

Browse files
authored
Added instructions for Nitro (#45)
1 parent 3246216 commit 5a7dc74

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![npm downloads][npm-downloads-src]][npm-downloads-href]
55
[![CI][ci-src]][ci-href]
66

7-
Internationalization middleware & utilities for h3
7+
Internationalization middleware & utilities for h3 (and therfore also for Nitro, which is using h3)
88

99
## 🌟 Features
1010

@@ -33,7 +33,7 @@ pnpm add @intlify/h3
3333
bun add @intlify/h3
3434
```
3535

36-
## 🚀 Usage
36+
## 🚀 Usage (h3)
3737

3838
```ts
3939
import { createServer } from 'node:http'
@@ -78,6 +78,36 @@ app.use(router)
7878
createServer(toNodeListener(app)).listen(3000)
7979
```
8080

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+
81111
## 🛠️ Custom locale detection
82112

83113
You can detect locale with your custom logic from current `H3Event`.

0 commit comments

Comments
 (0)