@gears-frontx/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.
@@ -0,0 +1,328 @@
1
+ import { I18nRegistry, Language, I18nConfig, TranslationDictionary, TranslationLoader, TranslationParams, LanguageMetadata, TranslationMap } from './types.js';
2
+ export { HTML_DIR_ATTRIBUTE, HTML_LANG_ATTRIBUTE, I18N_DEFAULT_NAMESPACE, I18N_NAMESPACE_SEPARATOR, I18N_PATH_SEPARATOR, LanguageCode, LanguageDisplayMode, TextDirection } from './types.js';
3
+
4
+ /**
5
+ * I18n Registry - Central translation management
6
+ *
7
+ * Implements namespace-based translation loading with:
8
+ * - Lazy loading for screenset/screen translations
9
+ * - RTL/LTR support
10
+ * - Parameter interpolation
11
+ *
12
+ * SDK Layer: L1 (Zero @gears-frontx dependencies)
13
+ */
14
+
15
+ /**
16
+ * I18n Registry Implementation
17
+ *
18
+ * Central registry for managing translations across the application.
19
+ * Supports lazy loading, namespace isolation, and RTL languages.
20
+ */
21
+ declare class I18nRegistryImpl implements I18nRegistry {
22
+ /** Configuration */
23
+ private config;
24
+ /** Current language */
25
+ private currentLanguage;
26
+ /** Translation dictionaries: namespace -> language -> dictionary */
27
+ private dictionaries;
28
+ /** Translation loaders: namespace -> loader function */
29
+ private loaders;
30
+ /** Subscribers for translation changes */
31
+ private subscribers;
32
+ /** Version counter for React re-rendering */
33
+ private version;
34
+ /** Language file mapping */
35
+ static readonly LANGUAGE_FILE_MAP: Record<Language, string>;
36
+ constructor(config: I18nConfig);
37
+ /**
38
+ * Register translations for a namespace.
39
+ */
40
+ register(namespace: string, language: Language, translations: TranslationDictionary): void;
41
+ /**
42
+ * Subscribe to translation changes.
43
+ * Returns an unsubscribe function.
44
+ */
45
+ subscribe(callback: () => void): () => void;
46
+ /**
47
+ * Notify all subscribers of a change.
48
+ */
49
+ private notifySubscribers;
50
+ /**
51
+ * Get the current version number.
52
+ * Used by React to detect changes.
53
+ */
54
+ getVersion(): number;
55
+ /**
56
+ * Register a translation loader for a namespace.
57
+ */
58
+ registerLoader(namespace: string, loader: TranslationLoader): void;
59
+ /**
60
+ * Check if namespace is registered.
61
+ */
62
+ hasNamespace(namespace: string): boolean;
63
+ /**
64
+ * Get all registered namespaces.
65
+ */
66
+ getNamespaces(): string[];
67
+ /**
68
+ * Translate a key.
69
+ * Format: 'namespace:key.subkey' or just 'key' for default namespace
70
+ */
71
+ t(key: string, params?: TranslationParams): string;
72
+ /**
73
+ * Parse a translation key into namespace and path.
74
+ */
75
+ private parseKey;
76
+ /**
77
+ * Get a translation from the dictionary.
78
+ */
79
+ private getTranslation;
80
+ /**
81
+ * Find a translation in the dictionary.
82
+ */
83
+ private findTranslation;
84
+ /**
85
+ * Interpolate parameters into a translation string.
86
+ */
87
+ private interpolate;
88
+ /**
89
+ * Set current language and load translations.
90
+ */
91
+ setLanguage(language: Language): Promise<void>;
92
+ /**
93
+ * Get current language.
94
+ */
95
+ getLanguage(): Language | null;
96
+ /**
97
+ * Update HTML lang and dir attributes.
98
+ */
99
+ private updateHtmlAttributes;
100
+ /**
101
+ * Get language metadata.
102
+ */
103
+ getLanguageMetadata(code?: Language): LanguageMetadata | undefined;
104
+ /**
105
+ * Get all supported languages.
106
+ */
107
+ getSupportedLanguages(): LanguageMetadata[];
108
+ /**
109
+ * Check if a language is RTL.
110
+ */
111
+ isRTL(code?: Language): boolean;
112
+ /**
113
+ * Load translations for a language.
114
+ * Excludes screen.* and screenset.* namespaces which are lazy-loaded:
115
+ * - screen.* namespaces: loaded when screen mounts
116
+ * - screenset.* namespaces: loaded when screenset is activated
117
+ */
118
+ loadLanguage(language: Language): Promise<void>;
119
+ /**
120
+ * Load translations for a specific namespace.
121
+ */
122
+ private loadNamespace;
123
+ /**
124
+ * Load translations for a specific screenset.
125
+ */
126
+ loadScreensetTranslations(screensetId: string, language?: Language): Promise<void>;
127
+ /**
128
+ * Preload translations for multiple languages.
129
+ */
130
+ preloadLanguages(languages: Language[]): Promise<void>;
131
+ /**
132
+ * Create a translation loader from a translation map.
133
+ *
134
+ * @example
135
+ * ```typescript
136
+ * const loader = I18nRegistry.createLoader({
137
+ * [Language.English]: () => import('./i18n/en.json'),
138
+ * [Language.Spanish]: () => import('./i18n/es.json'),
139
+ * });
140
+ * ```
141
+ */
142
+ static createLoader(translationMap: TranslationMap): TranslationLoader;
143
+ /**
144
+ * Create a translation loader from a directory.
145
+ * Maps Language enum values to file names automatically.
146
+ *
147
+ * @param basePath - Base path for translation files
148
+ * @param importFn - Dynamic import function
149
+ */
150
+ static createLoaderFromDirectory(importFn: (filename: string) => Promise<{
151
+ default: TranslationDictionary;
152
+ }>): TranslationLoader;
153
+ }
154
+ /**
155
+ * Default i18n registry instance.
156
+ * Use this instance throughout the application.
157
+ */
158
+ declare const i18nRegistry: I18nRegistryImpl;
159
+ /**
160
+ * Create a new i18n registry with custom configuration.
161
+ *
162
+ * @param config - I18n configuration
163
+ * @returns New I18nRegistry instance
164
+ */
165
+ declare function createI18nRegistry(config: I18nConfig): I18nRegistryImpl;
166
+
167
+ /**
168
+ * Supported Languages - All 36 HAI3 supported languages
169
+ *
170
+ * Based on major platforms (Google, Facebook, Microsoft, Apple).
171
+ * Includes native names, English names, and text direction.
172
+ */
173
+
174
+ /**
175
+ * All supported languages with metadata.
176
+ * This list defines what languages the application can use.
177
+ */
178
+ declare const SUPPORTED_LANGUAGES: LanguageMetadata[];
179
+ /**
180
+ * Get language metadata by code.
181
+ *
182
+ * @param code - Language code
183
+ * @returns Language metadata or undefined
184
+ */
185
+ declare function getLanguageMetadata(code: Language): LanguageMetadata | undefined;
186
+ /**
187
+ * Get RTL language codes.
188
+ *
189
+ * @returns Array of RTL language codes
190
+ */
191
+ declare function getRTLLanguages(): Language[];
192
+ /**
193
+ * Check if a language code is valid.
194
+ *
195
+ * @param code - Language code to check
196
+ * @returns True if valid
197
+ */
198
+ declare function isValidLanguage(code: string): code is Language;
199
+
200
+ /**
201
+ * Date formatter - Locale-aware date and time formatting
202
+ *
203
+ * Uses i18nRegistry.getLanguage() for locale; fallback to Language.English when null.
204
+ * Invalid date inputs return ''.
205
+ */
206
+ type DateFormatStyle = 'short' | 'medium' | 'long' | 'full';
207
+ type TimeFormatStyle = 'short' | 'medium';
208
+ type DateInput = Date | number | string | null | undefined;
209
+ /**
210
+ * Format a date according to the current locale.
211
+ *
212
+ * @param date - Date, timestamp, or ISO string
213
+ * @param format - 'short' | 'medium' | 'long' | 'full'
214
+ * @returns Formatted date string, or '' if input is invalid
215
+ */
216
+ declare function formatDate(date: DateInput, format: DateFormatStyle): string;
217
+ /**
218
+ * Format the time portion of a date according to the current locale.
219
+ *
220
+ * @param date - Date, timestamp, or ISO string
221
+ * @param format - 'short' | 'medium'
222
+ * @returns Formatted time string, or '' if input is invalid
223
+ */
224
+ declare function formatTime(date: DateInput, format: TimeFormatStyle): string;
225
+ /**
226
+ * Format date and time according to the current locale.
227
+ *
228
+ * @param date - Date, timestamp, or ISO string
229
+ * @param dateFormat - Date style: 'short' | 'medium' | 'long' | 'full'
230
+ * @param timeFormat - Time style: 'short' | 'medium'
231
+ * @returns Formatted date-time string, or '' if input is invalid
232
+ */
233
+ declare function formatDateTime(date: DateInput, dateFormat: DateFormatStyle, timeFormat: TimeFormatStyle): string;
234
+ /**
235
+ * Format a date as relative time (e.g. "2 hours ago", "in 3 days").
236
+ *
237
+ * @param date - Date, timestamp, or ISO string
238
+ * @param base - Reference date for "now"; defaults to new Date()
239
+ * @returns Relative time string, or '' if input is invalid
240
+ */
241
+ declare function formatRelative(date: DateInput, base?: DateInput): string;
242
+
243
+ /**
244
+ * Number formatter - Locale-aware number formatting
245
+ *
246
+ * Uses i18nRegistry.getLanguage() for locale; fallback to Language.English when null.
247
+ * null, undefined, and NaN return ''.
248
+ */
249
+ /**
250
+ * Format a number according to the current locale.
251
+ *
252
+ * @param value - Number to format
253
+ * @param options - Intl.NumberFormatOptions (e.g. minimumFractionDigits)
254
+ * @returns Formatted number string, or '' if input is null, undefined, or NaN
255
+ */
256
+ declare function formatNumber(value: number | null | undefined, options?: Intl.NumberFormatOptions): string;
257
+ /**
258
+ * Format a value as a percentage (0.15 -> "15%").
259
+ *
260
+ * @param value - Decimal value (e.g. 0.15 for 15%)
261
+ * @param decimals - Optional decimal places
262
+ * @returns Formatted percent string, or '' if input is null, undefined, or NaN
263
+ */
264
+ declare function formatPercent(value: number | null | undefined, decimals?: number): string;
265
+ /**
266
+ * Format a number in compact notation (e.g. "1.2K", "3.4M").
267
+ *
268
+ * @param value - Number to format
269
+ * @returns Compact formatted string, or '' if input is null, undefined, or NaN
270
+ */
271
+ declare function formatCompact(value: number | null | undefined): string;
272
+
273
+ /**
274
+ * Currency formatter - Locale-aware currency formatting
275
+ *
276
+ * Uses i18nRegistry.getLanguage() for locale; fallback to Language.English when null.
277
+ * null, undefined, and NaN return ''; invalid currencyCode also returns '' and does not throw.
278
+ */
279
+ /**
280
+ * Format a value as currency for the given currency code.
281
+ *
282
+ * @param value - Numeric amount
283
+ * @param currencyCode - ISO 4217 currency code (e.g. 'USD', 'EUR')
284
+ * @returns Formatted currency string, or '' if value is null, undefined, NaN, or currencyCode is invalid
285
+ */
286
+ declare function formatCurrency(value: number | null | undefined, currencyCode: string): string;
287
+
288
+ /**
289
+ * Sort utilities - Locale-aware string comparison
290
+ *
291
+ * Uses i18nRegistry.getLanguage() for locale; fallback to Language.English when null.
292
+ */
293
+ /**
294
+ * Compare two strings according to the current locale (for sorting).
295
+ *
296
+ * @param a - First string
297
+ * @param b - Second string
298
+ * @param options - Intl.Collator options (e.g. sensitivity, numeric)
299
+ * @returns Negative if a < b, 0 if equal, positive if a > b
300
+ */
301
+ declare function compareStrings(a: string, b: string, options?: Intl.CollatorOptions): number;
302
+ /**
303
+ * Create a Collator for the current locale (reuse for many comparisons, e.g. in tables).
304
+ *
305
+ * @param options - Intl.Collator options
306
+ * @returns Intl.Collator instance
307
+ */
308
+ declare function createCollator(options?: Intl.CollatorOptions): Intl.Collator;
309
+
310
+ /**
311
+ * Formatters type - Canonical type for the locale-aware formatters object.
312
+ * Use this (e.g. UseFormattersReturn) instead of re-declaring the 10 function signatures.
313
+ */
314
+
315
+ interface Formatters {
316
+ formatDate: typeof formatDate;
317
+ formatTime: typeof formatTime;
318
+ formatDateTime: typeof formatDateTime;
319
+ formatRelative: typeof formatRelative;
320
+ formatNumber: typeof formatNumber;
321
+ formatPercent: typeof formatPercent;
322
+ formatCompact: typeof formatCompact;
323
+ formatCurrency: typeof formatCurrency;
324
+ compareStrings: typeof compareStrings;
325
+ createCollator: typeof createCollator;
326
+ }
327
+
328
+ export { type DateFormatStyle, type DateInput, type Formatters, I18nConfig, I18nRegistry, I18nRegistryImpl, Language, LanguageMetadata, SUPPORTED_LANGUAGES, type TimeFormatStyle, TranslationDictionary, TranslationLoader, TranslationMap, TranslationParams, compareStrings, createCollator, createI18nRegistry, formatCompact, formatCurrency, formatDate, formatDateTime, formatNumber, formatPercent, formatRelative, formatTime, getLanguageMetadata, getRTLLanguages, i18nRegistry, isValidLanguage };