@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.
Files changed (78) hide show
  1. package/.turbo/turbo-build.log +264 -0
  2. package/README.md +402 -0
  3. package/dist/core/I18n.d.ts +83 -0
  4. package/dist/core/I18n.d.ts.map +1 -0
  5. package/dist/core/I18n.js +203 -0
  6. package/dist/core/I18n.js.map +1 -0
  7. package/dist/core/I18nProvider.d.ts +64 -0
  8. package/dist/core/I18nProvider.d.ts.map +1 -0
  9. package/dist/core/I18nProvider.js +9 -0
  10. package/dist/core/I18nProvider.js.map +1 -0
  11. package/dist/core/types.d.ts +20 -0
  12. package/dist/core/types.d.ts.map +1 -0
  13. package/dist/core/types.js +5 -0
  14. package/dist/core/types.js.map +1 -0
  15. package/dist/index.d.ts +8 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +10 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/locales/ar-SA.d.ts +763 -0
  20. package/dist/locales/ar-SA.d.ts.map +1 -0
  21. package/dist/locales/ar-SA.js +796 -0
  22. package/dist/locales/ar-SA.js.map +1 -0
  23. package/dist/locales/en-US.d.ts +731 -0
  24. package/dist/locales/en-US.d.ts.map +1 -0
  25. package/dist/locales/en-US.js +764 -0
  26. package/dist/locales/en-US.js.map +1 -0
  27. package/dist/locales/es-ES.d.ts +734 -0
  28. package/dist/locales/es-ES.d.ts.map +1 -0
  29. package/dist/locales/es-ES.js +758 -0
  30. package/dist/locales/es-ES.js.map +1 -0
  31. package/dist/locales/fr-FR.d.ts +733 -0
  32. package/dist/locales/fr-FR.d.ts.map +1 -0
  33. package/dist/locales/fr-FR.js +757 -0
  34. package/dist/locales/fr-FR.js.map +1 -0
  35. package/dist/locales/nl-NL.d.ts +733 -0
  36. package/dist/locales/nl-NL.d.ts.map +1 -0
  37. package/dist/locales/nl-NL.js +757 -0
  38. package/dist/locales/nl-NL.js.map +1 -0
  39. package/dist/locales/ro-RO.d.ts +733 -0
  40. package/dist/locales/ro-RO.d.ts.map +1 -0
  41. package/dist/locales/ro-RO.js +757 -0
  42. package/dist/locales/ro-RO.js.map +1 -0
  43. package/dist/locales/th-TH.d.ts +733 -0
  44. package/dist/locales/th-TH.d.ts.map +1 -0
  45. package/dist/locales/th-TH.js +758 -0
  46. package/dist/locales/th-TH.js.map +1 -0
  47. package/dist/locales/zh-CN.d.ts +731 -0
  48. package/dist/locales/zh-CN.d.ts.map +1 -0
  49. package/dist/locales/zh-CN.js +764 -0
  50. package/dist/locales/zh-CN.js.map +1 -0
  51. package/dist/providers/SvelteI18nProvider.d.ts +40 -0
  52. package/dist/providers/SvelteI18nProvider.d.ts.map +1 -0
  53. package/dist/providers/SvelteI18nProvider.js +133 -0
  54. package/dist/providers/SvelteI18nProvider.js.map +1 -0
  55. package/docs/ADVANCED_FEATURES.md +556 -0
  56. package/docs/COMPONENT_PATTERNS.md +896 -0
  57. package/docs/INTEGRATION_GUIDE.md +695 -0
  58. package/package.json +42 -0
  59. package/scripts/check-translations.ts +289 -0
  60. package/scripts/scan-hardcoded-strings.ts +266 -0
  61. package/src/components/LocaleSwitcher.svelte +145 -0
  62. package/src/components/README.md +224 -0
  63. package/src/core/I18n.ts +235 -0
  64. package/src/core/I18nProvider.ts +68 -0
  65. package/src/core/types.ts +45 -0
  66. package/src/index.ts +22 -0
  67. package/src/locales/ar-SA.ts +860 -0
  68. package/src/locales/en-US.ts +828 -0
  69. package/src/locales/es-ES.ts +816 -0
  70. package/src/locales/fr-FR.ts +815 -0
  71. package/src/locales/nl-NL.ts +815 -0
  72. package/src/locales/ro-RO.ts +815 -0
  73. package/src/locales/th-TH.ts +816 -0
  74. package/src/locales/zh-CN.ts +828 -0
  75. package/src/providers/SvelteI18nProvider.ts +170 -0
  76. package/tests/i18n.test.ts +146 -0
  77. package/tsconfig.json +10 -0
  78. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,556 @@
1
+ # Advanced i18n Features
2
+
3
+ This guide covers advanced internationalization features including pluralization, number formatting, and date formatting.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Pluralization](#pluralization)
8
+ - [Number Formatting](#number-formatting)
9
+ - [Date and Time Formatting](#date-and-time-formatting)
10
+ - [Best Practices](#best-practices)
11
+
12
+ ## Pluralization
13
+
14
+ Pluralization handles different word forms based on count (e.g., "1 item" vs "2 items").
15
+
16
+ ### Basic Usage
17
+
18
+ The i18n provider includes an optional `plural()` method that selects the correct form based on count:
19
+
20
+ ```typescript
21
+ // In your component
22
+ const translations = $derived({
23
+ itemCount: (count: number) =>
24
+ i18n?.plural?.('plurals.items', { count }) ??
25
+ `${count} item(s)`,
26
+ });
27
+
28
+ // Usage in template
29
+ {translations.itemCount(1)} // "1 item"
30
+ {translations.itemCount(5)} // "5 items"
31
+ ```
32
+
33
+ ### How It Works
34
+
35
+ The `plural()` method uses a simple rule:
36
+ - If `count === 1`, uses `{key}.one`
37
+ - Otherwise, uses `{key}.other`
38
+
39
+ Example locale structure:
40
+
41
+ ```typescript
42
+ // en-US.ts
43
+ export default {
44
+ plurals: {
45
+ items: {
46
+ one: '{count} item',
47
+ other: '{count} items',
48
+ },
49
+ },
50
+ };
51
+ ```
52
+
53
+ ### Available Plural Forms
54
+
55
+ The framework includes plural forms for common scenarios:
56
+
57
+ | Key | Singular (count=1) | Plural (count≠1) |
58
+ |-----|-------------------|------------------|
59
+ | `plurals.items` | "1 item" | "N items" |
60
+ | `plurals.files` | "1 file selected" | "N files selected" |
61
+ | `plurals.questions` | "1 question" | "N questions" |
62
+ | `plurals.answers` | "1 answer" | "N answers" |
63
+ | `plurals.choices` | "1 choice" | "N choices" |
64
+ | `plurals.attempts` | "1 attempt remaining" | "N attempts remaining" |
65
+ | `plurals.minutesRemaining` | "1 minute remaining" | "N minutes remaining" |
66
+ | `plurals.secondsRemaining` | "1 second remaining" | "N seconds remaining" |
67
+
68
+ ### Complete Example
69
+
70
+ ```svelte
71
+ <script lang="ts">
72
+ import type { I18nProvider } from '@pie-qti/i18n';
73
+
74
+ interface Props {
75
+ selectedCount: number;
76
+ i18n?: I18nProvider;
77
+ }
78
+
79
+ let { selectedCount, i18n }: Props = $props();
80
+
81
+ const translations = $derived({
82
+ itemsSelected: (count: number) =>
83
+ i18n?.plural?.('plurals.items', { count }) ??
84
+ `${count} item(s)`,
85
+ });
86
+ </script>
87
+
88
+ <div class="selection-info">
89
+ <p>{translations.itemsSelected(selectedCount)}</p>
90
+ </div>
91
+ ```
92
+
93
+ ### Adding Custom Plural Forms
94
+
95
+ To add new plural forms to your locale:
96
+
97
+ ```typescript
98
+ // locales/en-US.ts
99
+ export default {
100
+ plurals: {
101
+ // Add your custom plural
102
+ participants: {
103
+ one: '{count} participant',
104
+ other: '{count} participants',
105
+ },
106
+ errors: {
107
+ one: '{count} error found',
108
+ other: '{count} errors found',
109
+ },
110
+ },
111
+ };
112
+ ```
113
+
114
+ Then translate to other locales:
115
+
116
+ ```typescript
117
+ // locales/es-ES.ts
118
+ export default {
119
+ plurals: {
120
+ participants: {
121
+ one: '{count} participante',
122
+ other: '{count} participantes',
123
+ },
124
+ errors: {
125
+ one: '{count} error encontrado',
126
+ other: '{count} errores encontrados',
127
+ },
128
+ },
129
+ };
130
+ ```
131
+
132
+ ### Pluralization with Additional Variables
133
+
134
+ You can pass additional interpolation values along with count:
135
+
136
+ ```typescript
137
+ const translations = $derived({
138
+ filesInFolder: (count: number, folder: string) =>
139
+ i18n?.plural?.('plurals.filesInFolder', { count, folder }) ??
140
+ `${count} file(s) in ${folder}`,
141
+ });
142
+
143
+ // In locale:
144
+ plurals: {
145
+ filesInFolder: {
146
+ one: '{count} file in {folder}',
147
+ other: '{count} files in {folder}',
148
+ },
149
+ }
150
+
151
+ // Usage:
152
+ {translations.filesInFolder(3, 'Documents')}
153
+ // "3 files in Documents"
154
+ ```
155
+
156
+ ### Fallback Behavior
157
+
158
+ If `i18n?.plural` is not available (e.g., custom provider without plural support), provide a fallback:
159
+
160
+ ```typescript
161
+ const translations = $derived({
162
+ items: (count: number) =>
163
+ i18n?.plural?.('plurals.items', { count }) ??
164
+ i18n?.t('plurals.items.other', { count }) ??
165
+ `${count} item(s)`,
166
+ });
167
+ ```
168
+
169
+ ### Languages Without Plurals
170
+
171
+ Some languages (like Thai, Chinese, Japanese) don't have plural forms. For these, use the same text for both:
172
+
173
+ ```typescript
174
+ // th-TH.ts (Thai)
175
+ plurals: {
176
+ items: {
177
+ one: '{count} รายการ', // Same for both
178
+ other: '{count} รายการ',
179
+ },
180
+ }
181
+ ```
182
+
183
+ ### Complex Pluralization
184
+
185
+ For languages with complex plural rules (Russian, Arabic, Polish), you may need to use a more sophisticated i18n library like [Intl.PluralRules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules):
186
+
187
+ ```typescript
188
+ // Custom provider with complex pluralization
189
+ class ComplexI18nProvider implements I18nProvider {
190
+ plural(key: string, options: { count: number; [key: string]: any }): string {
191
+ const pluralRules = new Intl.PluralRules(this.locale);
192
+ const rule = pluralRules.select(options.count); // 'zero', 'one', 'two', 'few', 'many', 'other'
193
+
194
+ return this.t(`${key}.${rule}`, options);
195
+ }
196
+ }
197
+ ```
198
+
199
+ ## Number Formatting
200
+
201
+ Format numbers according to locale conventions (decimal separators, thousands grouping, currency symbols).
202
+
203
+ ### Basic Usage
204
+
205
+ ```typescript
206
+ const translations = $derived({
207
+ price: (amount: number) =>
208
+ i18n?.formatNumber?.(amount, { style: 'currency', currency: 'USD' }) ??
209
+ `$${amount}`,
210
+
211
+ percentage: (value: number) =>
212
+ i18n?.formatNumber?.(value, { style: 'percent' }) ??
213
+ `${value}%`,
214
+
215
+ fileSize: (bytes: number) =>
216
+ i18n?.formatNumber?.(bytes, { maximumFractionDigits: 2 }) ??
217
+ String(bytes),
218
+ });
219
+
220
+ // Usage:
221
+ {translations.price(1299.99)} // "$1,299.99" (en-US)
222
+ // "1 299,99 $" (fr-FR)
223
+
224
+ {translations.percentage(0.856)} // "85.6%" (en-US)
225
+ // "85,6 %" (fr-FR)
226
+
227
+ {translations.fileSize(1234567)} // "1,234,567" (en-US)
228
+ // "1 234 567" (fr-FR)
229
+ ```
230
+
231
+ ### Number Format Options
232
+
233
+ All [Intl.NumberFormat options](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat) are supported:
234
+
235
+ ```typescript
236
+ // Currency formatting
237
+ i18n?.formatNumber?.(99.95, {
238
+ style: 'currency',
239
+ currency: 'EUR',
240
+ currencyDisplay: 'symbol', // or 'code', 'name'
241
+ });
242
+
243
+ // Percentage formatting
244
+ i18n?.formatNumber?.(0.5, {
245
+ style: 'percent',
246
+ minimumFractionDigits: 2,
247
+ });
248
+
249
+ // Decimal formatting
250
+ i18n?.formatNumber?.(1234.5678, {
251
+ minimumFractionDigits: 2,
252
+ maximumFractionDigits: 2,
253
+ });
254
+
255
+ // Compact notation
256
+ i18n?.formatNumber?.(1500000, {
257
+ notation: 'compact',
258
+ compactDisplay: 'short', // or 'long'
259
+ });
260
+ // "1.5M" (en-US), "1,5 M" (fr-FR)
261
+ ```
262
+
263
+ ### Complete Example
264
+
265
+ ```svelte
266
+ <script lang="ts">
267
+ import type { I18nProvider } from '@pie-qti/i18n';
268
+
269
+ interface Props {
270
+ score: number;
271
+ maxScore: number;
272
+ i18n?: I18nProvider;
273
+ }
274
+
275
+ let { score, maxScore, i18n }: Props = $props();
276
+
277
+ const percentage = $derived((score / maxScore) * 100);
278
+
279
+ const translations = $derived({
280
+ score: i18n?.formatNumber?.(score) ?? String(score),
281
+ maxScore: i18n?.formatNumber?.(maxScore) ?? String(maxScore),
282
+ percentage: i18n?.formatNumber?.(percentage / 100, {
283
+ style: 'percent',
284
+ maximumFractionDigits: 1,
285
+ }) ?? `${percentage}%`,
286
+ });
287
+ </script>
288
+
289
+ <div class="score-display">
290
+ <p>Score: {translations.score} / {translations.maxScore}</p>
291
+ <p>Percentage: {translations.percentage}</p>
292
+ </div>
293
+ ```
294
+
295
+ ## Date and Time Formatting
296
+
297
+ Format dates and times according to locale conventions.
298
+
299
+ ### Basic Usage
300
+
301
+ ```typescript
302
+ const now = new Date();
303
+
304
+ const translations = $derived({
305
+ date: i18n?.formatDate?.(now, { dateStyle: 'medium' }) ??
306
+ now.toLocaleDateString(),
307
+
308
+ time: i18n?.formatDate?.(now, { timeStyle: 'short' }) ??
309
+ now.toLocaleTimeString(),
310
+
311
+ dateTime: i18n?.formatDate?.(now, {
312
+ dateStyle: 'short',
313
+ timeStyle: 'short',
314
+ }) ?? now.toLocaleString(),
315
+ });
316
+
317
+ // Usage:
318
+ {translations.date} // "Jan 10, 2026" (en-US)
319
+ // "10 janv. 2026" (fr-FR)
320
+
321
+ {translations.time} // "3:30 PM" (en-US)
322
+ // "15:30" (fr-FR)
323
+
324
+ {translations.dateTime} // "1/10/26, 3:30 PM" (en-US)
325
+ // "10/01/2026 15:30" (fr-FR)
326
+ ```
327
+
328
+ ### Date Format Options
329
+
330
+ All [Intl.DateTimeFormat options](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat) are supported:
331
+
332
+ ```typescript
333
+ // Short, medium, long, full date styles
334
+ i18n?.formatDate?.(date, { dateStyle: 'full' });
335
+ // "Friday, January 10, 2026" (en-US)
336
+ // "vendredi 10 janvier 2026" (fr-FR)
337
+
338
+ // Custom format
339
+ i18n?.formatDate?.(date, {
340
+ year: 'numeric',
341
+ month: 'long',
342
+ day: 'numeric',
343
+ weekday: 'long',
344
+ });
345
+
346
+ // Time with seconds
347
+ i18n?.formatDate?.(date, {
348
+ hour: '2-digit',
349
+ minute: '2-digit',
350
+ second: '2-digit',
351
+ });
352
+
353
+ // 24-hour vs 12-hour format
354
+ i18n?.formatDate?.(date, {
355
+ hour: '2-digit',
356
+ minute: '2-digit',
357
+ hour12: false, // Use 24-hour format
358
+ });
359
+ ```
360
+
361
+ ### Complete Example
362
+
363
+ ```svelte
364
+ <script lang="ts">
365
+ import type { I18nProvider } from '@pie-qti/i18n';
366
+
367
+ interface Props {
368
+ deadline: Date;
369
+ i18n?: I18nProvider;
370
+ }
371
+
372
+ let { deadline, i18n }: Props = $props();
373
+
374
+ const now = new Date();
375
+ const timeRemaining = $derived(deadline.getTime() - now.getTime());
376
+ const minutesRemaining = $derived(Math.floor(timeRemaining / 60000));
377
+
378
+ const translations = $derived({
379
+ deadlineDate: i18n?.formatDate?.(deadline, {
380
+ dateStyle: 'medium',
381
+ timeStyle: 'short',
382
+ }) ?? deadline.toLocaleString(),
383
+
384
+ minutesLeft: (count: number) =>
385
+ i18n?.plural?.('plurals.minutesRemaining', { count }) ??
386
+ `${count} minute(s) remaining`,
387
+ });
388
+ </script>
389
+
390
+ <div class="deadline-info">
391
+ <p>Deadline: {translations.deadlineDate}</p>
392
+ <p class="time-remaining">
393
+ {translations.minutesLeft(minutesRemaining)}
394
+ </p>
395
+ </div>
396
+ ```
397
+
398
+ ### Relative Time Formatting
399
+
400
+ For "time ago" or "time until" formatting, use [Intl.RelativeTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat):
401
+
402
+ ```typescript
403
+ // Custom method in your i18n provider
404
+ class ExtendedI18nProvider extends DefaultI18nProvider {
405
+ formatRelativeTime(
406
+ value: number,
407
+ unit: Intl.RelativeTimeFormatUnit,
408
+ options?: Intl.RelativeTimeFormatOptions
409
+ ): string {
410
+ const rtf = new Intl.RelativeTimeFormat(this.currentLocale, options);
411
+ return rtf.format(value, unit);
412
+ }
413
+ }
414
+
415
+ // Usage:
416
+ i18n.formatRelativeTime(-1, 'day'); // "1 day ago" (en-US)
417
+ i18n.formatRelativeTime(2, 'hour'); // "in 2 hours" (en-US)
418
+ i18n.formatRelativeTime(-3, 'month'); // "3 months ago" (en-US)
419
+ ```
420
+
421
+ ## Best Practices
422
+
423
+ ### 1. Always Provide Fallbacks
424
+
425
+ ```typescript
426
+ // ✅ Good - has fallback
427
+ const text = i18n?.plural?.('plurals.items', { count }) ??
428
+ `${count} item(s)`;
429
+
430
+ // ❌ Bad - crashes if plural not available
431
+ const text = i18n.plural('plurals.items', { count });
432
+ ```
433
+
434
+ ### 2. Use $derived for Reactive Formatting
435
+
436
+ ```typescript
437
+ // ✅ Good - reactive
438
+ const formattedDate = $derived(
439
+ i18n?.formatDate?.(deadline) ?? deadline.toLocaleDateString()
440
+ );
441
+
442
+ // ❌ Bad - not reactive, won't update when locale changes
443
+ let formattedDate = i18n?.formatDate?.(deadline);
444
+ ```
445
+
446
+ ### 3. Cache Expensive Formatters
447
+
448
+ For frequently updated values, consider caching formatters:
449
+
450
+ ```typescript
451
+ const numberFormatter = $derived(
452
+ i18n ? new Intl.NumberFormat(i18n.getLocale(), {
453
+ style: 'currency',
454
+ currency: 'USD',
455
+ }) : null
456
+ );
457
+
458
+ const formattedPrice = $derived(
459
+ numberFormatter?.format(price) ?? `$${price}`
460
+ );
461
+ ```
462
+
463
+ ### 4. Use Appropriate Precision
464
+
465
+ ```typescript
466
+ // ✅ Good - appropriate precision for percentages
467
+ i18n?.formatNumber?.(0.856, {
468
+ style: 'percent',
469
+ maximumFractionDigits: 1,
470
+ });
471
+ // "85.6%"
472
+
473
+ // ❌ Bad - too much precision
474
+ i18n?.formatNumber?.(0.856, { style: 'percent' });
475
+ // "85.6000000000000014%"
476
+ ```
477
+
478
+ ### 5. Consider Timezone for Dates
479
+
480
+ ```typescript
481
+ // Display in user's timezone
482
+ i18n?.formatDate?.(date, {
483
+ dateStyle: 'medium',
484
+ timeStyle: 'short',
485
+ timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
486
+ });
487
+
488
+ // Or specify explicit timezone
489
+ i18n?.formatDate?.(date, {
490
+ dateStyle: 'medium',
491
+ timeStyle: 'short',
492
+ timeZone: 'America/New_York',
493
+ });
494
+ ```
495
+
496
+ ### 6. Test with Multiple Locales
497
+
498
+ Always test formatting with different locales to catch issues:
499
+
500
+ ```typescript
501
+ // Test with various locales
502
+ const testLocales = ['en-US', 'fr-FR', 'de-DE', 'ja-JP', 'ar-SA'];
503
+
504
+ testLocales.forEach(locale => {
505
+ const i18n = createSvelteI18nProvider(locale);
506
+ console.log(locale, i18n.formatNumber(1234.56, {
507
+ style: 'currency',
508
+ currency: 'USD',
509
+ }));
510
+ });
511
+
512
+ // Output:
513
+ // en-US: $1,234.56
514
+ // fr-FR: 1 234,56 $US
515
+ // de-DE: 1.234,56 $
516
+ // ja-JP: $1,234.56
517
+ // ar-SA: US$ ١٬٢٣٤٫٥٦
518
+ ```
519
+
520
+ ### 7. Document Custom Plural Forms
521
+
522
+ When adding custom plurals, document them clearly:
523
+
524
+ ```typescript
525
+ // locales/en-US.ts
526
+
527
+ /**
528
+ * Custom plural forms for the application
529
+ *
530
+ * Usage: i18n.plural('plurals.participants', { count: n })
531
+ */
532
+ export default {
533
+ plurals: {
534
+ // Assessment-specific plurals
535
+ participants: {
536
+ one: '{count} participant',
537
+ other: '{count} participants',
538
+ },
539
+
540
+ // Quiz-specific plurals
541
+ quizzes: {
542
+ one: '{count} quiz',
543
+ other: '{count} quizzes',
544
+ },
545
+ },
546
+ };
547
+ ```
548
+
549
+ ## See Also
550
+
551
+ - [INTEGRATION_GUIDE.md](./INTEGRATION_GUIDE.md) - Basic i18n integration
552
+ - [COMPONENT_PATTERNS.md](./COMPONENT_PATTERNS.md) - UI component patterns
553
+ - [MIGRATION.md](./MIGRATION.md) - Migrating existing components
554
+ - [MDN: Intl.NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat)
555
+ - [MDN: Intl.DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat)
556
+ - [MDN: Intl.PluralRules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules)