@enonic/ui 1.2.1 → 1.3.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/README.md CHANGED
@@ -160,6 +160,41 @@ Optional peers, needed per feature:
160
160
  | `react-virtuoso` | `VirtualizedTreeList` and other virtualized lists |
161
161
  | `tw-animate-css` | Component animations on the `preset.css` path |
162
162
 
163
+ ## Localization
164
+
165
+ Every label a component renders on its own — the close button of a `Dialog`, the placeholder of a
166
+ `SearchField`, the month navigation of a `DatePicker` — is English by default and keyed
167
+ `enonic.ui.<component>.<name>`. To render them in the application's words, hand `I18nProvider` one
168
+ function at the root. It receives the key and the library's English for it, and answers the
169
+ application's text or the English it was given:
170
+
171
+ ```tsx
172
+ import { I18nProvider, type Translate } from '@enonic/ui';
173
+
174
+ const translate: Translate = (key, { defaultValue, values = [] }) => {
175
+ const phrase = phrases[key];
176
+ if (phrase === undefined) return defaultValue;
177
+
178
+ return phrase.replace(/\{(\d+)\}/g, (placeholder, i) => String(values[Number(i)] ?? placeholder));
179
+ };
180
+
181
+ <I18nProvider translate={translate}>
182
+ <App />
183
+ </I18nProvider>;
184
+ ```
185
+
186
+ A phrase the application supplies still carries the library's `{0}` placeholders, so fill them with
187
+ `values` — returning `phrases[key]` unformatted renders `Go to step {0}` verbatim. On a miss, return
188
+ `defaultValue`: never an empty string and never the key, or the `aria-label`s go blank.
189
+
190
+ `translate`'s identity is the only thing that re-renders the labels. Hoisting it out of the render is
191
+ right for a phrase bundle fixed at load; where the locale switches or the bundle arrives later, put
192
+ it in a `useMemo` keyed on that instead, or the labels keep their first wording.
193
+
194
+ `uiPhrases` lists every key with its English, for the application to add to its own phrase files
195
+ and check its bundle against. Texts the application supplies through props — a dialog title, an
196
+ item name — never pass through the provider.
197
+
163
198
  ## What's exported
164
199
 
165
200
  Beyond the components, the package entry re-exports the hooks that drive them (roving tabindex, type-ahead, floating position, scroll lock, and more), the bundled icon set, context providers, shared types, and the `cn` class-merge helper.