@digitaldefiance/i18n-lib 1.2.0 → 1.2.1
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/README.md +59 -31
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -217,23 +217,44 @@ const saveText = i18n.translate('core', CoreStringKey.Common_Save);
|
|
|
217
217
|
const errorMsg = i18n.translate('core', CoreStringKey.Error_ValidationFailed);
|
|
218
218
|
```
|
|
219
219
|
|
|
220
|
-
####
|
|
220
|
+
#### Custom Component Example
|
|
221
221
|
|
|
222
|
-
|
|
222
|
+
Create your own component with translations:
|
|
223
223
|
|
|
224
224
|
```typescript
|
|
225
225
|
import {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
226
|
+
ComponentDefinition,
|
|
227
|
+
ComponentRegistration,
|
|
228
|
+
LanguageCodes
|
|
229
229
|
} from '@digitaldefiance/i18n-lib';
|
|
230
230
|
|
|
231
|
-
|
|
232
|
-
|
|
231
|
+
enum UserStringKey {
|
|
232
|
+
Auth_Login = 'auth_login',
|
|
233
|
+
Error_UserNotFoundTemplate = 'error_user_not_found_template'
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const userComponent: ComponentDefinition<UserStringKey> = {
|
|
237
|
+
id: 'user-system',
|
|
238
|
+
name: 'User System',
|
|
239
|
+
stringKeys: Object.values(UserStringKey)
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const registration: ComponentRegistration<UserStringKey, CoreLanguageCode> = {
|
|
243
|
+
component: userComponent,
|
|
244
|
+
strings: {
|
|
245
|
+
[LanguageCodes.EN_US]: {
|
|
246
|
+
[UserStringKey.Auth_Login]: 'Login',
|
|
247
|
+
[UserStringKey.Error_UserNotFoundTemplate]: 'User "{username}" not found'
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
i18n.registerComponent(registration);
|
|
233
253
|
|
|
234
|
-
// Use
|
|
235
|
-
const loginText =
|
|
236
|
-
const userNotFound =
|
|
254
|
+
// Use translations
|
|
255
|
+
const loginText = i18n.translate('user-system', UserStringKey.Auth_Login);
|
|
256
|
+
const userNotFound = i18n.translate(
|
|
257
|
+
'user-system',
|
|
237
258
|
UserStringKey.Error_UserNotFoundTemplate,
|
|
238
259
|
{ username: 'john_doe' }
|
|
239
260
|
);
|
|
@@ -309,18 +330,18 @@ const myComponent: ComponentDefinition<MyStrings> = {
|
|
|
309
330
|
};
|
|
310
331
|
|
|
311
332
|
// System has EN, FR, ES languages - component must provide translations for all three
|
|
312
|
-
const registration: ComponentRegistration<MyStrings,
|
|
333
|
+
const registration: ComponentRegistration<MyStrings, CoreLanguageCode> = {
|
|
313
334
|
component: myComponent,
|
|
314
335
|
strings: {
|
|
315
|
-
[
|
|
336
|
+
[LanguageCodes.EN_US]: {
|
|
316
337
|
[MyStrings.Welcome]: 'Welcome',
|
|
317
338
|
[MyStrings.Goodbye]: 'Goodbye'
|
|
318
339
|
},
|
|
319
|
-
[
|
|
340
|
+
[LanguageCodes.FR]: {
|
|
320
341
|
[MyStrings.Welcome]: 'Bienvenue',
|
|
321
342
|
[MyStrings.Goodbye]: 'Au revoir'
|
|
322
343
|
},
|
|
323
|
-
[
|
|
344
|
+
[LanguageCodes.ES]: {
|
|
324
345
|
[MyStrings.Welcome]: 'Bienvenido',
|
|
325
346
|
[MyStrings.Goodbye]: 'Adiós'
|
|
326
347
|
}
|
|
@@ -343,9 +364,9 @@ Components can support different subsets of system languages:
|
|
|
343
364
|
const componentA = {
|
|
344
365
|
component: { id: 'comp-a', name: 'Component A', stringKeys: ['hello'] },
|
|
345
366
|
strings: {
|
|
346
|
-
en: { hello: 'Hello' },
|
|
347
|
-
fr: { hello: 'Bonjour' },
|
|
348
|
-
es: { hello: 'Hola' }
|
|
367
|
+
'en-US': { hello: 'Hello' },
|
|
368
|
+
'fr': { hello: 'Bonjour' },
|
|
369
|
+
'es': { hello: 'Hola' }
|
|
349
370
|
}
|
|
350
371
|
};
|
|
351
372
|
|
|
@@ -353,8 +374,8 @@ const componentA = {
|
|
|
353
374
|
const componentB = {
|
|
354
375
|
component: { id: 'comp-b', name: 'Component B', stringKeys: ['save'] },
|
|
355
376
|
strings: {
|
|
356
|
-
en: { save: 'Save' },
|
|
357
|
-
de: { save: 'Speichern' }
|
|
377
|
+
'en-US': { save: 'Save' },
|
|
378
|
+
'de': { save: 'Speichern' }
|
|
358
379
|
}
|
|
359
380
|
};
|
|
360
381
|
|
|
@@ -363,23 +384,25 @@ i18n.registerComponent(componentA); // ✓ Complete
|
|
|
363
384
|
i18n.registerComponent(componentB); // ⚠ Missing FR, ES - uses fallback
|
|
364
385
|
|
|
365
386
|
// Usage automatically handles fallbacks
|
|
366
|
-
i18n.translate('comp-b', 'save', {}, 'fr'); // Returns 'Save' (
|
|
387
|
+
i18n.translate('comp-b', 'save', {}, 'fr'); // Returns 'Save' (en-US fallback)
|
|
367
388
|
```
|
|
368
389
|
|
|
369
390
|
#### Dynamic Language Addition
|
|
370
391
|
|
|
371
392
|
```typescript
|
|
393
|
+
import { createLanguageDefinition } from '@digitaldefiance/i18n-lib';
|
|
394
|
+
|
|
372
395
|
// Add new language to system
|
|
373
|
-
const germanLang =
|
|
396
|
+
const germanLang = createLanguageDefinition('de', 'Deutsch', 'de');
|
|
374
397
|
i18n.registerLanguage(germanLang);
|
|
375
398
|
|
|
376
399
|
// New component registrations now require German translations
|
|
377
400
|
const newRegistration = {
|
|
378
401
|
component: { id: 'new-comp', name: 'New Component', stringKeys: ['test'] },
|
|
379
402
|
strings: {
|
|
380
|
-
en: { test: 'Test' },
|
|
381
|
-
fr: { test: 'Test' },
|
|
382
|
-
es: { test: 'Prueba' }
|
|
403
|
+
'en-US': { test: 'Test' },
|
|
404
|
+
'fr': { test: 'Test' },
|
|
405
|
+
'es': { test: 'Prueba' }
|
|
383
406
|
// Missing 'de' - validation will flag this
|
|
384
407
|
}
|
|
385
408
|
};
|
|
@@ -412,7 +435,7 @@ const strictEngine = new PluginI18nEngine(languages, {
|
|
|
412
435
|
validation: {
|
|
413
436
|
requireCompleteStrings: true,
|
|
414
437
|
allowPartialRegistration: false,
|
|
415
|
-
fallbackLanguageId: 'en'
|
|
438
|
+
fallbackLanguageId: 'en-US'
|
|
416
439
|
}
|
|
417
440
|
});
|
|
418
441
|
```
|
|
@@ -438,7 +461,7 @@ For complete documentation on the plugin architecture, see [PLUGIN_ARCHITECTURE.
|
|
|
438
461
|
The `TranslatableGenericError` class provides a simple way to create errors with translated messages that work across any component:
|
|
439
462
|
|
|
440
463
|
```typescript
|
|
441
|
-
import { TranslatableGenericError, CoreStringKey,
|
|
464
|
+
import { TranslatableGenericError, CoreStringKey, LanguageCodes } from '@digitaldefiance/i18n-lib';
|
|
442
465
|
|
|
443
466
|
// Define your error string keys
|
|
444
467
|
enum UserErrorKey {
|
|
@@ -457,12 +480,12 @@ const userErrorComponent = {
|
|
|
457
480
|
const registration = {
|
|
458
481
|
component: userErrorComponent,
|
|
459
482
|
strings: {
|
|
460
|
-
en: {
|
|
483
|
+
'en-US': {
|
|
461
484
|
[UserErrorKey.UserNotFound]: 'User "{username}" not found',
|
|
462
485
|
[UserErrorKey.InvalidCredentials]: 'Invalid credentials provided',
|
|
463
486
|
[UserErrorKey.AccountLocked]: 'Account locked until {unlockTime}'
|
|
464
487
|
},
|
|
465
|
-
fr: {
|
|
488
|
+
'fr': {
|
|
466
489
|
[UserErrorKey.UserNotFound]: 'Utilisateur "{username}" introuvable',
|
|
467
490
|
[UserErrorKey.InvalidCredentials]: 'Identifiants invalides fournis',
|
|
468
491
|
[UserErrorKey.AccountLocked]: 'Compte verrouillé jusqu\'à {unlockTime}'
|
|
@@ -477,7 +500,7 @@ throw new TranslatableGenericError(
|
|
|
477
500
|
'user-errors',
|
|
478
501
|
UserErrorKey.UserNotFound,
|
|
479
502
|
{ username: 'john_doe' },
|
|
480
|
-
'en',
|
|
503
|
+
'en-US',
|
|
481
504
|
{ userId: 123 }, // metadata
|
|
482
505
|
'myapp' // engine instance key
|
|
483
506
|
);
|
|
@@ -506,7 +529,7 @@ throw new TranslatableGenericError(
|
|
|
506
529
|
'core',
|
|
507
530
|
CoreStringKey.Error_AccessDenied,
|
|
508
531
|
undefined,
|
|
509
|
-
|
|
532
|
+
LanguageCodes.EN_US,
|
|
510
533
|
{ requestId: '12345' },
|
|
511
534
|
'myapp'
|
|
512
535
|
);
|
|
@@ -1036,6 +1059,11 @@ Part of the DigitalBurnbag project - a secure file sharing and automated protoco
|
|
|
1036
1059
|
|
|
1037
1060
|
## ChangeLog
|
|
1038
1061
|
|
|
1062
|
+
### Version 1.2.1
|
|
1063
|
+
|
|
1064
|
+
- Thu Oct 23 2025 15:10:00 GMT-0700 (Pacific Daylight Time)
|
|
1065
|
+
- Update README
|
|
1066
|
+
|
|
1039
1067
|
### Version 1.2.0
|
|
1040
1068
|
|
|
1041
1069
|
- Thu Oct 23 2025 14:13:00 GMT-0700 (Pacific Daylight Time)
|
|
@@ -1063,7 +1091,7 @@ Part of the DigitalBurnbag project - a secure file sharing and automated protoco
|
|
|
1063
1091
|
|
|
1064
1092
|
#### Migration Guide
|
|
1065
1093
|
```typescript
|
|
1066
|
-
// Before
|
|
1094
|
+
// Before (v1.1.x)
|
|
1067
1095
|
import { CoreLanguage } from '@digitaldefiance/i18n-lib';
|
|
1068
1096
|
i18n.setLanguage(CoreLanguage.French);
|
|
1069
1097
|
|