@pie-qti/i18n 0.1.0 → 0.1.5

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 (63) hide show
  1. package/dist/core/I18n.d.ts +3 -2
  2. package/dist/core/I18n.d.ts.map +1 -1
  3. package/dist/core/I18n.js +11 -6
  4. package/dist/core/I18n.js.map +1 -1
  5. package/dist/locales/ar-SA.d.ts +43 -0
  6. package/dist/locales/ar-SA.d.ts.map +1 -1
  7. package/dist/locales/ar-SA.js +44 -0
  8. package/dist/locales/ar-SA.js.map +1 -1
  9. package/dist/locales/en-US.d.ts +43 -0
  10. package/dist/locales/en-US.d.ts.map +1 -1
  11. package/dist/locales/en-US.js +44 -0
  12. package/dist/locales/en-US.js.map +1 -1
  13. package/dist/locales/es-ES.d.ts +43 -0
  14. package/dist/locales/es-ES.d.ts.map +1 -1
  15. package/dist/locales/es-ES.js +44 -0
  16. package/dist/locales/es-ES.js.map +1 -1
  17. package/dist/locales/fr-FR.d.ts +43 -0
  18. package/dist/locales/fr-FR.d.ts.map +1 -1
  19. package/dist/locales/fr-FR.js +44 -0
  20. package/dist/locales/fr-FR.js.map +1 -1
  21. package/dist/locales/nl-NL.d.ts +43 -0
  22. package/dist/locales/nl-NL.d.ts.map +1 -1
  23. package/dist/locales/nl-NL.js +44 -0
  24. package/dist/locales/nl-NL.js.map +1 -1
  25. package/dist/locales/ro-RO.d.ts +43 -0
  26. package/dist/locales/ro-RO.d.ts.map +1 -1
  27. package/dist/locales/ro-RO.js +44 -0
  28. package/dist/locales/ro-RO.js.map +1 -1
  29. package/dist/locales/th-TH.d.ts +43 -0
  30. package/dist/locales/th-TH.d.ts.map +1 -1
  31. package/dist/locales/th-TH.js +44 -0
  32. package/dist/locales/th-TH.js.map +1 -1
  33. package/dist/locales/zh-CN.d.ts +43 -0
  34. package/dist/locales/zh-CN.d.ts.map +1 -1
  35. package/dist/locales/zh-CN.js +44 -0
  36. package/dist/locales/zh-CN.js.map +1 -1
  37. package/dist/providers/SvelteI18nProvider.d.ts +5 -14
  38. package/dist/providers/SvelteI18nProvider.d.ts.map +1 -1
  39. package/dist/providers/SvelteI18nProvider.js +7 -47
  40. package/dist/providers/SvelteI18nProvider.js.map +1 -1
  41. package/package.json +29 -7
  42. package/.turbo/turbo-build.log +0 -264
  43. package/docs/ADVANCED_FEATURES.md +0 -556
  44. package/docs/COMPONENT_PATTERNS.md +0 -896
  45. package/docs/INTEGRATION_GUIDE.md +0 -695
  46. package/scripts/check-translations.ts +0 -289
  47. package/scripts/scan-hardcoded-strings.ts +0 -266
  48. package/src/core/I18n.ts +0 -235
  49. package/src/core/I18nProvider.ts +0 -68
  50. package/src/core/types.ts +0 -45
  51. package/src/index.ts +0 -22
  52. package/src/locales/ar-SA.ts +0 -860
  53. package/src/locales/en-US.ts +0 -828
  54. package/src/locales/es-ES.ts +0 -816
  55. package/src/locales/fr-FR.ts +0 -815
  56. package/src/locales/nl-NL.ts +0 -815
  57. package/src/locales/ro-RO.ts +0 -815
  58. package/src/locales/th-TH.ts +0 -816
  59. package/src/locales/zh-CN.ts +0 -828
  60. package/src/providers/SvelteI18nProvider.ts +0 -170
  61. package/tests/i18n.test.ts +0 -146
  62. package/tsconfig.json +0 -10
  63. package/tsconfig.tsbuildinfo +0 -1
package/src/core/I18n.ts DELETED
@@ -1,235 +0,0 @@
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 };
@@ -1,68 +0,0 @@
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
- }
package/src/core/types.ts DELETED
@@ -1,45 +0,0 @@
1
- /**
2
- * Type definitions for i18n system
3
- */
4
-
5
- import type enUS from '../locales/en-US.js';
6
-
7
- // Framework-provided locale codes (BCP 47 format)
8
- // The framework provides a minimal set of locales. Clients can provide their own
9
- // translations for additional locales via the customMessages parameter.
10
- export type FrameworkLocaleCode =
11
- | 'en-US' // English (United States)
12
- | 'es-ES' // Spanish (Spain)
13
- | 'fr-FR' // French (France)
14
- | 'nl-NL' // Dutch (Netherlands)
15
- | 'ro-RO' // Romanian (Romania)
16
- | 'th-TH' // Thai (Thailand)
17
- | 'zh-CN' // Chinese (Simplified, China)
18
- | 'ar-SA'; // Arabic (Saudi Arabia)
19
-
20
- // Allow any locale code (clients can provide custom locales)
21
- export type LocaleCode = FrameworkLocaleCode | (string & {});
22
-
23
- // Utility type to extract nested keys as dot-notation strings
24
- type NestedKeyOfHelper<ObjectType extends object> = {
25
- [Key in keyof ObjectType & (string | number)]: ObjectType[Key] extends object
26
- ? `${Key}` | `${Key}.${NestedKeyOfHelper<ObjectType[Key]>}`
27
- : `${Key}`;
28
- }[keyof ObjectType & (string | number)];
29
-
30
- // Message key generated from English locale structure
31
- export type MessageKey = NestedKeyOfHelper<typeof enUS>;
32
-
33
- // Interpolation values for dynamic message content
34
- export interface InterpolationValues {
35
- [key: string]: string | number;
36
- }
37
-
38
- // Pluralization options
39
- export interface PluralOptions {
40
- count: number;
41
- [key: string]: string | number;
42
- }
43
-
44
- // Locale message structure
45
- export type LocaleMessages = Record<string, any>;
package/src/index.ts DELETED
@@ -1,22 +0,0 @@
1
- /**
2
- * Public API exports for @pie-qti/i18n package
3
- */
4
-
5
- // Core interface (framework-agnostic)
6
- export type { I18nProvider, InterpolationValues, PluralOptions } from './core/I18nProvider.js';
7
-
8
- // Default implementation
9
- export { DefaultI18nProvider, createDefaultI18nProvider } from './core/I18n.js';
10
-
11
- // Svelte-specific provider with reactive stores
12
- export { SvelteI18nProvider, createSvelteI18nProvider, createDefaultSvelteI18nProvider } from './providers/SvelteI18nProvider.js';
13
-
14
- // Types
15
- export type {
16
- LocaleCode,
17
- MessageKey,
18
- LocaleMessages,
19
- } from './core/types.js';
20
-
21
- // Example components (commented out - Svelte files need special build process)
22
- // export { default as LocaleSwitcher } from './components/LocaleSwitcher.svelte';