@pie-qti/i18n 0.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/.turbo/turbo-build.log +264 -0
- package/README.md +402 -0
- package/dist/core/I18n.d.ts +83 -0
- package/dist/core/I18n.d.ts.map +1 -0
- package/dist/core/I18n.js +203 -0
- package/dist/core/I18n.js.map +1 -0
- package/dist/core/I18nProvider.d.ts +64 -0
- package/dist/core/I18nProvider.d.ts.map +1 -0
- package/dist/core/I18nProvider.js +9 -0
- package/dist/core/I18nProvider.js.map +1 -0
- package/dist/core/types.d.ts +20 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +5 -0
- package/dist/core/types.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/locales/ar-SA.d.ts +763 -0
- package/dist/locales/ar-SA.d.ts.map +1 -0
- package/dist/locales/ar-SA.js +796 -0
- package/dist/locales/ar-SA.js.map +1 -0
- package/dist/locales/en-US.d.ts +731 -0
- package/dist/locales/en-US.d.ts.map +1 -0
- package/dist/locales/en-US.js +764 -0
- package/dist/locales/en-US.js.map +1 -0
- package/dist/locales/es-ES.d.ts +734 -0
- package/dist/locales/es-ES.d.ts.map +1 -0
- package/dist/locales/es-ES.js +758 -0
- package/dist/locales/es-ES.js.map +1 -0
- package/dist/locales/fr-FR.d.ts +733 -0
- package/dist/locales/fr-FR.d.ts.map +1 -0
- package/dist/locales/fr-FR.js +757 -0
- package/dist/locales/fr-FR.js.map +1 -0
- package/dist/locales/nl-NL.d.ts +733 -0
- package/dist/locales/nl-NL.d.ts.map +1 -0
- package/dist/locales/nl-NL.js +757 -0
- package/dist/locales/nl-NL.js.map +1 -0
- package/dist/locales/ro-RO.d.ts +733 -0
- package/dist/locales/ro-RO.d.ts.map +1 -0
- package/dist/locales/ro-RO.js +757 -0
- package/dist/locales/ro-RO.js.map +1 -0
- package/dist/locales/th-TH.d.ts +733 -0
- package/dist/locales/th-TH.d.ts.map +1 -0
- package/dist/locales/th-TH.js +758 -0
- package/dist/locales/th-TH.js.map +1 -0
- package/dist/locales/zh-CN.d.ts +731 -0
- package/dist/locales/zh-CN.d.ts.map +1 -0
- package/dist/locales/zh-CN.js +764 -0
- package/dist/locales/zh-CN.js.map +1 -0
- package/dist/providers/SvelteI18nProvider.d.ts +40 -0
- package/dist/providers/SvelteI18nProvider.d.ts.map +1 -0
- package/dist/providers/SvelteI18nProvider.js +133 -0
- package/dist/providers/SvelteI18nProvider.js.map +1 -0
- package/docs/ADVANCED_FEATURES.md +556 -0
- package/docs/COMPONENT_PATTERNS.md +896 -0
- package/docs/INTEGRATION_GUIDE.md +695 -0
- package/package.json +42 -0
- package/scripts/check-translations.ts +289 -0
- package/scripts/scan-hardcoded-strings.ts +266 -0
- package/src/components/LocaleSwitcher.svelte +145 -0
- package/src/components/README.md +224 -0
- package/src/core/I18n.ts +235 -0
- package/src/core/I18nProvider.ts +68 -0
- package/src/core/types.ts +45 -0
- package/src/index.ts +22 -0
- package/src/locales/ar-SA.ts +860 -0
- package/src/locales/en-US.ts +828 -0
- package/src/locales/es-ES.ts +816 -0
- package/src/locales/fr-FR.ts +815 -0
- package/src/locales/nl-NL.ts +815 -0
- package/src/locales/ro-RO.ts +815 -0
- package/src/locales/th-TH.ts +816 -0
- package/src/locales/zh-CN.ts +828 -0
- package/src/providers/SvelteI18nProvider.ts +170 -0
- package/tests/i18n.test.ts +146 -0
- package/tsconfig.json +10 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
LocaleSwitcher Example Component
|
|
3
|
+
|
|
4
|
+
This is a reference implementation showing how to create a locale switcher
|
|
5
|
+
in your application. You can copy and adapt this for your needs.
|
|
6
|
+
|
|
7
|
+
Usage:
|
|
8
|
+
|
|
9
|
+
<script>
|
|
10
|
+
import LocaleSwitcher from '@pie-qti/i18n/components/LocaleSwitcher.svelte';
|
|
11
|
+
import { createI18n } from '@pie-qti/i18n';
|
|
12
|
+
|
|
13
|
+
let locale = $state('en-US');
|
|
14
|
+
const i18n = $derived(createI18n(locale));
|
|
15
|
+
|
|
16
|
+
function handleLocaleChange(newLocale) {
|
|
17
|
+
locale = newLocale;
|
|
18
|
+
}
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<LocaleSwitcher currentLocale={locale} onChange={handleLocaleChange} {i18n} />
|
|
22
|
+
-->
|
|
23
|
+
|
|
24
|
+
<script lang="ts">
|
|
25
|
+
import type { I18nProvider } from '../core/types';
|
|
26
|
+
|
|
27
|
+
interface Props {
|
|
28
|
+
/** Currently selected locale code */
|
|
29
|
+
currentLocale: string;
|
|
30
|
+
/** Callback when locale changes */
|
|
31
|
+
onChange: (locale: string) => void;
|
|
32
|
+
/** I18n provider for translating the component UI (optional) */
|
|
33
|
+
i18n?: I18nProvider;
|
|
34
|
+
/** Supported locales (defaults to all available locales) */
|
|
35
|
+
availableLocales?: Array<{ code: string; label: string }>;
|
|
36
|
+
/** Label text (can be overridden) */
|
|
37
|
+
label?: string;
|
|
38
|
+
/** Compact mode - no label, just dropdown */
|
|
39
|
+
compact?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let {
|
|
43
|
+
currentLocale,
|
|
44
|
+
onChange,
|
|
45
|
+
i18n,
|
|
46
|
+
availableLocales = DEFAULT_LOCALES,
|
|
47
|
+
label,
|
|
48
|
+
compact = false,
|
|
49
|
+
}: Props = $props();
|
|
50
|
+
|
|
51
|
+
// Default list of supported locales
|
|
52
|
+
// Note: Some locales may not have full translations yet
|
|
53
|
+
const DEFAULT_LOCALES = [
|
|
54
|
+
// Currently supported with translations
|
|
55
|
+
{ code: 'en-US', label: 'English (US)' },
|
|
56
|
+
{ code: 'es-ES', label: 'Español' },
|
|
57
|
+
{ code: 'fr-FR', label: 'Français' },
|
|
58
|
+
{ code: 'nl-NL', label: 'Nederlands' },
|
|
59
|
+
{ code: 'ro-RO', label: 'Română' },
|
|
60
|
+
{ code: 'th-TH', label: 'ไทย' },
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
// Reactive translations
|
|
64
|
+
const translations = $derived({
|
|
65
|
+
selectLanguage: label ?? i18n?.t('i18n.selectLanguage') ?? 'Language',
|
|
66
|
+
ariaLabel: i18n?.t('i18n.selectLanguageAriaLabel') ?? 'Select display language',
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
function handleChange(event: Event) {
|
|
70
|
+
const select = event.target as HTMLSelectElement;
|
|
71
|
+
onChange(select.value);
|
|
72
|
+
}
|
|
73
|
+
</script>
|
|
74
|
+
|
|
75
|
+
<div class="locale-switcher" class:compact>
|
|
76
|
+
{#if !compact}
|
|
77
|
+
<label for="locale-select" class="locale-label">
|
|
78
|
+
{translations.selectLanguage}:
|
|
79
|
+
</label>
|
|
80
|
+
{/if}
|
|
81
|
+
|
|
82
|
+
<select
|
|
83
|
+
id="locale-select"
|
|
84
|
+
class="locale-select"
|
|
85
|
+
value={currentLocale}
|
|
86
|
+
onchange={handleChange}
|
|
87
|
+
aria-label={translations.ariaLabel}
|
|
88
|
+
>
|
|
89
|
+
{#each availableLocales as { code, label }}
|
|
90
|
+
<option value={code}>{label}</option>
|
|
91
|
+
{/each}
|
|
92
|
+
</select>
|
|
93
|
+
</div>
|
|
94
|
+
|
|
95
|
+
<style>
|
|
96
|
+
.locale-switcher {
|
|
97
|
+
display: flex;
|
|
98
|
+
align-items: center;
|
|
99
|
+
gap: 0.5rem;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.locale-switcher.compact {
|
|
103
|
+
gap: 0;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.locale-label {
|
|
107
|
+
font-size: 0.875rem;
|
|
108
|
+
font-weight: 500;
|
|
109
|
+
color: var(--color-base-content, oklch(20% 0 0));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
.locale-select {
|
|
113
|
+
padding: 0.375rem 0.75rem;
|
|
114
|
+
border: 1px solid var(--color-base-300, oklch(90% 0 0));
|
|
115
|
+
border-radius: 0.375rem;
|
|
116
|
+
background: var(--color-base-100, oklch(100% 0 0));
|
|
117
|
+
color: var(--color-base-content, oklch(20% 0 0));
|
|
118
|
+
font-size: 0.875rem;
|
|
119
|
+
cursor: pointer;
|
|
120
|
+
transition: all 0.15s ease;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
.locale-select:hover {
|
|
124
|
+
border-color: var(--color-primary, oklch(45% 0.24 277));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
.locale-select:focus {
|
|
128
|
+
outline: 2px solid var(--color-primary, oklch(45% 0.24 277));
|
|
129
|
+
outline-offset: 2px;
|
|
130
|
+
border-color: var(--color-primary, oklch(45% 0.24 277));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
.locale-select:disabled {
|
|
134
|
+
opacity: 0.5;
|
|
135
|
+
cursor: not-allowed;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/* Support for DaisyUI if available */
|
|
139
|
+
:global(.theme-dark) .locale-select,
|
|
140
|
+
:global([data-theme='dark']) .locale-select {
|
|
141
|
+
background: var(--color-base-200, oklch(20% 0 0));
|
|
142
|
+
border-color: var(--color-base-300, oklch(30% 0 0));
|
|
143
|
+
color: var(--color-base-content, oklch(90% 0 0));
|
|
144
|
+
}
|
|
145
|
+
</style>
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
# Example Components
|
|
2
|
+
|
|
3
|
+
This directory contains example/reference components demonstrating i18n patterns in PIE-QTI.
|
|
4
|
+
|
|
5
|
+
## LocaleSwitcher
|
|
6
|
+
|
|
7
|
+
A reference implementation of a locale switcher component. You can use this directly or copy and customize it for your needs.
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
- Dropdown selector for changing display language
|
|
12
|
+
- Internationalized labels and aria-labels
|
|
13
|
+
- Compact mode (dropdown only, no label)
|
|
14
|
+
- Customizable available locales
|
|
15
|
+
- Keyboard accessible
|
|
16
|
+
- Supports DaisyUI theming
|
|
17
|
+
|
|
18
|
+
### Basic Usage
|
|
19
|
+
|
|
20
|
+
```svelte
|
|
21
|
+
<script>
|
|
22
|
+
import { LocaleSwitcher, createSvelteI18nProvider } from '@pie-qti/i18n';
|
|
23
|
+
|
|
24
|
+
let locale = $state('en-US');
|
|
25
|
+
const i18nProvider = $derived(createSvelteI18nProvider(locale));
|
|
26
|
+
|
|
27
|
+
function handleLocaleChange(newLocale) {
|
|
28
|
+
locale = newLocale;
|
|
29
|
+
}
|
|
30
|
+
</script>
|
|
31
|
+
|
|
32
|
+
<LocaleSwitcher
|
|
33
|
+
currentLocale={locale}
|
|
34
|
+
onChange={handleLocaleChange}
|
|
35
|
+
i18n={i18nProvider}
|
|
36
|
+
/>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Props
|
|
40
|
+
|
|
41
|
+
| Prop | Type | Required | Default | Description |
|
|
42
|
+
|------|------|----------|---------|-------------|
|
|
43
|
+
| `currentLocale` | `string` | Yes | - | Currently selected locale code (e.g., 'en-US') |
|
|
44
|
+
| `onChange` | `(locale: string) => void` | Yes | - | Callback when locale changes |
|
|
45
|
+
| `i18n` | `I18nProvider` | No | undefined | I18n provider for translating component UI |
|
|
46
|
+
| `availableLocales` | `Array<{code: string, label: string}>` | No | See below | List of locales to show in dropdown |
|
|
47
|
+
| `label` | `string` | No | Translated 'Language' | Override label text |
|
|
48
|
+
| `compact` | `boolean` | No | `false` | Hide label, show only dropdown |
|
|
49
|
+
|
|
50
|
+
### Default Available Locales
|
|
51
|
+
|
|
52
|
+
By default, LocaleSwitcher shows the locales that have translations in PIE-QTI:
|
|
53
|
+
|
|
54
|
+
- `en-US` - English (US)
|
|
55
|
+
- `es-ES` - Español
|
|
56
|
+
- `fr-FR` - Français
|
|
57
|
+
- `nl-NL` - Nederlands
|
|
58
|
+
- `ro-RO` - Română
|
|
59
|
+
- `th-TH` - ไทย
|
|
60
|
+
|
|
61
|
+
### Customizing Available Locales
|
|
62
|
+
|
|
63
|
+
You can provide your own list of locales:
|
|
64
|
+
|
|
65
|
+
```svelte
|
|
66
|
+
<script>
|
|
67
|
+
const myLocales = [
|
|
68
|
+
{ code: 'en-US', label: 'English' },
|
|
69
|
+
{ code: 'es-ES', label: 'Spanish' },
|
|
70
|
+
{ code: 'fr-FR', label: 'French' },
|
|
71
|
+
];
|
|
72
|
+
</script>
|
|
73
|
+
|
|
74
|
+
<LocaleSwitcher
|
|
75
|
+
currentLocale={locale}
|
|
76
|
+
onChange={handleLocaleChange}
|
|
77
|
+
availableLocales={myLocales}
|
|
78
|
+
/>
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Compact Mode
|
|
82
|
+
|
|
83
|
+
Use compact mode when you want the dropdown without a label (e.g., in a toolbar):
|
|
84
|
+
|
|
85
|
+
```svelte
|
|
86
|
+
<LocaleSwitcher
|
|
87
|
+
currentLocale={locale}
|
|
88
|
+
onChange={handleLocaleChange}
|
|
89
|
+
i18n={i18nProvider}
|
|
90
|
+
compact={true}
|
|
91
|
+
/>
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Custom Label
|
|
95
|
+
|
|
96
|
+
Override the default "Language" label:
|
|
97
|
+
|
|
98
|
+
```svelte
|
|
99
|
+
<LocaleSwitcher
|
|
100
|
+
currentLocale={locale}
|
|
101
|
+
onChange={handleLocaleChange}
|
|
102
|
+
label="Choose Language"
|
|
103
|
+
/>
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Complete Example with Assessment Player
|
|
107
|
+
|
|
108
|
+
```svelte
|
|
109
|
+
<script lang="ts">
|
|
110
|
+
import { AssessmentShell } from '@pie-qti/assessment-player';
|
|
111
|
+
import { LocaleSwitcher, createSvelteI18nProvider } from '@pie-qti/i18n';
|
|
112
|
+
|
|
113
|
+
interface Props {
|
|
114
|
+
assessmentData: AssessmentData;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let { assessmentData }: Props = $props();
|
|
118
|
+
|
|
119
|
+
// Locale state
|
|
120
|
+
let locale = $state('en-US');
|
|
121
|
+
const i18nProvider = $derived(createSvelteI18nProvider(locale));
|
|
122
|
+
|
|
123
|
+
function handleLocaleChange(newLocale: string) {
|
|
124
|
+
locale = newLocale;
|
|
125
|
+
// Optionally save to localStorage
|
|
126
|
+
localStorage.setItem('preferredLocale', newLocale);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Restore saved locale on mount
|
|
130
|
+
$effect(() => {
|
|
131
|
+
const saved = localStorage.getItem('preferredLocale');
|
|
132
|
+
if (saved) {
|
|
133
|
+
locale = saved;
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
</script>
|
|
137
|
+
|
|
138
|
+
<div class="app-header">
|
|
139
|
+
<h1>My Assessment</h1>
|
|
140
|
+
<LocaleSwitcher
|
|
141
|
+
currentLocale={locale}
|
|
142
|
+
onChange={handleLocaleChange}
|
|
143
|
+
i18n={i18nProvider}
|
|
144
|
+
compact={true}
|
|
145
|
+
/>
|
|
146
|
+
</div>
|
|
147
|
+
|
|
148
|
+
<AssessmentShell
|
|
149
|
+
assessment={assessmentData}
|
|
150
|
+
i18n={i18nProvider}
|
|
151
|
+
/>
|
|
152
|
+
|
|
153
|
+
<style>
|
|
154
|
+
.app-header {
|
|
155
|
+
display: flex;
|
|
156
|
+
justify-content: space-between;
|
|
157
|
+
align-items: center;
|
|
158
|
+
padding: 1rem;
|
|
159
|
+
border-bottom: 1px solid #e5e5e5;
|
|
160
|
+
}
|
|
161
|
+
</style>
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Styling
|
|
165
|
+
|
|
166
|
+
The component uses CSS custom properties for theming:
|
|
167
|
+
|
|
168
|
+
```css
|
|
169
|
+
/* Override colors */
|
|
170
|
+
:root {
|
|
171
|
+
--color-base-100: #ffffff; /* Background */
|
|
172
|
+
--color-base-200: #f5f5f5; /* Alt background */
|
|
173
|
+
--color-base-300: #e5e5e5; /* Border */
|
|
174
|
+
--color-base-content: #000000; /* Text */
|
|
175
|
+
--color-primary: #3b82f6; /* Primary accent */
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/* Dark mode */
|
|
179
|
+
[data-theme='dark'] {
|
|
180
|
+
--color-base-100: #1f2937;
|
|
181
|
+
--color-base-200: #111827;
|
|
182
|
+
--color-base-300: #374151;
|
|
183
|
+
--color-base-content: #f9fafb;
|
|
184
|
+
--color-primary: #60a5fa;
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Or use the `part` attribute for Shadow DOM styling:
|
|
189
|
+
|
|
190
|
+
```css
|
|
191
|
+
/* Style the select element */
|
|
192
|
+
pie-locale-switcher::part(select) {
|
|
193
|
+
border-radius: 8px;
|
|
194
|
+
font-size: 16px;
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Accessibility
|
|
199
|
+
|
|
200
|
+
The component follows WCAG 2.2 Level AA guidelines:
|
|
201
|
+
|
|
202
|
+
- Properly labeled with `<label for="...">` association
|
|
203
|
+
- Includes `aria-label` on the select for screen readers
|
|
204
|
+
- Keyboard navigable (arrow keys, Enter to select)
|
|
205
|
+
- Focus visible with outline
|
|
206
|
+
- Sufficient color contrast (>4.5:1)
|
|
207
|
+
|
|
208
|
+
### Copying and Customizing
|
|
209
|
+
|
|
210
|
+
This is a reference implementation - feel free to copy and modify:
|
|
211
|
+
|
|
212
|
+
1. Copy [LocaleSwitcher.svelte](./LocaleSwitcher.svelte) to your project
|
|
213
|
+
2. Customize the styling, props, or behavior
|
|
214
|
+
3. Add features like:
|
|
215
|
+
- Flag icons for each locale
|
|
216
|
+
- Search/filter for many locales
|
|
217
|
+
- Automatic locale detection from browser
|
|
218
|
+
- Two-letter language codes display
|
|
219
|
+
|
|
220
|
+
### Related Documentation
|
|
221
|
+
|
|
222
|
+
- [INTEGRATION_GUIDE.md](../../docs/INTEGRATION_GUIDE.md) - How to add i18n to components
|
|
223
|
+
- [COMPONENT_PATTERNS.md](../../docs/COMPONENT_PATTERNS.md) - UI patterns and examples
|
|
224
|
+
- [MIGRATION.md](../../docs/MIGRATION.md) - Migrating existing components to i18n
|
package/src/core/I18n.ts
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default I18n provider implementation
|
|
3
|
+
*
|
|
4
|
+
* This is the framework's default implementation of the I18nProvider interface.
|
|
5
|
+
* It uses Vite's dynamic imports for lazy locale loading and provides simple
|
|
6
|
+
* interpolation and pluralization support.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { I18nProvider, InterpolationValues, PluralOptions } from './I18nProvider.js';
|
|
10
|
+
import type { LocaleCode, FrameworkLocaleCode, MessageKey, LocaleMessages } from './types.js';
|
|
11
|
+
|
|
12
|
+
// Use Vite's glob import to load all locales dynamically
|
|
13
|
+
// Load en-US eagerly (default/fallback locale), others lazily
|
|
14
|
+
// In dev mode, Vite uses .ts files directly. In production, use .js files from dist.
|
|
15
|
+
// @ts-expect-error - import.meta.glob is a Vite feature, not standard TypeScript
|
|
16
|
+
const eagerLocales = import.meta.glob('../locales/en-US.{js,ts}', { eager: true });
|
|
17
|
+
// @ts-expect-error - import.meta.glob is a Vite feature, not standard TypeScript
|
|
18
|
+
const lazyLocales = import.meta.glob('../locales/*.{js,ts}', { eager: false });
|
|
19
|
+
|
|
20
|
+
// Combine both for lookup
|
|
21
|
+
const localeModules = { ...lazyLocales };
|
|
22
|
+
|
|
23
|
+
export class DefaultI18nProvider implements I18nProvider {
|
|
24
|
+
private currentLocale: LocaleCode;
|
|
25
|
+
private messages: Record<LocaleCode, LocaleMessages>;
|
|
26
|
+
private customMessages: Record<string, LocaleMessages>; // Client-provided translations
|
|
27
|
+
private fallbackLocale: LocaleCode = 'en-US';
|
|
28
|
+
private loadedLocales: Set<LocaleCode> = new Set();
|
|
29
|
+
|
|
30
|
+
constructor(locale: LocaleCode = 'en-US', customMessages?: Record<string, LocaleMessages>) {
|
|
31
|
+
// Read persisted locale from localStorage if available
|
|
32
|
+
// This allows locale changes to persist across page reloads
|
|
33
|
+
const storedLocale = typeof localStorage !== 'undefined'
|
|
34
|
+
? localStorage.getItem('pie-qti-locale')
|
|
35
|
+
: null;
|
|
36
|
+
|
|
37
|
+
this.currentLocale = (storedLocale as LocaleCode) || locale;
|
|
38
|
+
this.messages = {} as Record<LocaleCode, LocaleMessages>;
|
|
39
|
+
this.customMessages = customMessages || {};
|
|
40
|
+
|
|
41
|
+
// Load en-US eagerly from the eager imports to ensure immediate availability
|
|
42
|
+
// Try both .ts (dev) and .js (prod) extensions
|
|
43
|
+
const enUSModule = (eagerLocales['../locales/en-US.ts'] || eagerLocales['../locales/en-US.js']) as any;
|
|
44
|
+
if (enUSModule) {
|
|
45
|
+
this.messages['en-US'] = enUSModule.default;
|
|
46
|
+
this.loadedLocales.add('en-US');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Load locale messages dynamically (framework locales only)
|
|
52
|
+
* For custom locales, use the customMessages parameter in the constructor or addCustomMessages()
|
|
53
|
+
*/
|
|
54
|
+
async loadLocale(locale: FrameworkLocaleCode): Promise<void> {
|
|
55
|
+
if (this.loadedLocales.has(locale)) {
|
|
56
|
+
return; // Already loaded
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
// Try both .ts (dev) and .js (prod) extensions
|
|
61
|
+
const modulePathTs = `../locales/${locale}.ts`;
|
|
62
|
+
const modulePathJs = `../locales/${locale}.js`;
|
|
63
|
+
const loader = localeModules[modulePathTs] || localeModules[modulePathJs];
|
|
64
|
+
|
|
65
|
+
if (!loader) {
|
|
66
|
+
throw new Error(`Locale '${locale}' not found in available modules`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const module = await loader();
|
|
70
|
+
this.messages[locale] = (module as any).default;
|
|
71
|
+
this.loadedLocales.add(locale);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
console.error(`[i18n] Failed to load locale '${locale}':`, error);
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Set current locale
|
|
80
|
+
* For framework locales, loadLocale() should be called first.
|
|
81
|
+
* For custom locales, translations should be provided via customMessages.
|
|
82
|
+
*/
|
|
83
|
+
setLocale(locale: string): void {
|
|
84
|
+
// Allow setting custom locales even if not in loadedLocales
|
|
85
|
+
// They will use customMessages + fallback to en-US
|
|
86
|
+
if (!this.loadedLocales.has(locale as LocaleCode) && !this.customMessages[locale]) {
|
|
87
|
+
console.warn(`[i18n] Locale '${locale}' not loaded and no custom messages provided. Using fallback locale.`);
|
|
88
|
+
}
|
|
89
|
+
this.currentLocale = locale as LocaleCode;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Get current locale
|
|
94
|
+
*/
|
|
95
|
+
getLocale(): string {
|
|
96
|
+
return this.currentLocale;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Translate message key with optional interpolation
|
|
101
|
+
* @example t('upload.label') => "Upload a file"
|
|
102
|
+
* @example t('upload.selected', { name: 'file.pdf' }) => "Selected: file.pdf"
|
|
103
|
+
*/
|
|
104
|
+
t(key: string, values?: Record<string, any>): string {
|
|
105
|
+
const message = this.getMessage(key);
|
|
106
|
+
if (!message) {
|
|
107
|
+
if (process.env.NODE_ENV === 'development') {
|
|
108
|
+
console.warn(`[i18n] Missing translation: ${key} (locale: ${this.currentLocale})`);
|
|
109
|
+
}
|
|
110
|
+
return key; // Fallback to key itself
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (!values) return message;
|
|
114
|
+
|
|
115
|
+
// Simple interpolation: replace {key} with values[key]
|
|
116
|
+
return message.replace(/\{(\w+)\}/g, (match, key) => {
|
|
117
|
+
return String(values[key] ?? match);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Pluralization support
|
|
123
|
+
* @example plural('upload.fileCount', { count: 1 }) => "1 file selected"
|
|
124
|
+
* @example plural('upload.fileCount', { count: 5 }) => "5 files selected"
|
|
125
|
+
*/
|
|
126
|
+
plural(key: string, options: PluralOptions): string {
|
|
127
|
+
const { count } = options;
|
|
128
|
+
const pluralKey = count === 1 ? `${key}.one` : `${key}.other`;
|
|
129
|
+
return this.t(pluralKey, options);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Format number according to locale
|
|
134
|
+
*/
|
|
135
|
+
formatNumber(value: number, options?: Intl.NumberFormatOptions): string {
|
|
136
|
+
return new Intl.NumberFormat(this.currentLocale, options).format(value);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Format date according to locale
|
|
141
|
+
*/
|
|
142
|
+
formatDate(date: Date, options?: Intl.DateTimeFormatOptions): string {
|
|
143
|
+
return new Intl.DateTimeFormat(this.currentLocale, options).format(date);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Get nested message by dot notation key
|
|
148
|
+
* Priority: custom messages > current locale > fallback locale
|
|
149
|
+
*/
|
|
150
|
+
private getMessage(key: string): string | undefined {
|
|
151
|
+
// 1. Try custom messages first (client overrides)
|
|
152
|
+
const customMessage = this.getMessageFromObject(key, this.customMessages[this.currentLocale]);
|
|
153
|
+
if (customMessage !== undefined) return customMessage;
|
|
154
|
+
|
|
155
|
+
// 2. Try current locale from framework defaults
|
|
156
|
+
const messages = this.messages[this.currentLocale];
|
|
157
|
+
if (messages) {
|
|
158
|
+
const message = this.getMessageFromObject(key, messages);
|
|
159
|
+
if (message !== undefined) return message;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// 3. Try custom fallback locale
|
|
163
|
+
const customFallback = this.getMessageFromObject(key, this.customMessages[this.fallbackLocale]);
|
|
164
|
+
if (customFallback !== undefined) return customFallback;
|
|
165
|
+
|
|
166
|
+
// 4. Try framework fallback locale
|
|
167
|
+
return this.getMessageFromFallback(key);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Get message from a specific messages object
|
|
172
|
+
*/
|
|
173
|
+
private getMessageFromObject(key: string, messages: any): string | undefined {
|
|
174
|
+
if (!messages) return undefined;
|
|
175
|
+
|
|
176
|
+
const parts = key.split('.');
|
|
177
|
+
let current: any = messages;
|
|
178
|
+
|
|
179
|
+
for (const part of parts) {
|
|
180
|
+
current = current[part];
|
|
181
|
+
if (current === undefined) return undefined;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return current;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Get message from fallback locale (framework defaults)
|
|
189
|
+
*/
|
|
190
|
+
private getMessageFromFallback(key: string): string | undefined {
|
|
191
|
+
const messages = this.messages[this.fallbackLocale];
|
|
192
|
+
return this.getMessageFromObject(key, messages);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Add or update custom messages for a locale
|
|
197
|
+
* This allows clients to provide their own translations or override defaults
|
|
198
|
+
*/
|
|
199
|
+
addCustomMessages(locale: string, messages: LocaleMessages): void {
|
|
200
|
+
this.customMessages[locale] = this.deepMerge(
|
|
201
|
+
this.customMessages[locale] || {},
|
|
202
|
+
messages
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Deep merge two objects (for merging translation objects)
|
|
208
|
+
*/
|
|
209
|
+
private deepMerge(target: any, source: any): any {
|
|
210
|
+
const result = { ...target };
|
|
211
|
+
for (const key in source) {
|
|
212
|
+
if (source[key] && typeof source[key] === 'object' && !Array.isArray(source[key])) {
|
|
213
|
+
result[key] = this.deepMerge(result[key] || {}, source[key]);
|
|
214
|
+
} else {
|
|
215
|
+
result[key] = source[key];
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return result;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Factory function to create a default i18n provider instance
|
|
224
|
+
* @param locale - Initial locale code (default: 'en-US')
|
|
225
|
+
* @param customMessages - Optional custom translations to add or override framework defaults
|
|
226
|
+
* @returns DefaultI18nProvider instance
|
|
227
|
+
*/
|
|
228
|
+
export function createDefaultI18nProvider(
|
|
229
|
+
locale: LocaleCode = 'en-US',
|
|
230
|
+
customMessages?: Record<string, LocaleMessages>
|
|
231
|
+
): I18nProvider {
|
|
232
|
+
return new DefaultI18nProvider(locale, customMessages);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export type { LocaleCode, MessageKey, InterpolationValues, PluralOptions };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-agnostic i18n provider interface
|
|
3
|
+
*
|
|
4
|
+
* This interface defines the contract for internationalization services.
|
|
5
|
+
* Users can provide their own implementations (ICU MessageFormat, i18next, etc.)
|
|
6
|
+
* or use the default implementation provided by the framework.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export interface I18nProvider {
|
|
10
|
+
/**
|
|
11
|
+
* Get the current locale code (e.g., 'en-US', 'fr-FR')
|
|
12
|
+
*/
|
|
13
|
+
getLocale(): string;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Set the current locale and optionally load locale messages
|
|
17
|
+
* @param locale - BCP 47 locale code
|
|
18
|
+
*/
|
|
19
|
+
setLocale(locale: string): Promise<void> | void;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Translate a message key with optional default and/or interpolation
|
|
23
|
+
* @param key - Message key (e.g., 'common.submit', 'assessment.question')
|
|
24
|
+
* @param defaultOrValues - Optional default string or interpolation values
|
|
25
|
+
* @param values - Optional interpolation values (if second arg is default string)
|
|
26
|
+
* @returns Translated message, default, or key if not found
|
|
27
|
+
*/
|
|
28
|
+
t(key: string, defaultOrValues?: Record<string, any> | string, values?: Record<string, any>): string;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Optional: Pluralization support
|
|
32
|
+
* @param key - Base message key
|
|
33
|
+
* @param options - Options including count and interpolation values
|
|
34
|
+
* @returns Pluralized and translated message
|
|
35
|
+
*/
|
|
36
|
+
plural?(key: string, options: { count: number; [key: string]: any }): string;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Optional: Format number according to locale
|
|
40
|
+
* @param value - Number to format
|
|
41
|
+
* @param options - Intl.NumberFormat options
|
|
42
|
+
* @returns Formatted number string
|
|
43
|
+
*/
|
|
44
|
+
formatNumber?(value: number, options?: Intl.NumberFormatOptions): string;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Optional: Format date according to locale
|
|
48
|
+
* @param date - Date to format
|
|
49
|
+
* @param options - Intl.DateTimeFormat options
|
|
50
|
+
* @returns Formatted date string
|
|
51
|
+
*/
|
|
52
|
+
formatDate?(date: Date, options?: Intl.DateTimeFormatOptions): string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Interpolation values for dynamic message content
|
|
57
|
+
*/
|
|
58
|
+
export interface InterpolationValues {
|
|
59
|
+
[key: string]: string | number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Pluralization options
|
|
64
|
+
*/
|
|
65
|
+
export interface PluralOptions {
|
|
66
|
+
count: number;
|
|
67
|
+
[key: string]: string | number;
|
|
68
|
+
}
|