@better-translate/nextjs 1.0.1 → 1.1.0

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.
Files changed (2) hide show
  1. package/README.md +178 -3
  2. package/package.json +15 -5
package/README.md CHANGED
@@ -1,5 +1,180 @@
1
- # @better-translate/nextjs
1
+ # Next.js
2
2
 
3
- `@better-translate/nextjs` adds locale routing, request helpers, navigation helpers, and proxy support for Next.js App Router. Use it with `@better-translate/core`, and add `@better-translate/react` only if you need client-side hooks.
3
+ Use `@better-translate/nextjs` when you want locale-prefixed URLs, server-side translations, and locale-aware navigation in App Router.
4
4
 
5
- Full docs: [better-translate-placeholder.com/en/docs/adapters/nextjs](https://better-translate-placeholder.com/en/docs/adapters/nextjs)
5
+ If you also need client-side hooks, add `@better-translate/react` on top of this setup.
6
+
7
+ ## 1. Install the packages
8
+
9
+ ```sh
10
+ npm install @better-translate/core @better-translate/nextjs
11
+ ```
12
+
13
+ ## 2. Create the translator
14
+
15
+ Create `src/lib/i18n/config.ts`:
16
+
17
+ ```ts
18
+ import { configureTranslations } from "@better-translate/core";
19
+
20
+ const en = {
21
+ home: {
22
+ title: "Hello from Next.js",
23
+ },
24
+ } as const;
25
+
26
+ const es = {
27
+ home: {
28
+ title: "Hola desde Next.js",
29
+ },
30
+ } as const;
31
+
32
+ export const translator = await configureTranslations({
33
+ availableLocales: ["en", "es"] as const,
34
+ defaultLocale: "en",
35
+ fallbackLocale: "en",
36
+ messages: { en, es },
37
+ });
38
+ ```
39
+
40
+ ## 3. Add the routing file
41
+
42
+ Create `src/lib/i18n/routing.ts`:
43
+
44
+ ```ts
45
+ import { defineRouting } from "@better-translate/nextjs";
46
+
47
+ export const routing = defineRouting({
48
+ defaultLocale: "en",
49
+ locales: ["en", "es"] as const,
50
+ routeTemplate: "/[lang]",
51
+ });
52
+ ```
53
+
54
+ ## 4. Add the request and server helpers
55
+
56
+ Create `src/lib/i18n/request.ts`:
57
+
58
+ ```ts
59
+ import { getRequestConfig } from "@better-translate/nextjs/server";
60
+
61
+ import { translator } from "./config";
62
+
63
+ export const requestConfig = getRequestConfig(async () => ({
64
+ translator,
65
+ }));
66
+ ```
67
+
68
+ Create `src/lib/i18n/server.ts`:
69
+
70
+ ```ts
71
+ import { createServerHelpers } from "@better-translate/nextjs/server";
72
+
73
+ import { requestConfig } from "./request";
74
+
75
+ export const { getTranslations } = createServerHelpers(requestConfig);
76
+ ```
77
+
78
+ ## 5. Add locale-aware navigation
79
+
80
+ Create `src/lib/i18n/navigation.ts`:
81
+
82
+ ```tsx
83
+ "use client";
84
+
85
+ import Link from "next/link";
86
+ import { useParams, usePathname, useRouter } from "next/navigation";
87
+
88
+ import { createNavigationFunctions } from "@better-translate/nextjs/navigation";
89
+
90
+ import { routing } from "./routing";
91
+
92
+ export const {
93
+ Link: I18nLink,
94
+ usePathname: useI18nPathname,
95
+ useRouter: useI18nRouter,
96
+ } = createNavigationFunctions({
97
+ Link,
98
+ routing,
99
+ useParams,
100
+ usePathname,
101
+ useRouter,
102
+ });
103
+ ```
104
+
105
+ ## 6. Add the proxy
106
+
107
+ Create `src/proxy.ts`:
108
+
109
+ ```ts
110
+ import { createProxy, getProxyMatcher } from "@better-translate/nextjs/proxy";
111
+
112
+ import { routing } from "./lib/i18n/routing";
113
+
114
+ export const proxy = createProxy(routing);
115
+
116
+ export const config = {
117
+ matcher: getProxyMatcher(routing),
118
+ };
119
+ ```
120
+
121
+ ## 7. Render a translated page
122
+
123
+ Create `src/app/[lang]/page.tsx`:
124
+
125
+ ```tsx
126
+ import { setRequestLocale } from "@better-translate/nextjs/server";
127
+
128
+ import { getTranslations } from "../../lib/i18n/server";
129
+
130
+ export default async function HomePage({
131
+ params,
132
+ }: {
133
+ params: Promise<{ lang: string }>;
134
+ }) {
135
+ const { lang } = await params;
136
+
137
+ setRequestLocale(lang);
138
+
139
+ const t = await getTranslations();
140
+
141
+ return <h1>{t("home.title")}</h1>;
142
+ }
143
+ ```
144
+
145
+ ## 8. Add links that keep the locale
146
+
147
+ Create `src/components/language-links.tsx`:
148
+
149
+ ```tsx
150
+ "use client";
151
+
152
+ import { I18nLink } from "../lib/i18n/navigation";
153
+
154
+ export function LanguageLinks() {
155
+ return (
156
+ <nav>
157
+ <I18nLink href="/" locale="en">
158
+ English
159
+ </I18nLink>
160
+ {" | "}
161
+ <I18nLink href="/" locale="es">
162
+ Espanol
163
+ </I18nLink>
164
+ </nav>
165
+ );
166
+ }
167
+ ```
168
+
169
+ ## When to add the React adapter
170
+
171
+ Add `@better-translate/react` only when client components need `useTranslations()` or a provider.
172
+
173
+ ## Generate locale files automatically
174
+
175
+ Instead of writing every translation by hand, use the CLI to extract strings and generate locale files: [CLI guide](https://better-translate.com/en/docs/cli)
176
+
177
+ ## Examples
178
+
179
+ - [nextjs-example](https://github.com/jralvarenga/better-translate/tree/main/examples/nextjs-example)
180
+ - [nextjs-nested-locale-example](https://github.com/jralvarenga/better-translate/tree/main/examples/nextjs-nested-locale-example)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-translate/nextjs",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Next.js integration package for Better Translate.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -31,7 +31,11 @@
31
31
  },
32
32
  "./package.json": "./package.json"
33
33
  },
34
- "files": ["dist", "LICENSE", "README.md"],
34
+ "files": [
35
+ "dist",
36
+ "LICENSE",
37
+ "README.md"
38
+ ],
35
39
  "scripts": {
36
40
  "build": "tsup src/index.ts src/server.ts src/navigation.tsx src/proxy.ts --format esm,cjs --dts --clean",
37
41
  "check-types": "tsc --noEmit",
@@ -46,13 +50,13 @@
46
50
  "url": "git+https://github.com/jralvarenga/better-translate.git",
47
51
  "directory": "packages/nextjs"
48
52
  },
49
- "homepage": "https://github.com/jralvarenga/better-translate/tree/main/packages/nextjs#readme",
53
+ "homepage": "https://www.npmjs.com/package/@better-translate/nextjs",
50
54
  "bugs": {
51
55
  "url": "https://github.com/jralvarenga/better-translate/issues"
52
56
  },
53
57
  "dependencies": {
54
58
  "@formatjs/intl-localematcher": "^0.6.2",
55
- "@better-translate/core": "^1.0.0",
59
+ "@better-translate/core": "^2.2.0",
56
60
  "negotiator": "^1.0.0"
57
61
  },
58
62
  "peerDependencies": {
@@ -63,5 +67,11 @@
63
67
  "devDependencies": {
64
68
  "@repo/typescript-config": "*"
65
69
  },
66
- "keywords": ["i18n", "l10n", "nextjs", "translations", "typescript"]
70
+ "keywords": [
71
+ "i18n",
72
+ "l10n",
73
+ "nextjs",
74
+ "translations",
75
+ "typescript"
76
+ ]
67
77
  }