Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![CI][ci-src]][ci-href]

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

## 🌟 Features

Expand Down Expand Up @@ -33,7 +33,7 @@ pnpm add @intlify/h3
bun add @intlify/h3
```

## 🚀 Usage
## 🚀 Usage (h3)

```ts
import { createServer } from 'node:http'
Expand Down Expand Up @@ -78,6 +78,36 @@ app.use(router)
createServer(toNodeListener(app)).listen(3000)
```

## 🚀 Usage (Nitro)
For usage with [Nitro](https://nitro.build/) you need to create a plugin instead, create file `plugins/i18n.ts`:

```ts
import { defineNitroPlugin } from 'nitropack/runtime';
import {
defineI18nMiddleware,
detectLocaleFromAcceptLanguageHeader,
} from '@intlify/h3';

export default defineNitroPlugin((nitroApp) => {
const { onRequest, onAfterResponse } = defineI18nMiddleware({
// detect locale with `accept-language` header
locale: detectLocaleFromAcceptLanguageHeader,
// resource messages
messages: {
en: {
hello: 'Hello {name}!',
},
ja: {
hello: 'こんにちは、{name}!',
},
},
});

nitroApp.hooks.hook('request', onRequest);
nitroApp.hooks.hook('afterResponse', onAfterResponse);
});
```

## 🛠️ Custom locale detection

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