@better-translate/react 2.1.0 → 2.1.1
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 +100 -9
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,17 +1,108 @@
|
|
|
1
|
-
#
|
|
1
|
+
# React
|
|
2
2
|
|
|
3
|
-
`@better-translate/react`
|
|
3
|
+
Use `@better-translate/react` when React components need translations and locale switching.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## 1. Install the packages
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @better-translate/core @better-translate/react
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 2. Create the shared translator
|
|
12
|
+
|
|
13
|
+
Create `src/i18n.ts`:
|
|
6
14
|
|
|
7
15
|
```ts
|
|
8
|
-
import
|
|
16
|
+
import { configureTranslations } from "@better-translate/core";
|
|
17
|
+
|
|
18
|
+
const en = {
|
|
19
|
+
home: {
|
|
20
|
+
title: "Hello",
|
|
21
|
+
},
|
|
22
|
+
} as const;
|
|
23
|
+
|
|
24
|
+
const es = {
|
|
25
|
+
home: {
|
|
26
|
+
title: "Hola",
|
|
27
|
+
},
|
|
28
|
+
} as const;
|
|
29
|
+
|
|
30
|
+
export const translator = await configureTranslations({
|
|
31
|
+
availableLocales: ["en", "es"] as const,
|
|
32
|
+
defaultLocale: "en",
|
|
33
|
+
fallbackLocale: "en",
|
|
34
|
+
messages: { en, es },
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## 3. Wrap your app
|
|
39
|
+
|
|
40
|
+
Create or update `src/main.tsx`:
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
import React from "react";
|
|
44
|
+
import ReactDOM from "react-dom/client";
|
|
45
|
+
|
|
46
|
+
import { BetterTranslateProvider } from "@better-translate/react";
|
|
47
|
+
|
|
48
|
+
import { App } from "./app";
|
|
49
|
+
import { translator } from "./i18n";
|
|
50
|
+
|
|
51
|
+
ReactDOM.createRoot(document.getElementById("root")!).render(
|
|
52
|
+
<React.StrictMode>
|
|
53
|
+
<BetterTranslateProvider translator={translator}>
|
|
54
|
+
<App />
|
|
55
|
+
</BetterTranslateProvider>
|
|
56
|
+
</React.StrictMode>,
|
|
57
|
+
);
|
|
58
|
+
```
|
|
9
59
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
60
|
+
## 4. Read translations inside a component
|
|
61
|
+
|
|
62
|
+
Create `src/header.tsx`:
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
import { useTranslations } from "@better-translate/react";
|
|
66
|
+
|
|
67
|
+
import { translator } from "./i18n";
|
|
68
|
+
|
|
69
|
+
export function Header() {
|
|
70
|
+
const { locale, setLocale, t } = useTranslations<typeof translator>();
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
<header>
|
|
74
|
+
<h1>{t("home.title")}</h1>
|
|
75
|
+
<button onClick={() => setLocale("en")} disabled={locale === "en"}>
|
|
76
|
+
English
|
|
77
|
+
</button>
|
|
78
|
+
<button onClick={() => setLocale("es")} disabled={locale === "es"}>
|
|
79
|
+
Espanol
|
|
80
|
+
</button>
|
|
81
|
+
</header>
|
|
82
|
+
);
|
|
14
83
|
}
|
|
15
84
|
```
|
|
16
85
|
|
|
17
|
-
|
|
86
|
+
## When to use React only
|
|
87
|
+
|
|
88
|
+
Use only `@better-translate/core` + `@better-translate/react` when:
|
|
89
|
+
|
|
90
|
+
- your app is a React SPA
|
|
91
|
+
- your app is an Expo app
|
|
92
|
+
- locale changes happen in client components
|
|
93
|
+
|
|
94
|
+
If you are in Next.js App Router, keep this package for client hooks and add the Next.js adapter for routing and server helpers.
|
|
95
|
+
|
|
96
|
+
## Generate locale files automatically
|
|
97
|
+
|
|
98
|
+
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)
|
|
99
|
+
|
|
100
|
+
## Continue with
|
|
101
|
+
|
|
102
|
+
- [Expo setup](https://better-translate.com/en/docs/adapters/expo)
|
|
103
|
+
- [Next.js setup](https://better-translate.com/en/docs/adapters/nextjs)
|
|
104
|
+
|
|
105
|
+
## Examples
|
|
106
|
+
|
|
107
|
+
- [react-vite-example](https://github.com/jralvarenga/better-translate/tree/main/examples/react-vite-example)
|
|
108
|
+
- [expo-example](https://github.com/jralvarenga/better-translate/tree/main/examples/expo-example)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-translate/react",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "React context and hooks for Better Translate in web and native React apps.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -34,12 +34,12 @@
|
|
|
34
34
|
"url": "git+https://github.com/jralvarenga/better-translate.git",
|
|
35
35
|
"directory": "packages/react"
|
|
36
36
|
},
|
|
37
|
-
"homepage": "https://
|
|
37
|
+
"homepage": "https://better-translate.com",
|
|
38
38
|
"bugs": {
|
|
39
39
|
"url": "https://github.com/jralvarenga/better-translate/issues"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@better-translate/core": "^2.
|
|
42
|
+
"@better-translate/core": "^2.2.1"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"react": "^19.0.0"
|