Skip to content

Commit 233af94

Browse files
committed
docs: Reorder extraction docs
1 parent d7bf7af commit 233af94

1 file changed

Lines changed: 53 additions & 53 deletions

File tree

β€Ždocs/src/pages/docs/usage/extraction.mdxβ€Ž

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,59 @@ AI-based translation can be automated with a translation management system like
282282

283283
</Callout>
284284

285+
## Static analysis [#static-analysis]
286+
287+
Message extraction relies on [static analysis](https://en.wikipedia.org/wiki/Static_program_analysis).
288+
289+
In practice, this means:
290+
291+
1. `t` must receive a literal string as its message argument
292+
2. `t` must be called in the same function body where it was retrieved from `useExtracted` or `getExtracted`
293+
294+
**Valid usage:**
295+
296+
```tsx
297+
import {useExtracted} from 'next-intl';
298+
299+
function Example({name}) {
300+
// βœ… Makes `t` available in this component
301+
const t = useExtracted();
302+
303+
// βœ… String literals are supported
304+
t('Hello there!');
305+
306+
// βœ… Arguments can be used for dynamic values
307+
t('Hello {name}!', {name});
308+
309+
function onClick() {
310+
// βœ… Usage in event handlers is supported
311+
t('You clicked the button!');
312+
}
313+
314+
// βœ… Usage inside JSX is supported
315+
return <button onClick={onClick}>{t('Click me')}</button>;
316+
}
317+
```
318+
319+
In contrast, these are examples of patterns that are **not supported**:
320+
321+
```tsx
322+
import {useExtracted} from 'next-intl';
323+
324+
function Example({key}) {
325+
const t = useExtracted();
326+
327+
// ❌ `key` is only known at runtime
328+
t(key);
329+
330+
// ❌ Passing `t` to another function
331+
getName(t);
332+
}
333+
334+
// ❌ Re-exporting the hook
335+
export const useExtractedExport = useExtracted;
336+
```
337+
285338
## Monorepos and external packages [#monorepos-external-packages]
286339

287340
Whenever your app pulls in external modules that call `useExtracted`, whether it's a sibling package in your monorepo or a reusable library installed in `node_modules`, there are two typical setups you can choose from.
@@ -401,56 +454,3 @@ const messages = {
401454
To avoid key collisions between packages, you can consider using [namespaces](#namespaces).
402455

403456
</Callout>
404-
405-
## Static analysis [#static-analysis]
406-
407-
Message extraction relies on [static analysis](https://en.wikipedia.org/wiki/Static_program_analysis).
408-
409-
In practice, this means:
410-
411-
1. `t` must receive a literal string as its message argument
412-
2. `t` must be called in the same function body where it was retrieved from `useExtracted` or `getExtracted`
413-
414-
**Valid usage:**
415-
416-
```tsx
417-
import {useExtracted} from 'next-intl';
418-
419-
function Example({name}) {
420-
// βœ… Makes `t` available in this component
421-
const t = useExtracted();
422-
423-
// βœ… String literals are supported
424-
t('Hello there!');
425-
426-
// βœ… Arguments can be used for dynamic values
427-
t('Hello {name}!', {name});
428-
429-
function onClick() {
430-
// βœ… Usage in event handlers is supported
431-
t('You clicked the button!');
432-
}
433-
434-
// βœ… Usage inside JSX is supported
435-
return <button onClick={onClick}>{t('Click me')}</button>;
436-
}
437-
```
438-
439-
In contrast, these are examples of patterns that are **not supported**:
440-
441-
```tsx
442-
import {useExtracted} from 'next-intl';
443-
444-
function Example({key}) {
445-
const t = useExtracted();
446-
447-
// ❌ `key` is only known at runtime
448-
t(key);
449-
450-
// ❌ Passing `t` to another function
451-
getName(t);
452-
}
453-
454-
// ❌ Re-exporting the hook
455-
export const useExtractedExport = useExtracted;
456-
```

0 commit comments

Comments
Β (0)