Skip to content

Commit cc95229

Browse files
cherkanovartclaude
andauthored
feat(compiler): add useLocale hook and setLocale/getLocale functions (#1964)
* feat(compiler): add useLocale hook and setLocale/getLocale functions - Add useLocale() hook for reactive locale access in components - Add setLocale() function for changing locale from anywhere - Add getLocale() function for non-reactive locale access - Add Next.js version compatibility warning for < 15 - Update README documentation with React Client API section Closes #1696 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(compiler): add useLocale hook and setLocale/getLocale functions - Add useLocale() hook for reactive locale access in components - Add setLocale() function for changing locale from anywhere - Add getLocale() function for non-reactive locale access - Update README documentation with React Client API section Closes #1696 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor(cli): update ensure-key-order tests to skip non-original keys - Rename test to reflect new behavior of skipping keys not present in the original input. - Adjust expected result to match the updated logic in the loader function. - Remove unnecessary key reordering logic from the loader implementation. * docs(compiler): document locale management with useLingoContext - Update README with useLingoContext() usage for locale switching - Remove unused useLocale/setLocale code in favor of useLingoContext() - Users can use useLingoContext() to get { locale, setLocale } Closes #1696 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * docs(react): correct wording in README for locale-switching components --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 5a1cb2f commit cc95229

File tree

3 files changed

+77
-23
lines changed

3 files changed

+77
-23
lines changed

.changeset/docs-locale-api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
---
3+
4+
Update documentation for locale management using useLingoContext hook.

packages/new-compiler/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,58 @@ LINGO_BUILD_MODE=cache-only npm run build
330330
2. **CI**: Generate real translations with `buildMode: "translate"` and real API keys
331331
3. **Production Build**: Use `buildMode: "cache-only"` (no API keys needed)
332332

333+
## React Client API
334+
335+
The compiler provides hooks and components for managing locale in your React components.
336+
337+
### `useLingoContext()`
338+
339+
Access the translation context to get the current locale and change it.
340+
341+
**Returns:**
342+
- `locale` (string): Current locale code
343+
- `setLocale` (function): Change the locale
344+
- `translations` (object): Translation dictionary
345+
- `isLoading` (boolean): Whether translations are loading
346+
347+
```tsx
348+
"use client";
349+
import { useLingoContext } from "@lingo.dev/compiler/react";
350+
351+
export function LanguageSwitcher() {
352+
const { locale, setLocale } = useLingoContext();
353+
354+
return (
355+
<select value={locale} onChange={(e) => setLocale(e.target.value)}>
356+
<option value="en">English</option>
357+
<option value="es">Español</option>
358+
<option value="de">Deutsch</option>
359+
</select>
360+
);
361+
}
362+
```
363+
364+
### `LocaleSwitcher` Component
365+
366+
A pre-built dropdown component for switching locales (no hooks needed):
367+
368+
```tsx
369+
"use client";
370+
import { LocaleSwitcher } from "@lingo.dev/compiler/react";
371+
372+
export function Header() {
373+
return (
374+
<LocaleSwitcher
375+
locales={[
376+
{ code: "en", label: "English" },
377+
{ code: "es", label: "Español" },
378+
{ code: "de", label: "Deutsch" },
379+
]}
380+
/>
381+
);
382+
}
383+
```
384+
333385
## Custom Locale Resolvers
334386

335387
Customize how locales are detected and persisted by providing custom resolver files:

packages/new-compiler/src/react/README.md

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Context provider that manages translations and locale switching for your entire
1919

2020
```tsx
2121
// app/layout.tsx
22-
import { LingoProvider } from "@lingo.dev/compiler-beta/react";
22+
import { LingoProvider } from "@lingo.dev/compiler/react";
2323

2424
export default function RootLayout({ children }) {
2525
return (
@@ -52,7 +52,7 @@ A dropdown component for switching between locales.
5252
// Client Component with Next.js integration
5353
"use client";
5454
import { useRouter } from "next/navigation";
55-
import { LocaleSwitcher } from "@lingo.dev/compiler-beta/react";
55+
import { LocaleSwitcher } from "@lingo.dev/compiler/react";
5656

5757
export function Header() {
5858
const router = useRouter();
@@ -84,7 +84,7 @@ Returns a translation function `t(hash)` for translating text in Client Componen
8484

8585
```tsx
8686
"use client";
87-
import { useTranslation } from "@lingo.dev/compiler-beta/react";
87+
import { useTranslation } from "@lingo.dev/compiler/react";
8888

8989
export function Welcome() {
9090
const t = useTranslation();
@@ -98,37 +98,35 @@ export function Welcome() {
9898
}
9999
```
100100

101-
### `useTranslationContext()`
101+
### `useLingoContext()`
102102

103-
Access the translation context directly.
103+
Access the full translation context directly. Use this for custom locale-switching components.
104104

105105
**Returns:**
106106

107107
- `locale` (string): Current locale
108108
- `setLocale` (function): Change locale
109109
- `translations` (object): Translation dictionary
110-
- `requestTranslation` (function): Request a translation
110+
- `registerHashes` (function): Register translation hashes
111111
- `isLoading` (boolean): Loading state
112112

113-
## Utility Functions
114-
115-
### `getLocaleFromCookies()`
116-
117-
Get the current locale from cookies (client-side).
118-
119-
**Parameters:**
120-
121-
- `cookieName` (string, optional): Cookie name, default: 'locale'
122-
- `defaultLocale` (string, optional): Default if not found, default: 'en'
123-
124-
**Returns:** Current locale string
125-
126113
**Example:**
127114

128115
```tsx
129-
import { getLocaleFromCookies } from "@lingo.dev/compiler-beta/react";
116+
"use client";
117+
import { useLingoContext } from "@lingo.dev/compiler/react";
118+
119+
export function LanguageSwitcher() {
120+
const { locale, setLocale } = useLingoContext();
130121

131-
const locale = getLocaleFromCookies(); // e.g., 'en'
122+
return (
123+
<select value={locale} onChange={(e) => setLocale(e.target.value)}>
124+
<option value="en">English</option>
125+
<option value="es">Español</option>
126+
<option value="de">Deutsch</option>
127+
</select>
128+
);
129+
}
132130
```
133131

134132
## How It Works
@@ -196,15 +194,15 @@ import type {
196194
LocaleConfig,
197195
TranslationFunction,
198196
TranslationContextType,
199-
} from "@lingo.dev/compiler-beta/react";
197+
} from "@lingo.dev/compiler/react";
200198
```
201199

202200
## Testing
203201

204202
When testing components that use translations:
205203

206204
```tsx
207-
import { LingoProvider } from "@lingo.dev/compiler-beta/react";
205+
import { LingoProvider } from "@lingo.dev/compiler/react";
208206

209207
test("my component", () => {
210208
render(

0 commit comments

Comments
 (0)