@pie-qti/i18n 0.1.0 → 0.1.4
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/dist/core/I18n.d.ts +3 -2
- package/dist/core/I18n.d.ts.map +1 -1
- package/dist/core/I18n.js +11 -6
- package/dist/core/I18n.js.map +1 -1
- package/dist/locales/ar-SA.d.ts +43 -0
- package/dist/locales/ar-SA.d.ts.map +1 -1
- package/dist/locales/ar-SA.js +44 -0
- package/dist/locales/ar-SA.js.map +1 -1
- package/dist/locales/en-US.d.ts +43 -0
- package/dist/locales/en-US.d.ts.map +1 -1
- package/dist/locales/en-US.js +44 -0
- package/dist/locales/en-US.js.map +1 -1
- package/dist/locales/es-ES.d.ts +43 -0
- package/dist/locales/es-ES.d.ts.map +1 -1
- package/dist/locales/es-ES.js +44 -0
- package/dist/locales/es-ES.js.map +1 -1
- package/dist/locales/fr-FR.d.ts +43 -0
- package/dist/locales/fr-FR.d.ts.map +1 -1
- package/dist/locales/fr-FR.js +44 -0
- package/dist/locales/fr-FR.js.map +1 -1
- package/dist/locales/nl-NL.d.ts +43 -0
- package/dist/locales/nl-NL.d.ts.map +1 -1
- package/dist/locales/nl-NL.js +44 -0
- package/dist/locales/nl-NL.js.map +1 -1
- package/dist/locales/ro-RO.d.ts +43 -0
- package/dist/locales/ro-RO.d.ts.map +1 -1
- package/dist/locales/ro-RO.js +44 -0
- package/dist/locales/ro-RO.js.map +1 -1
- package/dist/locales/th-TH.d.ts +43 -0
- package/dist/locales/th-TH.d.ts.map +1 -1
- package/dist/locales/th-TH.js +44 -0
- package/dist/locales/th-TH.js.map +1 -1
- package/dist/locales/zh-CN.d.ts +43 -0
- package/dist/locales/zh-CN.d.ts.map +1 -1
- package/dist/locales/zh-CN.js +44 -0
- package/dist/locales/zh-CN.js.map +1 -1
- package/dist/providers/SvelteI18nProvider.d.ts +5 -14
- package/dist/providers/SvelteI18nProvider.d.ts.map +1 -1
- package/dist/providers/SvelteI18nProvider.js +7 -47
- package/dist/providers/SvelteI18nProvider.js.map +1 -1
- package/package.json +29 -7
- package/.turbo/turbo-build.log +0 -264
- package/docs/ADVANCED_FEATURES.md +0 -556
- package/docs/COMPONENT_PATTERNS.md +0 -896
- package/docs/INTEGRATION_GUIDE.md +0 -695
- package/scripts/check-translations.ts +0 -289
- package/scripts/scan-hardcoded-strings.ts +0 -266
- package/src/core/I18n.ts +0 -235
- package/src/core/I18nProvider.ts +0 -68
- package/src/core/types.ts +0 -45
- package/src/index.ts +0 -22
- package/src/locales/ar-SA.ts +0 -860
- package/src/locales/en-US.ts +0 -828
- package/src/locales/es-ES.ts +0 -816
- package/src/locales/fr-FR.ts +0 -815
- package/src/locales/nl-NL.ts +0 -815
- package/src/locales/ro-RO.ts +0 -815
- package/src/locales/th-TH.ts +0 -816
- package/src/locales/zh-CN.ts +0 -828
- package/src/providers/SvelteI18nProvider.ts +0 -170
- package/tests/i18n.test.ts +0 -146
- package/tsconfig.json +0 -10
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -1,695 +0,0 @@
|
|
|
1
|
-
# I18n Integration Guide
|
|
2
|
-
|
|
3
|
-
**How to Integrate Internationalization in PIE-QTI Components**
|
|
4
|
-
|
|
5
|
-
This guide explains how to add and use i18n in your components.
|
|
6
|
-
|
|
7
|
-
---
|
|
8
|
-
|
|
9
|
-
## Table of Contents
|
|
10
|
-
|
|
11
|
-
1. [Quick Start](#quick-start)
|
|
12
|
-
2. [Why i18n is Optional](#why-i18n-is-optional)
|
|
13
|
-
3. [Adding i18n to a Component](#adding-i18n-to-a-component)
|
|
14
|
-
4. [Using Reactive Translations](#using-reactive-translations)
|
|
15
|
-
5. [Adding New Message Keys](#adding-new-message-keys)
|
|
16
|
-
6. [Testing with Different Locales](#testing-with-different-locales)
|
|
17
|
-
7. [Common Pitfalls](#common-pitfalls)
|
|
18
|
-
8. [Best Practices](#best-practices)
|
|
19
|
-
|
|
20
|
-
---
|
|
21
|
-
|
|
22
|
-
## Quick Start
|
|
23
|
-
|
|
24
|
-
### For Component Authors
|
|
25
|
-
|
|
26
|
-
```svelte
|
|
27
|
-
<script lang="ts">
|
|
28
|
-
import type { I18nProvider } from '@pie-qti/i18n';
|
|
29
|
-
|
|
30
|
-
interface Props {
|
|
31
|
-
// ... other props
|
|
32
|
-
i18n?: I18nProvider; // ✅ Always optional
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
let { i18n, /* other props */ }: Props = $props();
|
|
36
|
-
|
|
37
|
-
// ✅ Use $derived for reactive translations
|
|
38
|
-
const translations = $derived({
|
|
39
|
-
label: i18n?.t('component.label') ?? 'component.label',
|
|
40
|
-
submit: i18n?.t('common.submit') ?? 'common.submit',
|
|
41
|
-
});
|
|
42
|
-
</script>
|
|
43
|
-
|
|
44
|
-
<button>{translations.submit}</button>
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
### For Component Users
|
|
48
|
-
|
|
49
|
-
```svelte
|
|
50
|
-
<script lang="ts">
|
|
51
|
-
import { SvelteI18nProvider } from '@pie-qti/i18n';
|
|
52
|
-
import MyComponent from './MyComponent.svelte';
|
|
53
|
-
|
|
54
|
-
const i18nProvider = new SvelteI18nProvider('en-US');
|
|
55
|
-
</script>
|
|
56
|
-
|
|
57
|
-
<MyComponent i18n={i18nProvider} />
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
---
|
|
61
|
-
|
|
62
|
-
## Why i18n is Optional
|
|
63
|
-
|
|
64
|
-
The i18n system is designed to be **optional but recommended**:
|
|
65
|
-
|
|
66
|
-
### When i18n is Provided
|
|
67
|
-
|
|
68
|
-
```typescript
|
|
69
|
-
// Framework handles locale fallback automatically:
|
|
70
|
-
// 1. Custom messages (if provided)
|
|
71
|
-
// 2. Current locale (e.g., es-ES)
|
|
72
|
-
// 3. en-US (always eagerly loaded)
|
|
73
|
-
// 4. Key itself (if still missing)
|
|
74
|
-
|
|
75
|
-
i18n.t('common.submit') // Returns: "Submit" (en-US default)
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
### When i18n is Missing
|
|
79
|
-
|
|
80
|
-
```typescript
|
|
81
|
-
// Component falls back to message key:
|
|
82
|
-
i18n?.t('common.submit') ?? 'common.submit' // Returns: "common.submit"
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
### Benefits of This Approach
|
|
86
|
-
|
|
87
|
-
1. **Development Visibility** - Missing i18n integration is immediately obvious (shows keys)
|
|
88
|
-
2. **Framework Flexibility** - Components work in any context (testing, web components, etc.)
|
|
89
|
-
3. **Graceful Degradation** - Apps work without i18n (with keys as labels)
|
|
90
|
-
4. **Type Safety** - Keys are searchable and refactorable
|
|
91
|
-
|
|
92
|
-
---
|
|
93
|
-
|
|
94
|
-
## Adding i18n to a Component
|
|
95
|
-
|
|
96
|
-
### Step 1: Add the i18n Prop
|
|
97
|
-
|
|
98
|
-
```typescript
|
|
99
|
-
import type { I18nProvider } from '@pie-qti/i18n';
|
|
100
|
-
|
|
101
|
-
interface Props {
|
|
102
|
-
interaction: InteractionData;
|
|
103
|
-
response: string | null;
|
|
104
|
-
disabled?: boolean;
|
|
105
|
-
i18n?: I18nProvider; // ✅ Always optional
|
|
106
|
-
onChange: (value: string | null) => void;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
let {
|
|
110
|
-
interaction,
|
|
111
|
-
response,
|
|
112
|
-
disabled = false,
|
|
113
|
-
i18n, // ✅ Destructure it
|
|
114
|
-
onChange
|
|
115
|
-
}: Props = $props();
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
### Step 2: Create Reactive Translations
|
|
119
|
-
|
|
120
|
-
```typescript
|
|
121
|
-
// ✅ CORRECT - Use $derived for reactivity
|
|
122
|
-
const translations = $derived({
|
|
123
|
-
label: i18n?.t('interactions.myComponent.label') ?? 'interactions.myComponent.label',
|
|
124
|
-
instruction: i18n?.t('interactions.myComponent.instruction') ?? 'interactions.myComponent.instruction',
|
|
125
|
-
submit: i18n?.t('common.submit') ?? 'common.submit',
|
|
126
|
-
|
|
127
|
-
// For interpolation, use functions:
|
|
128
|
-
itemSelected: (count: number) =>
|
|
129
|
-
i18n?.t('interactions.myComponent.itemSelected', { count }) ??
|
|
130
|
-
`interactions.myComponent.itemSelected (${count})`,
|
|
131
|
-
});
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
**Why $derived?**
|
|
135
|
-
- Translations reactively update when locale changes
|
|
136
|
-
- Computed once per locale change (efficient)
|
|
137
|
-
- Clean, declarative syntax
|
|
138
|
-
|
|
139
|
-
### Step 3: Use Translations in Template
|
|
140
|
-
|
|
141
|
-
```svelte
|
|
142
|
-
<div class="form-control">
|
|
143
|
-
<label class="label">
|
|
144
|
-
<span class="label-text">{translations.label}</span>
|
|
145
|
-
</label>
|
|
146
|
-
<input
|
|
147
|
-
class="input input-bordered"
|
|
148
|
-
placeholder={translations.instruction}
|
|
149
|
-
aria-label={translations.label}
|
|
150
|
-
/>
|
|
151
|
-
</div>
|
|
152
|
-
|
|
153
|
-
<button class="btn btn-primary" onclick={handleSubmit}>
|
|
154
|
-
{translations.submit}
|
|
155
|
-
</button>
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
### Step 4: Pass i18n to Child Components
|
|
159
|
-
|
|
160
|
-
```svelte
|
|
161
|
-
<script lang="ts">
|
|
162
|
-
import ChildComponent from './ChildComponent.svelte';
|
|
163
|
-
|
|
164
|
-
let { i18n }: { i18n?: I18nProvider } = $props();
|
|
165
|
-
</script>
|
|
166
|
-
|
|
167
|
-
<ChildComponent {i18n} />
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
---
|
|
171
|
-
|
|
172
|
-
## Using Reactive Translations
|
|
173
|
-
|
|
174
|
-
### Basic Usage
|
|
175
|
-
|
|
176
|
-
```typescript
|
|
177
|
-
// ✅ CORRECT - Reactive with $derived
|
|
178
|
-
const translations = $derived({
|
|
179
|
-
title: i18n?.t('page.title') ?? 'page.title',
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
// ❌ INCORRECT - Won't update on locale change
|
|
183
|
-
const title = i18n?.t('page.title') ?? 'page.title';
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
### Interpolation
|
|
187
|
-
|
|
188
|
-
```typescript
|
|
189
|
-
const translations = $derived({
|
|
190
|
-
// Simple interpolation
|
|
191
|
-
greeting: (name: string) =>
|
|
192
|
-
i18n?.t('common.greeting', { name }) ?? `common.greeting (${name})`,
|
|
193
|
-
|
|
194
|
-
// Multiple values
|
|
195
|
-
dateRange: (start: string, end: string) =>
|
|
196
|
-
i18n?.t('common.dateRange', { start, end }) ??
|
|
197
|
-
`common.dateRange (${start} - ${end})`,
|
|
198
|
-
|
|
199
|
-
// Number formatting
|
|
200
|
-
score: (points: number) =>
|
|
201
|
-
i18n?.t('assessment.score', { points }) ??
|
|
202
|
-
`assessment.score (${points})`,
|
|
203
|
-
});
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
Usage in template:
|
|
207
|
-
|
|
208
|
-
```svelte
|
|
209
|
-
<p>{translations.greeting('Alice')}</p>
|
|
210
|
-
<p>{translations.dateRange('2024-01-01', '2024-12-31')}</p>
|
|
211
|
-
<p>{translations.score(95)}</p>
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
### Conditional Translations
|
|
215
|
-
|
|
216
|
-
```typescript
|
|
217
|
-
const translations = $derived({
|
|
218
|
-
status: disabled
|
|
219
|
-
? i18n?.t('common.disabled') ?? 'common.disabled'
|
|
220
|
-
: i18n?.t('common.enabled') ?? 'common.enabled',
|
|
221
|
-
});
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
### Computed Translations
|
|
225
|
-
|
|
226
|
-
```typescript
|
|
227
|
-
const itemCount = $state(0);
|
|
228
|
-
|
|
229
|
-
const translations = $derived({
|
|
230
|
-
// This automatically updates when itemCount changes
|
|
231
|
-
summary: i18n?.t('interactions.itemCount', { count: itemCount }) ??
|
|
232
|
-
`interactions.itemCount (${itemCount})`,
|
|
233
|
-
});
|
|
234
|
-
```
|
|
235
|
-
|
|
236
|
-
---
|
|
237
|
-
|
|
238
|
-
## Adding New Message Keys
|
|
239
|
-
|
|
240
|
-
### Step 1: Choose the Right Namespace
|
|
241
|
-
|
|
242
|
-
Message keys are organized by category:
|
|
243
|
-
|
|
244
|
-
```typescript
|
|
245
|
-
common.* // Shared across all components
|
|
246
|
-
units.* // Units of measurement
|
|
247
|
-
validation.* // Validation messages
|
|
248
|
-
interactions.* // Interaction-specific messages
|
|
249
|
-
item.* // Item player messages
|
|
250
|
-
itemSession.* // Item session messages
|
|
251
|
-
feedback.* // Feedback messages
|
|
252
|
-
assessment.* // Assessment player messages
|
|
253
|
-
accessibility.* // Accessibility messages
|
|
254
|
-
```
|
|
255
|
-
|
|
256
|
-
### Step 2: Add to en-US Locale
|
|
257
|
-
|
|
258
|
-
Open `packages/i18n/src/locales/en-US.ts`:
|
|
259
|
-
|
|
260
|
-
```typescript
|
|
261
|
-
export default {
|
|
262
|
-
// ... existing keys
|
|
263
|
-
|
|
264
|
-
interactions: {
|
|
265
|
-
// ... existing interactions
|
|
266
|
-
|
|
267
|
-
myComponent: {
|
|
268
|
-
label: 'Component Label',
|
|
269
|
-
instruction: 'Enter your response',
|
|
270
|
-
submit: 'Submit Response',
|
|
271
|
-
clear: 'Clear Response',
|
|
272
|
-
|
|
273
|
-
// For interpolation
|
|
274
|
-
itemsSelected: '{count} items selected',
|
|
275
|
-
|
|
276
|
-
// For aria-labels
|
|
277
|
-
ariaLabel: 'Interactive component',
|
|
278
|
-
ariaDescription: 'Use arrow keys to navigate',
|
|
279
|
-
},
|
|
280
|
-
},
|
|
281
|
-
};
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
### Step 3: Use in Component
|
|
285
|
-
|
|
286
|
-
```typescript
|
|
287
|
-
const translations = $derived({
|
|
288
|
-
label: i18n?.t('interactions.myComponent.label') ?? 'interactions.myComponent.label',
|
|
289
|
-
instruction: i18n?.t('interactions.myComponent.instruction') ?? 'interactions.myComponent.instruction',
|
|
290
|
-
});
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
### Step 4: TypeScript Autocomplete
|
|
294
|
-
|
|
295
|
-
TypeScript will automatically provide autocomplete for your new keys! The i18n package generates types from the en-US locale.
|
|
296
|
-
|
|
297
|
-
---
|
|
298
|
-
|
|
299
|
-
## Testing with Different Locales
|
|
300
|
-
|
|
301
|
-
### In Development
|
|
302
|
-
|
|
303
|
-
```svelte
|
|
304
|
-
<script lang="ts">
|
|
305
|
-
import { SvelteI18nProvider } from '@pie-qti/i18n';
|
|
306
|
-
|
|
307
|
-
// Test with different locales
|
|
308
|
-
const i18nProvider = new SvelteI18nProvider('es-ES'); // Spanish
|
|
309
|
-
// const i18nProvider = new SvelteI18nProvider('fr-FR'); // French
|
|
310
|
-
// const i18nProvider = new SvelteI18nProvider('nl-NL'); // Dutch
|
|
311
|
-
</script>
|
|
312
|
-
|
|
313
|
-
<YourComponent i18n={i18nProvider} />
|
|
314
|
-
```
|
|
315
|
-
|
|
316
|
-
### Switching Locales
|
|
317
|
-
|
|
318
|
-
```svelte
|
|
319
|
-
<script lang="ts">
|
|
320
|
-
import { SvelteI18nProvider } from '@pie-qti/i18n';
|
|
321
|
-
|
|
322
|
-
const i18nProvider = new SvelteI18nProvider('en-US');
|
|
323
|
-
|
|
324
|
-
async function switchLocale(locale: string) {
|
|
325
|
-
await i18nProvider.setLocale(locale);
|
|
326
|
-
// All components reactively update!
|
|
327
|
-
}
|
|
328
|
-
</script>
|
|
329
|
-
|
|
330
|
-
<select onchange={(e) => switchLocale(e.currentTarget.value)}>
|
|
331
|
-
<option value="en-US">English</option>
|
|
332
|
-
<option value="es-ES">Español</option>
|
|
333
|
-
<option value="fr-FR">Français</option>
|
|
334
|
-
</select>
|
|
335
|
-
|
|
336
|
-
<YourComponent i18n={i18nProvider} />
|
|
337
|
-
```
|
|
338
|
-
|
|
339
|
-
### Without i18n (Testing)
|
|
340
|
-
|
|
341
|
-
```svelte
|
|
342
|
-
<!-- Component works without i18n - shows keys -->
|
|
343
|
-
<YourComponent />
|
|
344
|
-
```
|
|
345
|
-
|
|
346
|
-
---
|
|
347
|
-
|
|
348
|
-
## Common Pitfalls
|
|
349
|
-
|
|
350
|
-
### ❌ Using English String Fallbacks
|
|
351
|
-
|
|
352
|
-
```typescript
|
|
353
|
-
// ❌ WRONG - Hides missing i18n integration
|
|
354
|
-
const label = i18n?.t('component.label') ?? 'Label';
|
|
355
|
-
|
|
356
|
-
// ✅ CORRECT - Shows descriptive key if i18n missing
|
|
357
|
-
const label = i18n?.t('component.label') ?? 'component.label';
|
|
358
|
-
```
|
|
359
|
-
|
|
360
|
-
### ❌ Not Using $derived
|
|
361
|
-
|
|
362
|
-
```typescript
|
|
363
|
-
// ❌ WRONG - Won't update on locale change
|
|
364
|
-
const title = i18n?.t('page.title') ?? 'page.title';
|
|
365
|
-
|
|
366
|
-
// ✅ CORRECT - Reactive translation
|
|
367
|
-
const translations = $derived({
|
|
368
|
-
title: i18n?.t('page.title') ?? 'page.title',
|
|
369
|
-
});
|
|
370
|
-
```
|
|
371
|
-
|
|
372
|
-
### ❌ Using Translations in $effect
|
|
373
|
-
|
|
374
|
-
```typescript
|
|
375
|
-
// ❌ WRONG - Effect recreates on every translation change
|
|
376
|
-
$effect(() => {
|
|
377
|
-
const label = i18n?.t('component.label');
|
|
378
|
-
// Do something
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
// ✅ CORRECT - Use untrack() if needed
|
|
382
|
-
import { untrack } from 'svelte';
|
|
383
|
-
|
|
384
|
-
$effect(() => {
|
|
385
|
-
const label = untrack(() => i18n?.t('component.label'));
|
|
386
|
-
// Effect won't rerun on locale change
|
|
387
|
-
});
|
|
388
|
-
```
|
|
389
|
-
|
|
390
|
-
### ❌ Forgetting Optional Chaining
|
|
391
|
-
|
|
392
|
-
```typescript
|
|
393
|
-
// ❌ WRONG - Crashes if i18n is undefined
|
|
394
|
-
const label = i18n.t('component.label');
|
|
395
|
-
|
|
396
|
-
// ✅ CORRECT - Safe with optional chaining
|
|
397
|
-
const label = i18n?.t('component.label') ?? 'component.label';
|
|
398
|
-
```
|
|
399
|
-
|
|
400
|
-
### ❌ Hardcoding Locale in Component
|
|
401
|
-
|
|
402
|
-
```typescript
|
|
403
|
-
// ❌ WRONG - Hardcoded locale
|
|
404
|
-
const label = i18n?.t('component.label', { locale: 'en-US' });
|
|
405
|
-
|
|
406
|
-
// ✅ CORRECT - Use current locale
|
|
407
|
-
const label = i18n?.t('component.label');
|
|
408
|
-
```
|
|
409
|
-
|
|
410
|
-
---
|
|
411
|
-
|
|
412
|
-
## Best Practices
|
|
413
|
-
|
|
414
|
-
### 1. Always Use Descriptive Keys
|
|
415
|
-
|
|
416
|
-
```typescript
|
|
417
|
-
// ✅ GOOD - Clear, hierarchical keys
|
|
418
|
-
'interactions.drawing.clearCanvas'
|
|
419
|
-
'assessment.navigation.nextItem'
|
|
420
|
-
'feedback.correctAnswer'
|
|
421
|
-
|
|
422
|
-
// ❌ BAD - Ambiguous keys
|
|
423
|
-
'clear'
|
|
424
|
-
'next'
|
|
425
|
-
'correct'
|
|
426
|
-
```
|
|
427
|
-
|
|
428
|
-
### 2. Group Related Keys
|
|
429
|
-
|
|
430
|
-
```typescript
|
|
431
|
-
export default {
|
|
432
|
-
interactions: {
|
|
433
|
-
drawing: {
|
|
434
|
-
label: 'Drawing Canvas',
|
|
435
|
-
clear: 'Clear',
|
|
436
|
-
undo: 'Undo',
|
|
437
|
-
redo: 'Redo',
|
|
438
|
-
// All drawing-related keys together
|
|
439
|
-
},
|
|
440
|
-
},
|
|
441
|
-
};
|
|
442
|
-
```
|
|
443
|
-
|
|
444
|
-
### 3. Use Common Keys for Common Actions
|
|
445
|
-
|
|
446
|
-
```typescript
|
|
447
|
-
// ✅ GOOD - Reuse common keys
|
|
448
|
-
const translations = $derived({
|
|
449
|
-
submit: i18n?.t('common.submit') ?? 'common.submit',
|
|
450
|
-
cancel: i18n?.t('common.cancel') ?? 'common.cancel',
|
|
451
|
-
});
|
|
452
|
-
|
|
453
|
-
// ❌ BAD - Creating redundant keys
|
|
454
|
-
const translations = $derived({
|
|
455
|
-
submit: i18n?.t('myComponent.submit') ?? 'myComponent.submit',
|
|
456
|
-
cancel: i18n?.t('myComponent.cancel') ?? 'myComponent.cancel',
|
|
457
|
-
});
|
|
458
|
-
```
|
|
459
|
-
|
|
460
|
-
### 4. Provide Context in Aria Labels
|
|
461
|
-
|
|
462
|
-
```svelte
|
|
463
|
-
<button
|
|
464
|
-
aria-label="{translations.item} {index + 1}. {translations.instructions}"
|
|
465
|
-
onclick={handleClick}
|
|
466
|
-
>
|
|
467
|
-
{translations.item} {index + 1}
|
|
468
|
-
</button>
|
|
469
|
-
```
|
|
470
|
-
|
|
471
|
-
### 5. Use Screen Reader Announcements
|
|
472
|
-
|
|
473
|
-
```svelte
|
|
474
|
-
<script lang="ts">
|
|
475
|
-
let announceText = $state('');
|
|
476
|
-
|
|
477
|
-
const translations = $derived({
|
|
478
|
-
itemAdded: (item: string) =>
|
|
479
|
-
i18n?.t('component.itemAdded', { item }) ??
|
|
480
|
-
`component.itemAdded (${item})`,
|
|
481
|
-
});
|
|
482
|
-
|
|
483
|
-
function addItem(item: string) {
|
|
484
|
-
// Add item logic
|
|
485
|
-
announceText = translations.itemAdded(item);
|
|
486
|
-
}
|
|
487
|
-
</script>
|
|
488
|
-
|
|
489
|
-
<!-- Live region for screen readers -->
|
|
490
|
-
<div aria-live="polite" aria-atomic="true" class="sr-only">
|
|
491
|
-
{announceText}
|
|
492
|
-
</div>
|
|
493
|
-
|
|
494
|
-
<style>
|
|
495
|
-
.sr-only {
|
|
496
|
-
position: absolute;
|
|
497
|
-
width: 1px;
|
|
498
|
-
height: 1px;
|
|
499
|
-
padding: 0;
|
|
500
|
-
margin: -1px;
|
|
501
|
-
overflow: hidden;
|
|
502
|
-
clip: rect(0, 0, 0, 0);
|
|
503
|
-
white-space: nowrap;
|
|
504
|
-
border-width: 0;
|
|
505
|
-
}
|
|
506
|
-
</style>
|
|
507
|
-
```
|
|
508
|
-
|
|
509
|
-
### 6. Handle Pluralization
|
|
510
|
-
|
|
511
|
-
```typescript
|
|
512
|
-
// In locale file
|
|
513
|
-
export default {
|
|
514
|
-
interactions: {
|
|
515
|
-
upload: {
|
|
516
|
-
filesSelected: {
|
|
517
|
-
one: '1 file selected',
|
|
518
|
-
other: '{count} files selected',
|
|
519
|
-
},
|
|
520
|
-
},
|
|
521
|
-
},
|
|
522
|
-
};
|
|
523
|
-
|
|
524
|
-
// In component
|
|
525
|
-
const translations = $derived({
|
|
526
|
-
filesSelected: (count: number) =>
|
|
527
|
-
i18n?.plural('interactions.upload.filesSelected', { count }) ??
|
|
528
|
-
`${count} files selected`,
|
|
529
|
-
});
|
|
530
|
-
```
|
|
531
|
-
|
|
532
|
-
### 7. Format Numbers and Dates
|
|
533
|
-
|
|
534
|
-
```typescript
|
|
535
|
-
const translations = $derived({
|
|
536
|
-
// Use built-in formatting
|
|
537
|
-
score: (points: number) =>
|
|
538
|
-
i18n?.formatNumber?.(points) ?? points.toString(),
|
|
539
|
-
|
|
540
|
-
deadline: (date: Date) =>
|
|
541
|
-
i18n?.formatDate?.(date, {
|
|
542
|
-
dateStyle: 'medium',
|
|
543
|
-
timeStyle: 'short'
|
|
544
|
-
}) ?? date.toLocaleString(),
|
|
545
|
-
});
|
|
546
|
-
```
|
|
547
|
-
|
|
548
|
-
---
|
|
549
|
-
|
|
550
|
-
## Complete Example
|
|
551
|
-
|
|
552
|
-
Here's a complete example showing all best practices:
|
|
553
|
-
|
|
554
|
-
```svelte
|
|
555
|
-
<script lang="ts">
|
|
556
|
-
import type { I18nProvider } from '@pie-qti/i18n';
|
|
557
|
-
|
|
558
|
-
interface Props {
|
|
559
|
-
items: Array<{ id: string; label: string }>;
|
|
560
|
-
selectedIds: string[];
|
|
561
|
-
disabled?: boolean;
|
|
562
|
-
i18n?: I18nProvider;
|
|
563
|
-
onChange: (ids: string[]) => void;
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
let {
|
|
567
|
-
items,
|
|
568
|
-
selectedIds,
|
|
569
|
-
disabled = false,
|
|
570
|
-
i18n,
|
|
571
|
-
onChange
|
|
572
|
-
}: Props = $props();
|
|
573
|
-
|
|
574
|
-
// Reactive translations
|
|
575
|
-
const translations = $derived({
|
|
576
|
-
title: i18n?.t('component.selectionList.title') ?? 'component.selectionList.title',
|
|
577
|
-
instruction: i18n?.t('component.selectionList.instruction') ?? 'component.selectionList.instruction',
|
|
578
|
-
clearAll: i18n?.t('common.clearAll') ?? 'common.clearAll',
|
|
579
|
-
|
|
580
|
-
// Functions for interpolation
|
|
581
|
-
selected: (count: number) =>
|
|
582
|
-
i18n?.t('component.selectionList.selected', { count }) ??
|
|
583
|
-
`component.selectionList.selected (${count})`,
|
|
584
|
-
|
|
585
|
-
itemAriaLabel: (label: string, isSelected: boolean) => {
|
|
586
|
-
const status = isSelected
|
|
587
|
-
? i18n?.t('common.selected') ?? 'common.selected'
|
|
588
|
-
: i18n?.t('common.available') ?? 'common.available';
|
|
589
|
-
return `${label}. ${status}`;
|
|
590
|
-
},
|
|
591
|
-
});
|
|
592
|
-
|
|
593
|
-
// Local state
|
|
594
|
-
let announceText = $state('');
|
|
595
|
-
|
|
596
|
-
// Handlers
|
|
597
|
-
function toggleSelection(id: string, label: string) {
|
|
598
|
-
if (disabled) return;
|
|
599
|
-
|
|
600
|
-
const isSelected = selectedIds.includes(id);
|
|
601
|
-
const newIds = isSelected
|
|
602
|
-
? selectedIds.filter(existingId => existingId !== id)
|
|
603
|
-
: [...selectedIds, id];
|
|
604
|
-
|
|
605
|
-
onChange(newIds);
|
|
606
|
-
|
|
607
|
-
// Announce to screen readers
|
|
608
|
-
const action = isSelected
|
|
609
|
-
? i18n?.t('common.deselected', { item: label }) ?? `common.deselected (${label})`
|
|
610
|
-
: i18n?.t('common.selected', { item: label }) ?? `common.selected (${label})`;
|
|
611
|
-
announceText = action;
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
function clearAll() {
|
|
615
|
-
if (disabled) return;
|
|
616
|
-
onChange([]);
|
|
617
|
-
announceText = i18n?.t('component.selectionList.allCleared') ?? 'component.selectionList.allCleared';
|
|
618
|
-
}
|
|
619
|
-
</script>
|
|
620
|
-
|
|
621
|
-
<!-- Screen reader announcements -->
|
|
622
|
-
<div aria-live="polite" aria-atomic="true" class="sr-only">
|
|
623
|
-
{announceText}
|
|
624
|
-
</div>
|
|
625
|
-
|
|
626
|
-
<div class="space-y-4">
|
|
627
|
-
<div class="flex justify-between items-center">
|
|
628
|
-
<h3 class="font-bold">{translations.title}</h3>
|
|
629
|
-
<button
|
|
630
|
-
class="btn btn-sm btn-outline"
|
|
631
|
-
onclick={clearAll}
|
|
632
|
-
disabled={disabled || selectedIds.length === 0}
|
|
633
|
-
>
|
|
634
|
-
{translations.clearAll}
|
|
635
|
-
</button>
|
|
636
|
-
</div>
|
|
637
|
-
|
|
638
|
-
<p class="text-sm text-base-content/70">
|
|
639
|
-
{translations.instruction}
|
|
640
|
-
</p>
|
|
641
|
-
|
|
642
|
-
<div class="text-sm font-semibold">
|
|
643
|
-
{translations.selected(selectedIds.length)}
|
|
644
|
-
</div>
|
|
645
|
-
|
|
646
|
-
<div class="space-y-2">
|
|
647
|
-
{#each items as item}
|
|
648
|
-
{@const isSelected = selectedIds.includes(item.id)}
|
|
649
|
-
<div class="form-control">
|
|
650
|
-
<label class="label cursor-pointer justify-start gap-4">
|
|
651
|
-
<input
|
|
652
|
-
type="checkbox"
|
|
653
|
-
class="checkbox checkbox-primary"
|
|
654
|
-
checked={isSelected}
|
|
655
|
-
onchange={() => toggleSelection(item.id, item.label)}
|
|
656
|
-
disabled={disabled}
|
|
657
|
-
aria-label={translations.itemAriaLabel(item.label, isSelected)}
|
|
658
|
-
/>
|
|
659
|
-
<span class="label-text">{item.label}</span>
|
|
660
|
-
</label>
|
|
661
|
-
</div>
|
|
662
|
-
{/each}
|
|
663
|
-
</div>
|
|
664
|
-
</div>
|
|
665
|
-
|
|
666
|
-
<style>
|
|
667
|
-
.sr-only {
|
|
668
|
-
position: absolute;
|
|
669
|
-
width: 1px;
|
|
670
|
-
height: 1px;
|
|
671
|
-
padding: 0;
|
|
672
|
-
margin: -1px;
|
|
673
|
-
overflow: hidden;
|
|
674
|
-
clip: rect(0, 0, 0, 0);
|
|
675
|
-
white-space: nowrap;
|
|
676
|
-
border-width: 0;
|
|
677
|
-
}
|
|
678
|
-
</style>
|
|
679
|
-
```
|
|
680
|
-
|
|
681
|
-
---
|
|
682
|
-
|
|
683
|
-
## Related Documentation
|
|
684
|
-
|
|
685
|
-
- [COMPONENT_PATTERNS.md](./COMPONENT_PATTERNS.md) - Svelte 5 + DaisyUI + i18n patterns
|
|
686
|
-
- [I18N_IMPLEMENTATION_PLAN.md](../../../I18N_IMPLEMENTATION_PLAN.md) - Complete implementation plan
|
|
687
|
-
- [I18N_PROGRESS_SUMMARY.md](../../../I18N_PROGRESS_SUMMARY.md) - Current progress
|
|
688
|
-
|
|
689
|
-
---
|
|
690
|
-
|
|
691
|
-
## Need Help?
|
|
692
|
-
|
|
693
|
-
- Check existing components for examples: [NavigationBar.svelte](../../assessment-player/src/components/NavigationBar.svelte)
|
|
694
|
-
- Review the [en-US locale file](../src/locales/en-US.ts) for available keys
|
|
695
|
-
- See [COMPONENT_PATTERNS.md](./COMPONENT_PATTERNS.md) for more patterns
|