@pie-qti/i18n 1.0.0-beta.1 → 1.0.0-beta.2
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 +104 -55
- package/dist/core/I18n.d.ts +43 -13
- package/dist/core/I18n.js +135 -112
- package/dist/core/I18n.js.map +1 -1
- package/dist/core/I18nProvider.d.ts +9 -1
- package/dist/core/I18nProvider.js.map +1 -1
- package/dist/core/accessible-text.d.ts +36 -0
- package/dist/core/accessible-text.js +54 -0
- package/dist/core/accessible-text.js.map +1 -0
- package/dist/core/direction.d.ts +39 -0
- package/dist/core/direction.js +96 -0
- package/dist/core/direction.js.map +1 -0
- package/dist/core/language-tags.d.ts +47 -0
- package/dist/core/language-tags.js +91 -0
- package/dist/core/language-tags.js.map +1 -0
- package/dist/core/negotiation.d.ts +41 -0
- package/dist/core/negotiation.js +71 -0
- package/dist/core/negotiation.js.map +1 -0
- package/dist/core/numbering.d.ts +31 -0
- package/dist/core/numbering.js +88 -0
- package/dist/core/numbering.js.map +1 -0
- package/dist/core/pseudo.d.ts +33 -0
- package/dist/core/pseudo.js +44 -0
- package/dist/core/pseudo.js.map +1 -0
- package/dist/core/types.d.ts +13 -3
- package/dist/core/types.js.map +1 -1
- package/dist/index.d.ts +12 -2
- package/dist/index.js +13 -2
- package/dist/index.js.map +1 -1
- package/dist/locales/{ar-SA.d.ts → ar.d.ts} +65 -3
- package/dist/locales/{ar-SA.js → ar.js} +66 -4
- package/dist/locales/ar.js.map +1 -0
- package/dist/locales/{en-US.d.ts → en.d.ts} +61 -3
- package/dist/locales/{en-US.js → en.js} +62 -4
- package/dist/locales/en.js.map +1 -0
- package/dist/locales/{es-ES.d.ts → es.d.ts} +62 -3
- package/dist/locales/{es-ES.js → es.js} +63 -4
- package/dist/locales/es.js.map +1 -0
- package/dist/locales/{fr-FR.d.ts → fr.d.ts} +62 -3
- package/dist/locales/{fr-FR.js → fr.js} +63 -4
- package/dist/locales/fr.js.map +1 -0
- package/dist/locales/{nl-NL.d.ts → nl.d.ts} +61 -3
- package/dist/locales/{nl-NL.js → nl.js} +62 -4
- package/dist/locales/nl.js.map +1 -0
- package/dist/locales/{ro-RO.d.ts → ro.d.ts} +62 -3
- package/dist/locales/{ro-RO.js → ro.js} +63 -4
- package/dist/locales/ro.js.map +1 -0
- package/dist/locales/{th-TH.d.ts → th.d.ts} +61 -3
- package/dist/locales/{th-TH.js → th.js} +62 -4
- package/dist/locales/th.js.map +1 -0
- package/dist/locales/{zh-CN.d.ts → zh-Hans.d.ts} +61 -3
- package/dist/locales/{zh-CN.js → zh-Hans.js} +62 -4
- package/dist/locales/zh-Hans.js.map +1 -0
- package/dist/providers/SvelteI18nProvider.d.ts +31 -12
- package/dist/providers/SvelteI18nProvider.js +33 -38
- package/dist/providers/SvelteI18nProvider.js.map +1 -1
- package/package.json +4 -2
- package/dist/locales/ar-SA.js.map +0 -1
- package/dist/locales/en-US.js.map +0 -1
- package/dist/locales/es-ES.js.map +0 -1
- package/dist/locales/fr-FR.js.map +0 -1
- package/dist/locales/nl-NL.js.map +0 -1
- package/dist/locales/ro-RO.js.map +0 -1
- package/dist/locales/th-TH.js.map +0 -1
- package/dist/locales/zh-CN.js.map +0 -1
package/README.md
CHANGED
|
@@ -5,7 +5,8 @@ Lightweight, type-safe internationalization (i18n) package for the PIE-QTI playe
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
7
|
- ✅ **Type-Safe Translations** - TypeScript autocomplete for all message keys
|
|
8
|
-
- ✅ **Locale
|
|
8
|
+
- ✅ **Host-Owned Locale** - The host passes the locale in; nothing is read from storage. Persisting to localStorage and reloading on change are opt-in (`SvelteI18nProvider`)
|
|
9
|
+
- ✅ **Locale Negotiation** - Any BCP 47 or POSIX tag resolves by language: `es-MX` and `es_ES` reach Spanish, `ar-EG` reaches Arabic
|
|
9
10
|
- ✅ **Framework Agnostic** - Core provider works with any JavaScript framework
|
|
10
11
|
- ✅ **Svelte Integration** - Simple `$derived` pattern for Svelte 5 components
|
|
11
12
|
- ✅ **Web Component Compatible** - Works within Shadow DOM boundaries
|
|
@@ -13,7 +14,7 @@ Lightweight, type-safe internationalization (i18n) package for the PIE-QTI playe
|
|
|
13
14
|
- ✅ **On-Demand Loading** - Load additional locales asynchronously
|
|
14
15
|
- ✅ **Built-in Formatting** - Number and date formatting via `Intl` APIs
|
|
15
16
|
- ✅ **Custom Translations** - Override or extend framework translations
|
|
16
|
-
- ✅ **8 Languages Supported** - en
|
|
17
|
+
- ✅ **8 Languages Supported** - en (source locale), ar, es, fr, nl, ro, th, zh-Hans
|
|
17
18
|
|
|
18
19
|
## Installation
|
|
19
20
|
|
|
@@ -31,9 +32,9 @@ import { DefaultI18nProvider } from '@pie-qti/i18n';
|
|
|
31
32
|
// Create provider with default locale
|
|
32
33
|
const i18n = new DefaultI18nProvider('en-US');
|
|
33
34
|
|
|
34
|
-
// Optional: Load additional locales
|
|
35
|
-
await i18n.loadLocale('es-
|
|
36
|
-
await i18n.loadLocale('fr
|
|
35
|
+
// Optional: Load additional locales (any tag for the language works)
|
|
36
|
+
await i18n.loadLocale('es-MX');
|
|
37
|
+
await i18n.loadLocale('fr');
|
|
37
38
|
```
|
|
38
39
|
|
|
39
40
|
### 2. Use in Components (Svelte 5)
|
|
@@ -48,7 +49,7 @@ interface Props {
|
|
|
48
49
|
|
|
49
50
|
let { i18n }: Props = $props();
|
|
50
51
|
|
|
51
|
-
//
|
|
52
|
+
// Recomputed whenever the i18n prop changes
|
|
52
53
|
const translations = $derived({
|
|
53
54
|
submit: i18n?.t('common.submit') ?? 'Submit',
|
|
54
55
|
cancel: i18n?.t('common.cancel') ?? 'Cancel'
|
|
@@ -59,7 +60,7 @@ const translations = $derived({
|
|
|
59
60
|
<button>{translations.cancel}</button>
|
|
60
61
|
```
|
|
61
62
|
|
|
62
|
-
**Note:**
|
|
63
|
+
**Note:** `$derived` recomputes when the `i18n` prop changes. A `setLocale()` on the same provider object is not tracked; the host re-renders, passes a new provider or `withLocale()` view, or opts into reload (below).
|
|
63
64
|
|
|
64
65
|
### 3. Switch Locales
|
|
65
66
|
|
|
@@ -73,18 +74,27 @@ The two providers behave differently:
|
|
|
73
74
|
i18n.setLocale('es-ES');
|
|
74
75
|
```
|
|
75
76
|
|
|
76
|
-
- `SvelteI18nProvider.setLocale()`
|
|
77
|
-
|
|
77
|
+
- `SvelteI18nProvider.setLocale()` loads the catalog the locale negotiates to, then switches. It
|
|
78
|
+
persists and reloads only when the host opts in:
|
|
78
79
|
|
|
79
80
|
```typescript
|
|
80
|
-
|
|
81
|
-
|
|
81
|
+
import { createDefaultSvelteI18nProvider, DEFAULT_LOCALE_STORAGE_KEY } from '@pie-qti/i18n';
|
|
82
|
+
|
|
83
|
+
// The host reads its own stored value; the provider never reads storage
|
|
84
|
+
const saved = localStorage.getItem(DEFAULT_LOCALE_STORAGE_KEY) ?? 'en-US';
|
|
85
|
+
const i18n = await createDefaultSvelteI18nProvider(saved, undefined, {
|
|
86
|
+
persistLocale: true, // write the locale to localStorage on setLocale()
|
|
87
|
+
reloadOnLocaleChange: true, // reload the page after setLocale()
|
|
88
|
+
// storageKey: 'my-app-locale', // defaults to 'pie-qti-locale'
|
|
89
|
+
});
|
|
82
90
|
|
|
83
|
-
|
|
91
|
+
await i18n.setLocale('es-ES'); // persists, then reloads
|
|
84
92
|
```
|
|
85
93
|
|
|
86
|
-
To render a specific player or element in another locale without
|
|
87
|
-
`withLocale(locale)` view (
|
|
94
|
+
To render a specific player or element in another locale without touching the shared provider, use
|
|
95
|
+
the `withLocale(locale)` view instead of `setLocale()`. When that locale's catalog is not loaded yet,
|
|
96
|
+
the view renders English and carries a `ready` promise that settles once the catalog arrives; the
|
|
97
|
+
player custom elements re-render on it.
|
|
88
98
|
|
|
89
99
|
## API Reference
|
|
90
100
|
|
|
@@ -103,6 +113,7 @@ interface I18nProvider {
|
|
|
103
113
|
plural?(key: string, options: { count: number; [key: string]: any }): string;
|
|
104
114
|
getDirection?(): 'ltr' | 'rtl';
|
|
105
115
|
withLocale?(locale: string): I18nProvider;
|
|
116
|
+
readonly ready?: Promise<void>; // set on a withLocale() view still loading its catalog
|
|
106
117
|
formatNumber?(value: number, options?: Intl.NumberFormatOptions): string;
|
|
107
118
|
formatDate?(date: Date, options?: Intl.DateTimeFormatOptions): string;
|
|
108
119
|
}
|
|
@@ -126,11 +137,11 @@ const customMessages = {
|
|
|
126
137
|
};
|
|
127
138
|
const i18n = new DefaultI18nProvider('en-US', customMessages);
|
|
128
139
|
|
|
129
|
-
// Load additional locales
|
|
140
|
+
// Load additional locales; resolves without loading for a language with no catalog
|
|
130
141
|
await i18n.loadLocale('es-ES');
|
|
131
142
|
|
|
132
|
-
// Switch locales
|
|
133
|
-
|
|
143
|
+
// Switch locales (synchronous)
|
|
144
|
+
i18n.setLocale('es-ES');
|
|
134
145
|
|
|
135
146
|
// Translate
|
|
136
147
|
const text = i18n.t('common.submit');
|
|
@@ -144,34 +155,59 @@ Svelte-specific wrapper that extends `I18nProvider`:
|
|
|
144
155
|
```typescript
|
|
145
156
|
import { createDefaultSvelteI18nProvider } from '@pie-qti/i18n';
|
|
146
157
|
|
|
147
|
-
// Create Svelte provider
|
|
158
|
+
// Create Svelte provider; the locale's catalog is loaded before it resolves
|
|
148
159
|
const i18n = await createDefaultSvelteI18nProvider('en-US');
|
|
149
160
|
|
|
150
161
|
// Same API as I18nProvider
|
|
151
162
|
const text = i18n.t('common.submit');
|
|
152
|
-
await i18n.setLocale('es-ES'); //
|
|
163
|
+
await i18n.setLocale('es-ES'); // Loads and switches; no persistence or reload unless opted in
|
|
153
164
|
```
|
|
154
165
|
|
|
155
166
|
### Supported Locales
|
|
156
167
|
|
|
157
|
-
The framework currently includes 8
|
|
158
|
-
|
|
159
|
-
|
|
|
160
|
-
|
|
161
|
-
| `en
|
|
162
|
-
| `ar
|
|
163
|
-
| `es
|
|
164
|
-
| `fr
|
|
165
|
-
| `nl
|
|
166
|
-
| `ro
|
|
167
|
-
| `th
|
|
168
|
-
| `zh-
|
|
169
|
-
|
|
170
|
-
|
|
168
|
+
The framework currently includes 8 catalogs, keyed by language (and by script for Chinese):
|
|
169
|
+
|
|
170
|
+
| Catalog | Language | Served tags (examples) | Status |
|
|
171
|
+
|-----------|-----------------------|--------------------------------------|-----------------------------|
|
|
172
|
+
| `en` | English | `en`, `en-US`, `en-GB` | ✅ Complete (source locale) |
|
|
173
|
+
| `ar` | Arabic | `ar`, `ar-SA`, `ar-EG`, `ar_SA` | ✅ Complete |
|
|
174
|
+
| `es` | Spanish | `es`, `es-ES`, `es-MX`, `es-419` | ✅ Complete |
|
|
175
|
+
| `fr` | French | `fr`, `fr-FR`, `fr-CA` | ✅ Complete |
|
|
176
|
+
| `nl` | Dutch | `nl`, `nl-NL`, `nl-BE` | ✅ Complete |
|
|
177
|
+
| `ro` | Romanian | `ro`, `ro-RO` | ✅ Complete |
|
|
178
|
+
| `th` | Thai | `th`, `th-TH` | ✅ Complete |
|
|
179
|
+
| `zh-Hans` | Chinese (Simplified) | `zh`, `zh-CN`, `zh-SG`, `zh-Hans` | ✅ Complete |
|
|
180
|
+
|
|
181
|
+
A tag walks its RFC 4647 lookup sequence (`es-MX`, then `es`), with the language's CLDR likely script
|
|
182
|
+
inserted before the bare language, so `zh-CN` reaches `zh-Hans`. A catalog reached by widening must
|
|
183
|
+
share the request's script: `zh-TW`, `zh-HK` and `zh-Hant` do not get the Simplified catalog and
|
|
184
|
+
render in English. Any key missing from the resolved catalog falls back to English.
|
|
185
|
+
`resolveFrameworkLocale(tag)` answers which catalog serves a tag. The old region-qualified codes
|
|
186
|
+
(`es-ES`, `zh-CN`, …) remain valid `FrameworkLocaleCode` inputs.
|
|
187
|
+
|
|
188
|
+
"Complete" means every English key exists in the locale (verified by the translation coverage
|
|
171
189
|
checker below). Values that still match English are reported by the checker as *untranslated*
|
|
172
190
|
rather than missing.
|
|
173
191
|
|
|
174
|
-
|
|
192
|
+
### Numbers and dates
|
|
193
|
+
|
|
194
|
+
`formatNumber` and `formatDate` do not take the runtime's defaults, which follow whatever CLDR its
|
|
195
|
+
ICU ships. Digits come from a table pinned to CLDR 48 and the calendar is Gregorian:
|
|
196
|
+
|
|
197
|
+
```typescript
|
|
198
|
+
new DefaultI18nProvider('ar').formatNumber(1234.5); // '1,234.5'
|
|
199
|
+
new DefaultI18nProvider('ar-EG').formatNumber(1234.5); // '١٬٢٣٤٫٥'
|
|
200
|
+
new DefaultI18nProvider('ar-MA').formatNumber(1234.5); // '1.234,5'
|
|
201
|
+
new DefaultI18nProvider('fa-IR').formatDate(date); // Gregorian year, Extended Arabic-Indic digits
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
A host that wants other digits or a local calendar sets them on the locale: `ar-SA-u-nu-latn`,
|
|
205
|
+
`fa-IR-u-ca-persian`. Catalog lookup and the `lang` attribute ignore the extension. Numbers passed
|
|
206
|
+
to `t()` and `plural()` are formatted the same way, so `{count}` follows the locale.
|
|
207
|
+
`formattingLocale(tag)` and `numberingSystemFor(tag)` expose the resolution.
|
|
208
|
+
|
|
209
|
+
**Note:** Additional locales can be provided via custom translations (see below). Custom messages are
|
|
210
|
+
matched along the same lookup sequence, so a `de` entry serves `de-AT`.
|
|
175
211
|
|
|
176
212
|
## Message Keys
|
|
177
213
|
|
|
@@ -196,7 +232,7 @@ $t('invalid.key')
|
|
|
196
232
|
- `assessment.*` - Assessment player UI
|
|
197
233
|
- `accessibility.*` - ARIA labels and screen reader text
|
|
198
234
|
|
|
199
|
-
See [en
|
|
235
|
+
See [en.ts](./src/locales/en.ts) for the complete list of available keys.
|
|
200
236
|
|
|
201
237
|
## Custom Translations
|
|
202
238
|
|
|
@@ -294,14 +330,14 @@ let { i18n = $bindable() }: Props = $props();
|
|
|
294
330
|
const t = $derived((key: string, fallback: string) => i18n?.t(key) ?? fallback);
|
|
295
331
|
```
|
|
296
332
|
|
|
297
|
-
**Note:** No manual subscriptions
|
|
333
|
+
**Note:** No manual subscriptions needed. Components re-render when the host passes a new provider or view; hosts that change the locale in place opt into reload.
|
|
298
334
|
|
|
299
335
|
## Bundle Size
|
|
300
336
|
|
|
301
337
|
| Component | Size (gzipped) |
|
|
302
338
|
|-------------------------------|----------------|
|
|
303
339
|
| Core i18n logic | ~2 KB |
|
|
304
|
-
| English
|
|
340
|
+
| English catalog (en) | ~8 KB |
|
|
305
341
|
| Additional locale (on-demand) | ~8 KB each |
|
|
306
342
|
| **Total (default)** | **~10 KB** |
|
|
307
343
|
|
|
@@ -312,7 +348,7 @@ For detailed guides, see the `docs/` directory:
|
|
|
312
348
|
- **[Integration Guide](./docs/INTEGRATION_GUIDE.md)** - Complete guide for adding i18n to components
|
|
313
349
|
- **[Component UI Patterns](./docs/INTEGRATION_GUIDE.md#component-ui-patterns)** - Svelte 5 + DaisyUI patterns and examples
|
|
314
350
|
- **[Advanced Features](./docs/ADVANCED_FEATURES.md)** - Pluralization, number & date formatting
|
|
315
|
-
- **[Message Keys](./src/locales/en
|
|
351
|
+
- **[Message Keys](./src/locales/en.ts)** - All available translation keys and namespaces
|
|
316
352
|
|
|
317
353
|
## Development
|
|
318
354
|
|
|
@@ -321,7 +357,7 @@ For detailed guides, see the `docs/` directory:
|
|
|
321
357
|
1. Add to English locale:
|
|
322
358
|
|
|
323
359
|
```typescript
|
|
324
|
-
// src/locales/en
|
|
360
|
+
// src/locales/en.ts
|
|
325
361
|
export default {
|
|
326
362
|
myFeature: {
|
|
327
363
|
newMessage: 'My new message',
|
|
@@ -348,12 +384,12 @@ The package includes an automated translation coverage checker that ensures all
|
|
|
348
384
|
bun run check-translations
|
|
349
385
|
|
|
350
386
|
# Check specific locale
|
|
351
|
-
bun run scripts/check-translations.ts --locale
|
|
387
|
+
bun run scripts/check-translations.ts --locale=nl
|
|
352
388
|
```
|
|
353
389
|
|
|
354
390
|
The checker:
|
|
355
391
|
|
|
356
|
-
- ✅ **Automatically discovers** all keys from en
|
|
392
|
+
- ✅ **Automatically discovers** all keys from en (no hardcoded lists)
|
|
357
393
|
- ✅ **Automatically detects** all locale files (no configuration)
|
|
358
394
|
- ✅ **Reports missing keys** in other locales
|
|
359
395
|
- ✅ **Detects obsolete keys** that should be removed
|
|
@@ -370,33 +406,46 @@ and options.
|
|
|
370
406
|
|
|
371
407
|
### Hardcoded String Scanner
|
|
372
408
|
|
|
373
|
-
The
|
|
409
|
+
The scanner parses Svelte templates with the Svelte compiler and reports learner-visible strings that bypass the catalog: template text, and `aria-label`, `title` and `placeholder` values on DOM elements that are static text or a template literal. Script literals, component props and test directories are out of scope. A finding names the catalog key when en already carries the string.
|
|
374
410
|
|
|
375
411
|
```bash
|
|
376
|
-
# Scan
|
|
412
|
+
# Scan every package (advisory)
|
|
377
413
|
bun run scan-hardcoded
|
|
378
414
|
|
|
415
|
+
# Exit 1 on any finding; `bun run check:i18n` and CI run it this way
|
|
416
|
+
bun run scan-hardcoded --strict
|
|
417
|
+
|
|
379
418
|
# Scan a custom path
|
|
380
419
|
bun run scan-hardcoded --path=../custom-components
|
|
381
420
|
```
|
|
382
421
|
|
|
383
|
-
|
|
422
|
+
Put `<!-- i18n-ignore -->` before an element or text node that no learner reads in their own language, such as a code sample; the node and its subtree are skipped.
|
|
384
423
|
|
|
385
|
-
|
|
386
|
-
-
|
|
387
|
-
-
|
|
388
|
-
|
|
424
|
+
```text
|
|
425
|
+
packages/default-components: 1 finding(s)
|
|
426
|
+
packages/default-components/src/plugins/order/OrderInteraction.svelte:212 text"Order confirmed" → t('interactions.order.orderConfirmed')
|
|
427
|
+
```
|
|
389
428
|
|
|
390
|
-
|
|
429
|
+
### Physical-Direction CSS Check
|
|
391
430
|
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
431
|
+
`bun run check-physical-css` reports CSS that does not mirror under `dir="rtl"`: `margin-left`, `right: 0`, `text-align: left`, `border-top-left-radius`, and Tailwind utilities such as `ml-2`, `right-2` and `text-left` in `class` attributes. Use the logical form (`margin-inline-start`, `inset-inline-end`, `text-align: start`, `ms-2`, `end-2`, `text-start`). It runs last in `check:i18n` and exits 1 on any finding.
|
|
432
|
+
|
|
433
|
+
Graphic and spatial interactions are exempt, since their coordinates are image space. For CSS physical by definition, such as the QTI `qti-align-left` class, put an `i18n-physical` comment on the line or the line above.
|
|
434
|
+
|
|
435
|
+
### Pseudo-Locales
|
|
436
|
+
|
|
437
|
+
`pseudoLocalize(en)` turns a catalog into accented, lengthened, bracketed English with the same keys and placeholders. Register it under `PSEUDO_LOCALE_RTL` (`ar-XB`) to see untranslated strings, truncation and clipping in a right-to-left layout, or under `PSEUDO_LOCALE_EXPANDED` (`en-XA`) for expansion alone:
|
|
438
|
+
|
|
439
|
+
```typescript
|
|
440
|
+
import { DefaultI18nProvider, PSEUDO_LOCALE_RTL, pseudoLocalize } from '@pie-qti/i18n';
|
|
441
|
+
import en from '@pie-qti/i18n/locales/en.js';
|
|
442
|
+
|
|
443
|
+
const i18n = new DefaultI18nProvider(PSEUDO_LOCALE_RTL, { [PSEUDO_LOCALE_RTL]: pseudoLocalize(en) });
|
|
444
|
+
await i18n.loadLocale(PSEUDO_LOCALE_RTL);
|
|
445
|
+
i18n.t('common.close'); // '[Çĺóšé ~~]'
|
|
397
446
|
```
|
|
398
447
|
|
|
399
|
-
|
|
448
|
+
The demo's web-component routes accept `?locale=ar-XB`, which `apps/demo/tests/playwright/rtl-matrix.pw.ts` runs alongside `ar`.
|
|
400
449
|
|
|
401
450
|
### Running Tests
|
|
402
451
|
|
package/dist/core/I18n.d.ts
CHANGED
|
@@ -6,21 +6,43 @@
|
|
|
6
6
|
* interpolation and pluralization support.
|
|
7
7
|
*/
|
|
8
8
|
import type { I18nProvider, InterpolationValues, PluralOptions } from './I18nProvider.js';
|
|
9
|
-
import type { LocaleCode,
|
|
9
|
+
import type { LocaleCode, MessageKey, LocaleMessages } from './types.js';
|
|
10
10
|
export declare class DefaultI18nProvider implements I18nProvider {
|
|
11
11
|
/** Shared across instances: constructing Intl.PluralRules is not cheap */
|
|
12
12
|
private static pluralRules;
|
|
13
13
|
private currentLocale;
|
|
14
14
|
private messages;
|
|
15
15
|
private customMessages;
|
|
16
|
-
private fallbackLocale;
|
|
17
16
|
private loadedLocales;
|
|
17
|
+
private pendingLoads;
|
|
18
|
+
/**
|
|
19
|
+
* Settles once the catalog for this provider's locale is loaded. Set only on
|
|
20
|
+
* a `withLocale()` view created before that catalog was resident; the view
|
|
21
|
+
* starts the load itself. Never rejects: a failed load is logged and the view
|
|
22
|
+
* keeps rendering through the English fallback.
|
|
23
|
+
*/
|
|
24
|
+
ready?: Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* @param locale - Active locale; any BCP 47 or POSIX tag. The host owns this
|
|
27
|
+
* value: the provider reads no storage and persists nothing.
|
|
28
|
+
* @param customMessages - Host translations by locale tag, consulted before
|
|
29
|
+
* the framework catalogs.
|
|
30
|
+
*/
|
|
18
31
|
constructor(locale?: LocaleCode, customMessages?: Record<string, LocaleMessages>);
|
|
19
32
|
/**
|
|
20
|
-
* Load
|
|
21
|
-
*
|
|
33
|
+
* Load the framework catalog that serves a locale tag.
|
|
34
|
+
*
|
|
35
|
+
* The tag is negotiated (`es-MX` loads `es`, `zh-CN` loads `zh-Hans`). A tag
|
|
36
|
+
* no framework catalog serves resolves without loading anything: it renders
|
|
37
|
+
* from custom messages and the English fallback. Rejects only when a catalog
|
|
38
|
+
* module fails to load.
|
|
39
|
+
*/
|
|
40
|
+
loadLocale(locale: LocaleCode): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Whether every catalog a locale needs is resident: true for a tag no
|
|
43
|
+
* framework catalog serves, since there is nothing to load.
|
|
22
44
|
*/
|
|
23
|
-
|
|
45
|
+
isLocaleLoaded(locale: LocaleCode): boolean;
|
|
24
46
|
/**
|
|
25
47
|
* Set current locale
|
|
26
48
|
* For framework locales, loadLocale() should be called first.
|
|
@@ -71,21 +93,33 @@ export declare class DefaultI18nProvider implements I18nProvider {
|
|
|
71
93
|
* catalog is parsed twice. This is what lets two players on one page render
|
|
72
94
|
* different locales from one provider: the locale is per-view, the catalogs
|
|
73
95
|
* are per-provider.
|
|
96
|
+
*
|
|
97
|
+
* When the catalog its locale negotiates to is not resident, the view loads
|
|
98
|
+
* it and `ready` settles when that load does; until then it renders through
|
|
99
|
+
* the English fallback. A view over a resident catalog has no `ready`.
|
|
74
100
|
*/
|
|
75
|
-
withLocale(locale: LocaleCode):
|
|
101
|
+
withLocale(locale: LocaleCode): DefaultI18nProvider;
|
|
76
102
|
/**
|
|
77
|
-
* Format number
|
|
103
|
+
* Format a number in the locale's pinned numbering system
|
|
104
|
+
*
|
|
105
|
+
* @example formatNumber(1234.5) => "1,234.5" for ar, "١٬٢٣٤٫٥" for ar-EG
|
|
78
106
|
*/
|
|
79
107
|
formatNumber(value: number, options?: Intl.NumberFormatOptions): string;
|
|
80
108
|
/**
|
|
81
|
-
* Format date
|
|
109
|
+
* Format a date in the locale's pinned numbering system, on the Gregorian
|
|
110
|
+
* calendar unless the locale carries `-u-ca-`
|
|
82
111
|
*/
|
|
83
112
|
formatDate(date: Date, options?: Intl.DateTimeFormatOptions): string;
|
|
84
113
|
/**
|
|
85
114
|
* Get nested message by dot notation key
|
|
86
|
-
*
|
|
115
|
+
*
|
|
116
|
+
* Walks the active locale's negotiation sequence, then the English fallback's.
|
|
117
|
+
* At each step custom messages come before the framework catalog, so for
|
|
118
|
+
* `es-MX` the order is custom `es-MX`, custom `es`, framework `es`, custom
|
|
119
|
+
* `en-US`, custom `en`, framework `en`.
|
|
87
120
|
*/
|
|
88
121
|
private getMessage;
|
|
122
|
+
private lookupMessage;
|
|
89
123
|
/**
|
|
90
124
|
* Get message from a specific messages object
|
|
91
125
|
*
|
|
@@ -95,10 +129,6 @@ export declare class DefaultI18nProvider implements I18nProvider {
|
|
|
95
129
|
* expects a string, and interpolating one throws.
|
|
96
130
|
*/
|
|
97
131
|
private getMessageFromObject;
|
|
98
|
-
/**
|
|
99
|
-
* Get message from fallback locale (framework defaults)
|
|
100
|
-
*/
|
|
101
|
-
private getMessageFromFallback;
|
|
102
132
|
/**
|
|
103
133
|
* Add or update custom messages for a locale
|
|
104
134
|
* This allows clients to provide their own translations or override defaults
|