@primereact/mcp 11.0.0-rc.4 → 11.1.0
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/data/llms/headless/components/dialog/api.json +1 -1
- package/data/llms/headless/components/drawer/api.json +1 -1
- package/data/llms/headless/components/sidebar/api.json +1029 -54
- package/data/llms/headless/guides/misc/internationalization.md +282 -0
- package/data/llms/headless/guides/misc/locale.md +280 -0
- package/data/llms/llms-full.txt +9822 -1377
- package/data/llms/llms.txt +15 -0
- package/data/llms/primitive/components/dialog/api.json +1 -1
- package/data/llms/primitive/components/drawer/api.json +1 -1
- package/data/llms/primitive/components/sidebar/api.json +127 -33
- package/data/llms/primitive/guides/migration/updating-to-v11.md +2219 -0
- package/data/llms/primitive/guides/misc/internationalization.md +287 -0
- package/data/llms/primitive/guides/misc/locale.md +280 -0
- package/data/llms/styled/add-ons/designer/ci.md +273 -0
- package/data/llms/styled/add-ons/designer/guide.md +99 -0
- package/data/llms/styled/add-ons/designer/overview.md +194 -0
- package/data/llms/styled/add-ons/uikit/guide/v3.md +181 -0
- package/data/llms/styled/add-ons/uikit/guide/v4.md +163 -0
- package/data/llms/styled/add-ons/uikit/overview.md +204 -0
- package/data/llms/styled/components/datatable.md +8 -5
- package/data/llms/styled/components/menu.md +39 -41
- package/data/llms/styled/guides/configuration.md +198 -0
- package/data/llms/styled/guides/form/formik.md +448 -0
- package/data/llms/styled/guides/form/react-hook-form.md +503 -0
- package/data/llms/styled/guides/form/tanstack.md +502 -0
- package/data/llms/styled/guides/migration/updating-to-v11.md +2219 -0
- package/data/llms/styled/guides/misc/internationalization.md +379 -0
- package/data/llms/styled/guides/misc/locale.md +280 -0
- package/data/llms/tailwind/components/menu.md +5 -5
- package/data/llms/tailwind/components/tooltip.md +4 -13
- package/data/llms/tailwind/guides/misc/internationalization.md +287 -0
- package/data/llms/tailwind/guides/misc/locale.md +280 -0
- package/data/manifest.json +347 -32
- package/data/mcp-data.json +263 -8
- package/dist/index.js +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
# Internationalization and Localization
|
|
2
|
+
|
|
3
|
+
Translating component messages and configuring regional settings such as date formats and the first day of the week.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Components ship with a set of built-in messages such as filter operator labels, month names and ARIA descriptions. These messages live in a locale registry that is shared across PrimeReact, PrimeVue and PrimeNG through the `@primeuix/locale` package, and are re-exported from `@primereact/core/locale`.
|
|
8
|
+
|
|
9
|
+
English is registered by default, so no configuration is required until another language is needed.
|
|
10
|
+
|
|
11
|
+
Setting a language is enough for components to pick it up. No per-component prop is involved. Switching the language below re-renders the calendar with translated month and day names, moves the first day of the week, and updates the button label.
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
'use client';
|
|
15
|
+
import { ChevronDown } from '@primeicons/react/chevron-down';
|
|
16
|
+
import { ChevronLeft } from '@primeicons/react/chevron-left';
|
|
17
|
+
import { ChevronRight } from '@primeicons/react/chevron-right';
|
|
18
|
+
import { defineLocale, en, Locale, useLocale } from '@primereact/core/locale';
|
|
19
|
+
import { Button } from '@primereact/ui/button';
|
|
20
|
+
import { DatePicker, type DatePickerRootProps, type DatePickerRootValueChangeEvent } from '@primereact/ui/datepicker';
|
|
21
|
+
import { Select, type SelectValueChangeEvent } from '@primereact/ui/select';
|
|
22
|
+
import * as React from 'react';
|
|
23
|
+
|
|
24
|
+
// Spreading `en` keeps every untranslated key readable instead of resolving to undefined.
|
|
25
|
+
defineLocale('de', {
|
|
26
|
+
...en,
|
|
27
|
+
dayNamesMin: ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'],
|
|
28
|
+
monthNames: ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
|
|
29
|
+
today: 'Heute',
|
|
30
|
+
clear: 'Löschen',
|
|
31
|
+
firstDayOfWeek: 1
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
defineLocale('es', {
|
|
35
|
+
...en,
|
|
36
|
+
dayNamesMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sá'],
|
|
37
|
+
monthNames: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'],
|
|
38
|
+
today: 'Hoy',
|
|
39
|
+
clear: 'Limpiar',
|
|
40
|
+
firstDayOfWeek: 1
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const languages = [
|
|
44
|
+
{ label: 'English', value: 'en' },
|
|
45
|
+
{ label: 'Deutsch', value: 'de' },
|
|
46
|
+
{ label: 'Español', value: 'es' }
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
export default function SwitcherDemo() {
|
|
50
|
+
const [date, setDate] = React.useState<DatePickerRootProps['value'] | null>(null);
|
|
51
|
+
const { lang, t } = useLocale();
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<div className="flex flex-col items-center gap-4">
|
|
55
|
+
<Select.Root
|
|
56
|
+
value={lang}
|
|
57
|
+
onValueChange={(event: SelectValueChangeEvent) => Locale.use(event.value as string)}
|
|
58
|
+
options={languages}
|
|
59
|
+
optionLabel="label"
|
|
60
|
+
optionValue="value"
|
|
61
|
+
className="w-full md:w-56"
|
|
62
|
+
>
|
|
63
|
+
<Select.Trigger>
|
|
64
|
+
<Select.Value />
|
|
65
|
+
<Select.Indicator>
|
|
66
|
+
<ChevronDown />
|
|
67
|
+
</Select.Indicator>
|
|
68
|
+
</Select.Trigger>
|
|
69
|
+
<Select.Portal>
|
|
70
|
+
<Select.Positioner>
|
|
71
|
+
<Select.Popup>
|
|
72
|
+
<Select.List />
|
|
73
|
+
</Select.Popup>
|
|
74
|
+
</Select.Positioner>
|
|
75
|
+
</Select.Portal>
|
|
76
|
+
</Select.Root>
|
|
77
|
+
|
|
78
|
+
<DatePicker.Root value={date} onValueChange={(event: DatePickerRootValueChangeEvent) => setDate(event.value)}>
|
|
79
|
+
<DatePicker.Body>
|
|
80
|
+
<DatePicker.Panel>
|
|
81
|
+
<DatePicker.Calendar>
|
|
82
|
+
<DatePicker.Header className="bg-transparent!">
|
|
83
|
+
<DatePicker.Prev as={Button} iconOnly variant="text" rounded severity="secondary" size="small">
|
|
84
|
+
<ChevronLeft />
|
|
85
|
+
</DatePicker.Prev>
|
|
86
|
+
<DatePicker.Title>
|
|
87
|
+
<DatePicker.SelectMonth />
|
|
88
|
+
<DatePicker.SelectYear />
|
|
89
|
+
<DatePicker.Decade />
|
|
90
|
+
</DatePicker.Title>
|
|
91
|
+
<DatePicker.Next as={Button} iconOnly variant="text" rounded severity="secondary" size="small">
|
|
92
|
+
<ChevronRight />
|
|
93
|
+
</DatePicker.Next>
|
|
94
|
+
</DatePicker.Header>
|
|
95
|
+
<DatePicker.Table>
|
|
96
|
+
<DatePicker.TableHead />
|
|
97
|
+
<DatePicker.TableBody />
|
|
98
|
+
<DatePicker.TableBody view="month" />
|
|
99
|
+
<DatePicker.TableBody view="year" />
|
|
100
|
+
</DatePicker.Table>
|
|
101
|
+
</DatePicker.Calendar>
|
|
102
|
+
</DatePicker.Panel>
|
|
103
|
+
</DatePicker.Body>
|
|
104
|
+
</DatePicker.Root>
|
|
105
|
+
|
|
106
|
+
<Button severity="secondary" onClick={() => setDate(null)}>
|
|
107
|
+
{t('clear')}
|
|
108
|
+
</Button>
|
|
109
|
+
</div>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Import
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
import { $t, $l, defineLocale, updateLocale, useLocale, Locale, LocaleService, en } from '@primereact/core/locale';
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Setting the Language
|
|
121
|
+
|
|
122
|
+
`Locale.use` selects the active language. Every component reading a message re-renders when it changes, so this works both at startup and later in response to a language switcher.
|
|
123
|
+
|
|
124
|
+
```js
|
|
125
|
+
import { Locale } from '@primereact/core/locale';
|
|
126
|
+
|
|
127
|
+
Locale.use('de');
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Only `en` is built in, so any other language has to be registered first. See [Adding a Language](#adding-a-language) below.
|
|
131
|
+
|
|
132
|
+
`PrimeReactProvider` also accepts a `locale` prop, which sets the initial language for the components beneath it.
|
|
133
|
+
|
|
134
|
+
```jsx
|
|
135
|
+
import { PrimeReactProvider } from '@primereact/core';
|
|
136
|
+
|
|
137
|
+
<PrimeReactProvider locale="de">
|
|
138
|
+
<App />
|
|
139
|
+
</PrimeReactProvider>;
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Adding a Language
|
|
143
|
+
|
|
144
|
+
`defineLocale` registers a new language.
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
import { defineLocale, Locale } from '@primereact/core/locale';
|
|
148
|
+
|
|
149
|
+
defineLocale('es', {
|
|
150
|
+
clear: 'Limpiar',
|
|
151
|
+
apply: 'Aplicar',
|
|
152
|
+
accept: 'Sí',
|
|
153
|
+
reject: 'No',
|
|
154
|
+
dayNames: ['domingo', 'lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado'],
|
|
155
|
+
dayNamesShort: ['dom', 'lun', 'mar', 'mié', 'jue', 'vie', 'sáb'],
|
|
156
|
+
dayNamesMin: ['D', 'L', 'M', 'X', 'J', 'V', 'S'],
|
|
157
|
+
monthNames: ['enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre'],
|
|
158
|
+
monthNamesShort: ['ene', 'feb', 'mar', 'abr', 'may', 'jun', 'jul', 'ago', 'sep', 'oct', 'nov', 'dic']
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
Locale.use('es');
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
A registered language must be complete. Keys that are left out resolve to `undefined` rather than falling back to English, which renders as an empty label. The practical approach is to spread the built-in English messages and override from there:
|
|
165
|
+
|
|
166
|
+
```js
|
|
167
|
+
import { defineLocale, en } from '@primereact/core/locale';
|
|
168
|
+
|
|
169
|
+
defineLocale('es', {
|
|
170
|
+
...en,
|
|
171
|
+
clear: 'Limpiar',
|
|
172
|
+
apply: 'Aplicar'
|
|
173
|
+
});
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
The `en` object holds the built-in English messages and is exported for exactly this purpose. Ready-made translations from [PrimeLocale](#ready-made-translations) already cover every key, so spreading is only needed for hand-written translations.
|
|
177
|
+
|
|
178
|
+
The provider accepts a `locales` map, which registers languages and selects one in a single step. Anything listed here is registered before `locale` is applied.
|
|
179
|
+
|
|
180
|
+
```jsx
|
|
181
|
+
import { PrimeReactProvider } from '@primereact/core';
|
|
182
|
+
|
|
183
|
+
<PrimeReactProvider locale="es" locales={{ es: spanishMessages }}>
|
|
184
|
+
<App />
|
|
185
|
+
</PrimeReactProvider>;
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Updating an Existing Language
|
|
189
|
+
|
|
190
|
+
`updateLocale` merges new values into a language that is already registered, leaving the remaining keys untouched. It is useful for overriding a handful of labels without redefining a full translation.
|
|
191
|
+
|
|
192
|
+
```js
|
|
193
|
+
import { updateLocale } from '@primereact/core/locale';
|
|
194
|
+
|
|
195
|
+
updateLocale('en', {
|
|
196
|
+
clear: 'Reset',
|
|
197
|
+
apply: 'Confirm'
|
|
198
|
+
});
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
If the language does not exist yet, `updateLocale` registers it, behaving like `defineLocale`.
|
|
202
|
+
|
|
203
|
+
## Reading Messages
|
|
204
|
+
|
|
205
|
+
### In Components
|
|
206
|
+
|
|
207
|
+
`useLocale` is the React hook for reading the active locale. It re-renders the calling component when the language changes.
|
|
208
|
+
|
|
209
|
+
```jsx
|
|
210
|
+
import { Button } from '@primereact/ui/button';
|
|
211
|
+
import { useLocale } from '@primereact/core/locale';
|
|
212
|
+
|
|
213
|
+
function ClearFilterButton({ onClear }) {
|
|
214
|
+
const { t } = useLocale();
|
|
215
|
+
|
|
216
|
+
return (
|
|
217
|
+
<Button severity="secondary" onClick={onClear}>
|
|
218
|
+
{t('clear')}
|
|
219
|
+
</Button>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
| Field | Description |
|
|
225
|
+
| ---------- | --------------------------------------------- |
|
|
226
|
+
| `lang` | Active language code, such as `en` or `de`. |
|
|
227
|
+
| `messages` | Full message object of the active language. |
|
|
228
|
+
| `t` | Translates a key against the active language. |
|
|
229
|
+
|
|
230
|
+
Re-rendering on a language change relies on the context published by `PrimeReactProvider`, which applications normally have at their root. A component rendered outside any provider still reads the correct messages on first render, but will not update when the language changes later.
|
|
231
|
+
|
|
232
|
+
### Outside Components
|
|
233
|
+
|
|
234
|
+
`$t` performs the same lookup without a React context, which suits utilities, event handlers and module-level code.
|
|
235
|
+
|
|
236
|
+
```js
|
|
237
|
+
import { $t } from '@primereact/core/locale';
|
|
238
|
+
|
|
239
|
+
$t('clear'); // 'Clear'
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
Nested keys use dot notation.
|
|
243
|
+
|
|
244
|
+
```js
|
|
245
|
+
$t('aria.selectRow'); // 'Row Selected'
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
Messages containing placeholders accept replacement values. Positional placeholders such as `{0}` are filled from the argument order, while named ones are filled from an object.
|
|
249
|
+
|
|
250
|
+
```js
|
|
251
|
+
$t('searchMessage', 5); // '5 results are available'
|
|
252
|
+
$t('selectionMessage', 3); // '3 items selected'
|
|
253
|
+
$t('aria.stars', { star: 4 }); // '4 stars'
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
## Reacting to Language Changes
|
|
257
|
+
|
|
258
|
+
Language changes emit a `locale:change` event. Components handle this automatically through `useLocale`; the event is only needed for code living outside React, such as syncing a third-party library.
|
|
259
|
+
|
|
260
|
+
```js
|
|
261
|
+
import { LocaleService } from '@primereact/core/locale';
|
|
262
|
+
|
|
263
|
+
const onChange = ({ lang }) => console.log('Language is now', lang);
|
|
264
|
+
|
|
265
|
+
LocaleService.on('locale:change', onChange);
|
|
266
|
+
|
|
267
|
+
// remove the listener when it is no longer needed
|
|
268
|
+
LocaleService.off('locale:change', onChange);
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Inspecting the Registry
|
|
272
|
+
|
|
273
|
+
`$l` returns a facade over the registry, which helps when building a language switcher.
|
|
274
|
+
|
|
275
|
+
```js
|
|
276
|
+
import { $l } from '@primereact/core/locale';
|
|
277
|
+
|
|
278
|
+
$l().langs; // ['en', 'es']
|
|
279
|
+
$l().get('es'); // message object of the Spanish locale
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
## Message Keys
|
|
283
|
+
|
|
284
|
+
These components read their labels from the active language:
|
|
285
|
+
|
|
286
|
+
| Component | Keys |
|
|
287
|
+
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
288
|
+
| DatePicker | `dayNames`, `dayNamesShort`, `dayNamesMin`, `monthNames`, `monthNamesShort`, `today`, `weekHeader`, `clear`, `am`, `pm`, `firstDayOfWeek`, `dateFormat` |
|
|
289
|
+
| FileUpload | `fileSizeTypes` |
|
|
290
|
+
| Paginator | `aria.firstPageLabel`, `aria.prevPageLabel`, `aria.nextPageLabel`, `aria.lastPageLabel`, `aria.pageLabel` |
|
|
291
|
+
| Sidebar | `aria.toggleSidebar` |
|
|
292
|
+
|
|
293
|
+
The registry holds a larger set covering filter operators, password strength labels, empty-state messages and the remaining `aria` labels. Those are reserved for components that do not consume them yet, so translating them has no visible effect today.
|
|
294
|
+
|
|
295
|
+
Not every entry is a string. `firstDayOfWeek` is a number, `showMonthAfterYear` is a boolean, and `dayNames`, `monthNames` and `fileSizeTypes` are arrays. `searchMessage` and similar entries carry a `{0}` placeholder.
|
|
296
|
+
|
|
297
|
+
The full set with default English values is exported as `en`, which can also be inspected at runtime:
|
|
298
|
+
|
|
299
|
+
```js
|
|
300
|
+
import { en } from '@primereact/core/locale';
|
|
301
|
+
|
|
302
|
+
Object.keys(en); // every top-level key
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
## Ready-Made Translations
|
|
306
|
+
|
|
307
|
+
Writing a translation from scratch is rarely necessary. The community maintained [PrimeLocale](https://github.com/primefaces/primelocale) repository publishes complete translations for 70+ languages, shared across all Prime libraries. Each file covers every built-in message key, so nothing is left resolving to `undefined`.
|
|
308
|
+
|
|
309
|
+
### Copying a File
|
|
310
|
+
|
|
311
|
+
Grab the JSON file for the desired language from the repository, for example `es.json`, and place it in the project.
|
|
312
|
+
|
|
313
|
+
Each file wraps its messages in a single key named after the language:
|
|
314
|
+
|
|
315
|
+
```json
|
|
316
|
+
{
|
|
317
|
+
"es": {
|
|
318
|
+
"clear": "Limpiar",
|
|
319
|
+
"apply": "Aplicar",
|
|
320
|
+
"aria": { "selectRow": "Seleccionar fila" }
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
That wrapper has to be unwrapped before registering, otherwise every lookup resolves to `undefined`:
|
|
326
|
+
|
|
327
|
+
```js
|
|
328
|
+
import { defineLocale, Locale } from '@primereact/core/locale';
|
|
329
|
+
import es from './locales/es.json';
|
|
330
|
+
|
|
331
|
+
defineLocale('es', es.es);
|
|
332
|
+
Locale.use('es');
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
The same unwrapping applies when the language is registered through the provider:
|
|
336
|
+
|
|
337
|
+
```jsx
|
|
338
|
+
import { PrimeReactProvider } from '@primereact/core';
|
|
339
|
+
import es from './locales/es.json';
|
|
340
|
+
|
|
341
|
+
<PrimeReactProvider locale="es" locales={{ es: es.es }}>
|
|
342
|
+
<App />
|
|
343
|
+
</PrimeReactProvider>;
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
### Installing the Package
|
|
347
|
+
|
|
348
|
+
The translations are also published to npm, which avoids maintaining copies by hand.
|
|
349
|
+
|
|
350
|
+
```bash
|
|
351
|
+
npm install primelocale
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
```js
|
|
355
|
+
import { defineLocale, Locale } from '@primereact/core/locale';
|
|
356
|
+
import { es } from 'primelocale/js/es.js';
|
|
357
|
+
|
|
358
|
+
defineLocale('es', es);
|
|
359
|
+
Locale.use('es');
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
The package entry points export the messages directly, so no unwrapping is needed there.
|
|
363
|
+
|
|
364
|
+
Regional variants are named with an underscore in the package and a hyphen in the JSON files. Brazilian Portuguese is `primelocale/js/pt_BR.js` when imported and `pt-BR.json` when copied.
|
|
365
|
+
|
|
366
|
+
### Adjusting a Translation
|
|
367
|
+
|
|
368
|
+
A ready-made translation can be tuned without editing the file, by layering overrides on top of it:
|
|
369
|
+
|
|
370
|
+
```js
|
|
371
|
+
defineLocale('es', es.es);
|
|
372
|
+
updateLocale('es', { clear: 'Borrar' });
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
Contributions of new languages and corrections are welcome in the PrimeLocale repository, and benefit every Prime library at once.
|
|
376
|
+
|
|
377
|
+
## Coming from v10
|
|
378
|
+
|
|
379
|
+
The locale helpers from `primereact/api` were replaced. The [migration guide](/docs/styled/guides/migration/updating-to-v11) maps each one to its replacement.
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
# Internationalization and Localization
|
|
2
|
+
|
|
3
|
+
Translating component messages and configuring regional settings such as date formats and the first day of the week.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Components ship with a set of built-in messages such as filter operator labels, month names and ARIA descriptions. These messages live in a locale registry that is shared across PrimeReact, PrimeVue and PrimeNG through the `@primeuix/locale` package, and are re-exported from `@primereact/core/locale`.
|
|
8
|
+
|
|
9
|
+
English is registered by default, so no configuration is required until another language is needed.
|
|
10
|
+
|
|
11
|
+
## Import
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { $t, $l, defineLocale, updateLocale, useLocale, Locale, LocaleService, en } from '@primereact/core/locale';
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Setting the Language
|
|
18
|
+
|
|
19
|
+
`Locale.use` selects the active language. Every component reading a message re-renders when it changes, so this works both at startup and later in response to a language switcher.
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { Locale } from '@primereact/core/locale';
|
|
23
|
+
|
|
24
|
+
Locale.use('de');
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Only `en` is built in, so any other language has to be registered first — see [Adding a Language](#adding-a-language) below.
|
|
28
|
+
|
|
29
|
+
`PrimeReactProvider` also accepts a `locale` prop, which sets the initial language for the components beneath it.
|
|
30
|
+
|
|
31
|
+
```jsx
|
|
32
|
+
import { PrimeReactProvider } from '@primereact/core';
|
|
33
|
+
|
|
34
|
+
<PrimeReactProvider locale="de">
|
|
35
|
+
<App />
|
|
36
|
+
</PrimeReactProvider>;
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Adding a Language
|
|
40
|
+
|
|
41
|
+
`defineLocale` registers a new language.
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
import { defineLocale, Locale } from '@primereact/core/locale';
|
|
45
|
+
|
|
46
|
+
defineLocale('es', {
|
|
47
|
+
clear: 'Limpiar',
|
|
48
|
+
apply: 'Aplicar',
|
|
49
|
+
accept: 'Sí',
|
|
50
|
+
reject: 'No',
|
|
51
|
+
dayNames: ['domingo', 'lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado'],
|
|
52
|
+
dayNamesShort: ['dom', 'lun', 'mar', 'mié', 'jue', 'vie', 'sáb'],
|
|
53
|
+
dayNamesMin: ['D', 'L', 'M', 'X', 'J', 'V', 'S'],
|
|
54
|
+
monthNames: ['enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre'],
|
|
55
|
+
monthNamesShort: ['ene', 'feb', 'mar', 'abr', 'may', 'jun', 'jul', 'ago', 'sep', 'oct', 'nov', 'dic']
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
Locale.use('es');
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
A registered language must be complete. Keys that are left out resolve to `undefined` rather than falling back to English, which renders as an empty label. The practical approach is to spread the built-in English messages and override from there:
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
import { defineLocale, en } from '@primereact/core/locale';
|
|
65
|
+
|
|
66
|
+
defineLocale('es', {
|
|
67
|
+
...en,
|
|
68
|
+
clear: 'Limpiar',
|
|
69
|
+
apply: 'Aplicar'
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The `en` object holds the built-in English messages and is exported for exactly this purpose. Ready-made translations from [PrimeLocale](#ready-made-translations) already cover every key, so spreading is only needed for hand-written translations.
|
|
74
|
+
|
|
75
|
+
The provider accepts a `locales` map, which registers languages and selects one in a single step. Anything listed here is registered before `locale` is applied.
|
|
76
|
+
|
|
77
|
+
```jsx
|
|
78
|
+
import { PrimeReactProvider } from '@primereact/core';
|
|
79
|
+
|
|
80
|
+
<PrimeReactProvider locale="es" locales={{ es: spanishMessages }}>
|
|
81
|
+
<App />
|
|
82
|
+
</PrimeReactProvider>;
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Updating an Existing Language
|
|
86
|
+
|
|
87
|
+
`updateLocale` merges new values into a language that is already registered, leaving the remaining keys untouched. It is useful for overriding a handful of labels without redefining a full translation.
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
import { updateLocale } from '@primereact/core/locale';
|
|
91
|
+
|
|
92
|
+
updateLocale('en', {
|
|
93
|
+
clear: 'Reset',
|
|
94
|
+
apply: 'Confirm'
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
If the language does not exist yet, `updateLocale` registers it, behaving like `defineLocale`.
|
|
99
|
+
|
|
100
|
+
## Reading Messages
|
|
101
|
+
|
|
102
|
+
### In Components
|
|
103
|
+
|
|
104
|
+
`useLocale` is the React hook for reading the active locale. It re-renders the calling component when the language changes.
|
|
105
|
+
|
|
106
|
+
```jsx
|
|
107
|
+
import { useLocale } from '@primereact/core/locale';
|
|
108
|
+
|
|
109
|
+
function ClearButton() {
|
|
110
|
+
const { t, lang, messages } = useLocale();
|
|
111
|
+
|
|
112
|
+
return <button aria-label={t('clear')}>{t('clear')}</button>;
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
| Field | Description |
|
|
117
|
+
| ---------- | --------------------------------------------- |
|
|
118
|
+
| `lang` | Active language code, such as `en` or `de`. |
|
|
119
|
+
| `messages` | Full message object of the active language. |
|
|
120
|
+
| `t` | Translates a key against the active language. |
|
|
121
|
+
|
|
122
|
+
Re-rendering on a language change relies on the context published by `PrimeReactProvider`, which applications normally have at their root. A component rendered outside any provider still reads the correct messages on first render, but will not update when the language changes later.
|
|
123
|
+
|
|
124
|
+
### Outside Components
|
|
125
|
+
|
|
126
|
+
`$t` performs the same lookup without a React context, which suits utilities, event handlers and module-level code.
|
|
127
|
+
|
|
128
|
+
```js
|
|
129
|
+
import { $t } from '@primereact/core/locale';
|
|
130
|
+
|
|
131
|
+
$t('clear'); // 'Clear'
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Nested keys use dot notation.
|
|
135
|
+
|
|
136
|
+
```js
|
|
137
|
+
$t('aria.selectRow'); // 'Row Selected'
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Messages containing placeholders accept replacement values. Positional placeholders such as `{0}` are filled from the argument order, while named ones are filled from an object.
|
|
141
|
+
|
|
142
|
+
```js
|
|
143
|
+
$t('searchMessage', 5); // '5 results are available'
|
|
144
|
+
$t('selectionMessage', 3); // '3 items selected'
|
|
145
|
+
$t('aria.stars', { star: 4 }); // '4 stars'
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Reacting to Language Changes
|
|
149
|
+
|
|
150
|
+
Language changes emit a `locale:change` event. Components handle this automatically through `useLocale`; the event is only needed for code living outside React, such as syncing a third-party library.
|
|
151
|
+
|
|
152
|
+
```js
|
|
153
|
+
import { LocaleService } from '@primereact/core/locale';
|
|
154
|
+
|
|
155
|
+
const onChange = ({ lang }) => console.log('Language is now', lang);
|
|
156
|
+
|
|
157
|
+
LocaleService.on('locale:change', onChange);
|
|
158
|
+
|
|
159
|
+
// remove the listener when it is no longer needed
|
|
160
|
+
LocaleService.off('locale:change', onChange);
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Inspecting the Registry
|
|
164
|
+
|
|
165
|
+
`$l` returns a facade over the registry, which helps when building a language switcher.
|
|
166
|
+
|
|
167
|
+
```js
|
|
168
|
+
import { $l } from '@primereact/core/locale';
|
|
169
|
+
|
|
170
|
+
$l().langs; // ['en', 'es']
|
|
171
|
+
$l().get('es'); // message object of the Spanish locale
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Message Keys
|
|
175
|
+
|
|
176
|
+
Built-in messages are grouped as follows.
|
|
177
|
+
|
|
178
|
+
| Group | Keys |
|
|
179
|
+
| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
180
|
+
| Filter operators | `startsWith`, `contains`, `notContains`, `endsWith`, `equals`, `notEquals`, `noFilter`, `lt`, `lte`, `gt`, `gte`, `dateIs`, `dateIsNot`, `dateBefore`, `dateAfter` |
|
|
181
|
+
| Filter menu | `clear`, `apply`, `matchAll`, `matchAny`, `addRule`, `removeRule` |
|
|
182
|
+
| Actions | `accept`, `reject`, `choose`, `upload`, `cancel`, `completed`, `pending` |
|
|
183
|
+
| Dates | `dayNames`, `dayNamesShort`, `dayNamesMin`, `monthNames`, `monthNamesShort`, `today`, `weekHeader`, `firstDayOfWeek`, `dateFormat` |
|
|
184
|
+
| Date navigation | `chooseYear`, `chooseMonth`, `chooseDate`, `prevDecade`, `nextDecade`, `prevYear`, `nextYear`, `prevMonth`, `nextMonth`, `prevHour`, `nextHour`, `prevMinute`, `nextMinute`, `prevSecond`, `nextSecond`, `am`, `pm` |
|
|
185
|
+
| Password strength | `weak`, `medium`, `strong`, `passwordPrompt` |
|
|
186
|
+
| Empty and status messages | `emptyMessage`, `emptyFilterMessage`, `emptySearchMessage`, `emptySelectionMessage`, `searchMessage`, `selectionMessage` |
|
|
187
|
+
| Files | `fileSizeTypes`, `fileChosenMessage`, `noFileChosenMessage` |
|
|
188
|
+
| Regional settings | `firstDayOfWeek`, `dateFormat`, `showMonthAfterYear` |
|
|
189
|
+
| Accessibility | Nested under `aria`, covering screen reader labels such as `aria.selectRow`, `aria.expandRow`, `aria.showFilterMenu`, `aria.zoomImage`, `aria.listLabel` and pagination labels. |
|
|
190
|
+
|
|
191
|
+
Not every entry is a string. `firstDayOfWeek` is a number, `showMonthAfterYear` is a boolean, and `dayNames`, `monthNames` and `fileSizeTypes` are arrays.
|
|
192
|
+
|
|
193
|
+
The complete list with default English values is in `packages/@primereact/core/src/locale/defaultLocale.ts`.
|
|
194
|
+
|
|
195
|
+
## Ready-Made Translations
|
|
196
|
+
|
|
197
|
+
Writing a translation from scratch is rarely necessary. The community maintained [PrimeLocale](https://github.com/primefaces/primelocale) repository publishes complete translations for 70+ languages, shared across all Prime libraries. Each file covers every built-in message key, so nothing is left resolving to `undefined`.
|
|
198
|
+
|
|
199
|
+
### Copying a File
|
|
200
|
+
|
|
201
|
+
Grab the JSON file for the desired language from the repository, for example `tr.json`, and place it in the project.
|
|
202
|
+
|
|
203
|
+
Each file wraps its messages in a single key named after the language:
|
|
204
|
+
|
|
205
|
+
```json
|
|
206
|
+
{
|
|
207
|
+
"tr": {
|
|
208
|
+
"clear": "Temizle",
|
|
209
|
+
"apply": "Uygula",
|
|
210
|
+
"aria": { "selectRow": "Satır Seçildi" }
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
That wrapper has to be unwrapped before registering, otherwise every lookup resolves to `undefined`:
|
|
216
|
+
|
|
217
|
+
```js
|
|
218
|
+
import { defineLocale, Locale } from '@primereact/core/locale';
|
|
219
|
+
import tr from './locales/tr.json';
|
|
220
|
+
|
|
221
|
+
defineLocale('tr', tr.tr);
|
|
222
|
+
Locale.use('tr');
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
The same unwrapping applies when the language is registered through the provider:
|
|
226
|
+
|
|
227
|
+
```jsx
|
|
228
|
+
import { PrimeReactProvider } from '@primereact/core';
|
|
229
|
+
import tr from './locales/tr.json';
|
|
230
|
+
|
|
231
|
+
<PrimeReactProvider locale="tr" locales={{ tr: tr.tr }}>
|
|
232
|
+
<App />
|
|
233
|
+
</PrimeReactProvider>;
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### Installing the Package
|
|
237
|
+
|
|
238
|
+
The translations are also published to npm, which avoids maintaining copies by hand.
|
|
239
|
+
|
|
240
|
+
```bash
|
|
241
|
+
npm install primelocale
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
```js
|
|
245
|
+
import { defineLocale, Locale } from '@primereact/core/locale';
|
|
246
|
+
import { tr } from 'primelocale/js/tr.js';
|
|
247
|
+
|
|
248
|
+
defineLocale('tr', tr);
|
|
249
|
+
Locale.use('tr');
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
The package entry points export the messages directly, so no unwrapping is needed there.
|
|
253
|
+
|
|
254
|
+
Regional variants are named with an underscore in the package and a hyphen in the JSON files. Brazilian Portuguese is `primelocale/js/pt_BR.js` when imported and `pt-BR.json` when copied.
|
|
255
|
+
|
|
256
|
+
### Adjusting a Translation
|
|
257
|
+
|
|
258
|
+
A ready-made translation can be tuned without editing the file, by layering overrides on top of it:
|
|
259
|
+
|
|
260
|
+
```js
|
|
261
|
+
defineLocale('tr', tr.tr);
|
|
262
|
+
updateLocale('tr', { clear: 'Sıfırla' });
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
Contributions of new languages and corrections are welcome in the PrimeLocale repository, and benefit every Prime library at once.
|
|
266
|
+
|
|
267
|
+
## Migrating from v10
|
|
268
|
+
|
|
269
|
+
The locale helpers previously imported from `primereact/api` have been replaced.
|
|
270
|
+
|
|
271
|
+
| v10 | v11 |
|
|
272
|
+
| -------------------------------------- | ---------------------------------------------------------------- |
|
|
273
|
+
| `locale('de')` | `Locale.use('de')`, or the `locale` prop on `PrimeReactProvider` |
|
|
274
|
+
| `addLocale('es', {...})` | `defineLocale('es', {...})` |
|
|
275
|
+
| `updateLocaleOption(key, value, lang)` | `updateLocale(lang, { key: value })` |
|
|
276
|
+
| `updateLocaleOptions({...}, lang)` | `updateLocale(lang, {...})` |
|
|
277
|
+
| `localeOption(key)` | `$t(key)`, or `t(key)` from `useLocale()` |
|
|
278
|
+
| `localeOptions(lang)` | `$l().get(lang)`, or `messages` from `useLocale()` |
|
|
279
|
+
|
|
280
|
+
One behavioural difference is worth noting: locale state is now shared with other Prime libraries in the same application rather than being private to PrimeReact. Setting a language from PrimeReact also affects PrimeVue or PrimeNG components running alongside it.
|