@better-intl/next 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +94 -22
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -2,17 +2,40 @@
|
|
|
2
2
|
|
|
3
3
|
Next.js App Router integration for **better-intl**.
|
|
4
4
|
|
|
5
|
-
Middleware for locale routing, client/server providers, and RSC support.
|
|
6
|
-
|
|
7
5
|
## Install
|
|
8
6
|
|
|
9
7
|
```bash
|
|
10
|
-
npm install @better-intl/next @better-intl/react @better-intl/core
|
|
8
|
+
npm install @better-intl/next @better-intl/react @better-intl/core @better-intl/compiler
|
|
11
9
|
```
|
|
12
10
|
|
|
13
|
-
##
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
### 1. Configure
|
|
14
|
+
|
|
15
|
+
```json
|
|
16
|
+
// better-intl.config.json
|
|
17
|
+
{
|
|
18
|
+
"defaultLocale": "en",
|
|
19
|
+
"locales": ["en", "pt-BR", "es"],
|
|
20
|
+
"catalogs": "./locales/{locale}.json",
|
|
21
|
+
"include": ["app/**/*.tsx"]
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### 2. Plugin (next.config.js)
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import { withBetterIntl } from "@better-intl/next/plugin";
|
|
14
29
|
|
|
15
|
-
|
|
30
|
+
export default withBetterIntl({
|
|
31
|
+
locales: ["en", "pt-BR", "es"],
|
|
32
|
+
defaultLocale: "en",
|
|
33
|
+
})({
|
|
34
|
+
// your next config
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### 3. Middleware
|
|
16
39
|
|
|
17
40
|
```ts
|
|
18
41
|
// middleware.ts
|
|
@@ -26,7 +49,7 @@ export default createIntlMiddleware({
|
|
|
26
49
|
export const config = { matcher: ["/((?!api|_next|.*\\..*).*)"] };
|
|
27
50
|
```
|
|
28
51
|
|
|
29
|
-
###
|
|
52
|
+
### 4. Layout
|
|
30
53
|
|
|
31
54
|
```tsx
|
|
32
55
|
// app/[locale]/layout.tsx
|
|
@@ -54,7 +77,64 @@ export default async function Layout({
|
|
|
54
77
|
}
|
|
55
78
|
```
|
|
56
79
|
|
|
57
|
-
###
|
|
80
|
+
### 5. Write normal JSX
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
// app/[locale]/page.tsx
|
|
84
|
+
export default function Page() {
|
|
85
|
+
return (
|
|
86
|
+
<section>
|
|
87
|
+
<h1>Hello world</h1>
|
|
88
|
+
<p>The best SaaS platform</p>
|
|
89
|
+
<button type="button">Get started</button>
|
|
90
|
+
</section>
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
That's it. No `t()` calls, no hooks, no imports. The Babel plugin transforms your JSX automatically at build time:
|
|
96
|
+
|
|
97
|
+
```tsx
|
|
98
|
+
// What the compiler generates behind the scenes:
|
|
99
|
+
import { useTranslation } from "@better-intl/react";
|
|
100
|
+
|
|
101
|
+
export default function Page() {
|
|
102
|
+
const { t: __t } = useTranslation();
|
|
103
|
+
return (
|
|
104
|
+
<section>
|
|
105
|
+
<h1>{__t("0rr2b7c", undefined, "Hello world")}</h1>
|
|
106
|
+
<p>{__t("1di71qw", undefined, "The best SaaS platform")}</p>
|
|
107
|
+
<button type="button">{__t("1gvrrqk", undefined, "Get started")}</button>
|
|
108
|
+
</section>
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Enable it via Babel:
|
|
114
|
+
|
|
115
|
+
```js
|
|
116
|
+
// babel.config.js
|
|
117
|
+
module.exports = {
|
|
118
|
+
plugins: [["@better-intl/compiler/babel", { mode: "auto" }]],
|
|
119
|
+
};
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Or for zero-runtime (inlines translations at build time):
|
|
123
|
+
|
|
124
|
+
```js
|
|
125
|
+
module.exports = {
|
|
126
|
+
plugins: [
|
|
127
|
+
["@better-intl/compiler/babel", {
|
|
128
|
+
locale: "pt-BR",
|
|
129
|
+
messages: require("./locales/pt-BR.json"),
|
|
130
|
+
}],
|
|
131
|
+
],
|
|
132
|
+
};
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Server Components (RSC)
|
|
136
|
+
|
|
137
|
+
For React Server Components where hooks aren't available, use `getTranslator`:
|
|
58
138
|
|
|
59
139
|
```tsx
|
|
60
140
|
// app/[locale]/page.tsx
|
|
@@ -74,12 +154,16 @@ export default async function Page({
|
|
|
74
154
|
}
|
|
75
155
|
```
|
|
76
156
|
|
|
77
|
-
|
|
157
|
+
## Metadata
|
|
78
158
|
|
|
79
159
|
```tsx
|
|
80
160
|
import { getMetadataTranslator } from "@better-intl/next/server";
|
|
81
161
|
|
|
82
|
-
export async function generateMetadata({
|
|
162
|
+
export async function generateMetadata({
|
|
163
|
+
params,
|
|
164
|
+
}: {
|
|
165
|
+
params: Promise<{ locale: string }>;
|
|
166
|
+
}) {
|
|
83
167
|
const { locale } = await params;
|
|
84
168
|
const t = await getMetadataTranslator(locale, () =>
|
|
85
169
|
import(`../../locales/${locale}.json`)
|
|
@@ -89,25 +173,13 @@ export async function generateMetadata({ params }) {
|
|
|
89
173
|
}
|
|
90
174
|
```
|
|
91
175
|
|
|
92
|
-
### 5. Next.js plugin
|
|
93
|
-
|
|
94
|
-
```js
|
|
95
|
-
// next.config.js
|
|
96
|
-
import { withBetterIntl } from "@better-intl/next/plugin";
|
|
97
|
-
|
|
98
|
-
export default withBetterIntl({
|
|
99
|
-
locales: ["en", "pt-BR", "es"],
|
|
100
|
-
defaultLocale: "en",
|
|
101
|
-
})({});
|
|
102
|
-
```
|
|
103
|
-
|
|
104
176
|
## API
|
|
105
177
|
|
|
106
178
|
| Export | Entry point | Description |
|
|
107
179
|
|--------|-------------|-------------|
|
|
108
180
|
| `NextIntlClientProvider` | `@better-intl/next` | Client provider for layouts |
|
|
109
181
|
| `createIntlMiddleware(config)` | `@better-intl/next/middleware` | Locale detection and routing |
|
|
110
|
-
| `getTranslator(locale, loader)` | `@better-intl/next/server` | Server-side `t()`
|
|
182
|
+
| `getTranslator(locale, loader)` | `@better-intl/next/server` | Server-side `t()` for RSC |
|
|
111
183
|
| `getMetadataTranslator(locale, loader)` | `@better-intl/next/server` | For `generateMetadata` |
|
|
112
184
|
| `getMessages(loader)` | `@better-intl/next/server` | Load messages for a locale |
|
|
113
185
|
| `withBetterIntl(config)` | `@better-intl/next/plugin` | next.config.js plugin |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-intl/next",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Next.js App Router integration for better-intl",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"README.md"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@better-intl/core": "0.1.
|
|
37
|
-
"@better-intl/react": "0.1.
|
|
36
|
+
"@better-intl/core": "0.1.2",
|
|
37
|
+
"@better-intl/react": "0.1.2"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
40
|
"next": ">=14.0.0",
|