@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.
Files changed (2) hide show
  1. package/README.md +100 -9
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -1,17 +1,108 @@
1
- # @better-translate/react
1
+ # React
2
2
 
3
- `@better-translate/react` adds a provider and hooks on top of `@better-translate/core`. Use it in React web apps and Expo apps when components need access to translations and locale switching.
3
+ Use `@better-translate/react` when React components need translations and locale switching.
4
4
 
5
- To make plain `useTranslations()` fully typed across your app, augment the package once:
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 type { AppTranslator } from "./i18n";
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
- declare module "@better-translate/react" {
11
- interface BetterTranslateReactTypes {
12
- translator: AppTranslator;
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
- Full docs: [better-translate-placeholder.com/en/docs/adapters/react](https://better-translate-placeholder.com/en/docs/adapters/react)
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.0",
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://github.com/jralvarenga/better-translate/tree/main/packages/react#readme",
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.0.0"
42
+ "@better-translate/core": "^2.2.1"
43
43
  },
44
44
  "peerDependencies": {
45
45
  "react": "^19.0.0"