You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/pages/docs/usage/extraction.mdx
+59-11Lines changed: 59 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@ import Image from 'next/image';
2
2
importCalloutfrom'@/components/Callout';
3
3
importDetailsfrom'@/components/Details';
4
4
5
-
# `useExtracted`
5
+
# `useExtracted` (experimental)
6
6
7
7
As an alternative to managing namespaces and keys manually, `next-intl` provides an additional API that works similar to [`useTranslations`](/docs/usage/translations) but automatically extracts messages from your source files.
// Either 'json', 'po', or a custom format (see below)
77
77
format: 'json',
78
78
79
79
// Either 'infer' to automatically detect locales based on
@@ -268,11 +268,11 @@ it('renders', () => {
268
268
});
269
269
```
270
270
271
-
## Formatters
271
+
## Formats [#formats]
272
272
273
-
Currently, messages can be extracted as either JSON or PO files. Support for custom formatters is planned for a future release.
273
+
Messages can be extracted as JSON, PO, or with custom file formats.
274
274
275
-
When [`messages`](/docs/usage/plugin#messages) is configured, this will also set up a Turbo- or Webpack loader that will ensure loaded messages can be imported as plain JavaScript objects.
275
+
When [`messages`](/docs/usage/plugin#messages) is configured, this will also set up a Turbo- or Webpack loader that will ensure imported messages can be used as plain JavaScript objects.
276
276
277
277
For example, when using `format: 'po'`, messages can be imported as:
278
278
@@ -283,14 +283,14 @@ import {getRequestConfig} from 'next-intl/server';
When using this option, your messages will look like this:
296
296
@@ -300,7 +300,7 @@ When using this option, your messages will look like this:
300
300
}
301
301
```
302
302
303
-
Note that JSON files can only hold pairs of keys and values. To provide more context about a message like file references and descriptions, it's therefore recommended to use [PO files](#po) instead. Another alternative will be to use a custom JSON formatter in the future.
303
+
Note that JSON files can only hold pairs of keys and values. To provide more context about a message like file references and descriptions, it's therefore recommended to use [PO files](#formats-po) instead. Alternatively, you can create a [custom format](#formats-custom) to store additional metadata.
304
304
305
305
For local editing of JSON messages, you can use e.g. a [VSCode integration](/docs/workflows/vscode-integration) like i18n Ally:
306
306
@@ -318,7 +318,7 @@ For local editing of JSON messages, you can use e.g. a [VSCode integration](/doc
318
318
319
319
</Callout>
320
320
321
-
### PO formatter[#formatters-po]
321
+
### PO format[#formats-po]
322
322
323
323
When using this option, your messages will look like this:
324
324
@@ -331,10 +331,58 @@ msgstr "Right"
331
331
332
332
Besides the message key and the label itself, this format also supports optional descriptions and file references to all modules that consume this message.
333
333
334
-
For local editing of PO messages, you can use e.g. a tool like [Poedit](https://poedit.net/) (replacing keys with source text requires a pro license).
334
+
For local editing of .po files, you can use e.g. a tool like [Poedit](https://poedit.net/) (note however that replacing keys with source text requires a pro license).
335
335
336
336
<Callout>
337
337
338
338
**Tip:** AI-based translation can be automated with a translation management system like [Crowdin](/docs/workflows/localization-management).
339
339
340
340
</Callout>
341
+
342
+
### Custom format [#formats-custom]
343
+
344
+
To configure a custom format, you need to specify a codec along with an extension.
345
+
346
+
The codec can be created via `defineCodec` from `next-intl/extractor`:
347
+
348
+
```tsx filename="./CustomCodec.ts"
349
+
import {defineCodec} from'next-intl/extractor';
350
+
351
+
exportdefaultdefineCodec(() => ({
352
+
decode(content, context) {
353
+
// ...
354
+
},
355
+
356
+
encode(messages, context) {
357
+
// ...
358
+
},
359
+
360
+
toJSONString(content, context) {
361
+
// ...
362
+
}
363
+
}));
364
+
```
365
+
366
+
Then, reference it in your configuration along with an `extension`:
367
+
368
+
```tsx filename="next.config.ts"
369
+
const withNextIntl =createNextIntlPlugin({
370
+
experimental: {
371
+
messages: {
372
+
format: {
373
+
codec: './CustomCodec.ts',
374
+
extension: '.json'
375
+
}
376
+
// ...
377
+
}
378
+
}
379
+
});
380
+
```
381
+
382
+
See also the built-in [`codecs`](https://github.com/amannn/next-intl/tree/main/packages/next-intl/src/extractor/format/codecs) for inspiration.
383
+
384
+
<Callout>
385
+
386
+
Node.js supports native TypeScript execution like it's needed for the example above starting with v22.18. If you're on an older version, you should define your codec as a JavaScript file.
@@ -107,7 +107,7 @@ If you want to specify the locales explicitly, you can provide an array for `loc
107
107
locales: ['en', 'de'];
108
108
```
109
109
110
-
Configuring `experimental.messages` will also set up a Turbo- or Webpack loader that will ensure loaded messages can be imported as plain JavaScript objects (see [formatters](/docs/usage/extraction#formatters)).
110
+
Configuring `experimental.messages` will also set up a Turbo- or Webpack loader that will ensure loaded messages can be imported as plain JavaScript objects (see [formats](/docs/usage/extraction#formats)).
111
111
112
112
**Note:** The `messages` option should be used together with [`extract`](#extract) and [`srcPath`](#src-path).
0 commit comments