@digitaldefiance/i18n-lib 1.2.0 → 1.2.2
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 +75 -31
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/translation-engine.d.ts +8 -0
- package/dist/typed-error.d.ts +2 -6
- package/package.json +1 -1
- package/dist/core-language.d.ts +0 -13
- package/dist/core-language.js +0 -17
- package/dist/package.json +0 -52
- package/dist/registry-context.d.ts +0 -12
- package/dist/src/active-context.d.ts +0 -29
- package/dist/src/active-context.js +0 -2
- package/dist/src/context-manager.d.ts +0 -33
- package/dist/src/context-manager.js +0 -61
- package/dist/src/context.d.ts +0 -43
- package/dist/src/context.js +0 -69
- package/dist/src/currency-code.d.ts +0 -19
- package/dist/src/currency-code.js +0 -36
- package/dist/src/currency-format.d.ts +0 -10
- package/dist/src/currency-format.js +0 -2
- package/dist/src/currency.d.ts +0 -11
- package/dist/src/currency.js +0 -48
- package/dist/src/enum-registry.d.ts +0 -35
- package/dist/src/enum-registry.js +0 -67
- package/dist/src/i18n-engine.d.ts +0 -156
- package/dist/src/i18n-engine.js +0 -267
- package/dist/src/index.d.ts +0 -13
- package/dist/src/index.js +0 -32
- package/dist/src/template.d.ts +0 -12
- package/dist/src/template.js +0 -30
- package/dist/src/timezone.d.ts +0 -11
- package/dist/src/timezone.js +0 -22
- package/dist/src/types.d.ts +0 -78
- package/dist/src/types.js +0 -14
- package/dist/src/utils.d.ts +0 -41
- package/dist/src/utils.js +0 -85
- /package/dist/{registry-context.js → translation-engine.js} +0 -0
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,27 @@ Part of the DigitalBurnbag project - a secure file sharing and automated protoco
|
|
|
1036
1059
|
|
|
1037
1060
|
## ChangeLog
|
|
1038
1061
|
|
|
1062
|
+
### Version 1.2.2
|
|
1063
|
+
|
|
1064
|
+
- Thu Oct 23 2025 18:40:00 GMT-0700 (Pacific Daylight Time)
|
|
1065
|
+
|
|
1066
|
+
**i18n Library - TranslationEngine Interface Refactoring**
|
|
1067
|
+
|
|
1068
|
+
- Made `TranslationEngine` interface generic with `TStringKey` type parameter for improved type safety
|
|
1069
|
+
- Changed `translate` and `safeTranslate` methods from optional to required in `TranslationEngine` interface
|
|
1070
|
+
- Exported `TranslationEngine` from `typed-error.ts` for consistent usage across packages
|
|
1071
|
+
- Updated `TypedHandleableError` in ecies-lib to use generic `TranslationEngine<TStringKey>` instead of inline interface types
|
|
1072
|
+
- Updated test mocks to implement both required `translate` and `safeTranslate` methods
|
|
1073
|
+
|
|
1074
|
+
**Breaking Changes:**
|
|
1075
|
+
- Any code implementing `TranslationEngine` must now provide both `translate` and `safeTranslate` methods (previously optional)
|
|
1076
|
+
- `TranslationEngine` now requires explicit type parameter when used (e.g., `TranslationEngine<EciesStringKey>`)
|
|
1077
|
+
|
|
1078
|
+
### Version 1.2.1
|
|
1079
|
+
|
|
1080
|
+
- Thu Oct 23 2025 15:10:00 GMT-0700 (Pacific Daylight Time)
|
|
1081
|
+
- Update README
|
|
1082
|
+
|
|
1039
1083
|
### Version 1.2.0
|
|
1040
1084
|
|
|
1041
1085
|
- Thu Oct 23 2025 14:13:00 GMT-0700 (Pacific Daylight Time)
|
|
@@ -1063,7 +1107,7 @@ Part of the DigitalBurnbag project - a secure file sharing and automated protoco
|
|
|
1063
1107
|
|
|
1064
1108
|
#### Migration Guide
|
|
1065
1109
|
```typescript
|
|
1066
|
-
// Before
|
|
1110
|
+
// Before (v1.1.x)
|
|
1067
1111
|
import { CoreLanguage } from '@digitaldefiance/i18n-lib';
|
|
1068
1112
|
i18n.setLanguage(CoreLanguage.French);
|
|
1069
1113
|
|
package/dist/index.d.ts
CHANGED
|
@@ -32,6 +32,7 @@ export * from './plugin-i18n-engine';
|
|
|
32
32
|
export * from './registry-config';
|
|
33
33
|
export * from './registry-error';
|
|
34
34
|
export * from './registry-error-type';
|
|
35
|
+
export * from './translation-engine';
|
|
35
36
|
export * from './translation-request';
|
|
36
37
|
export * from './translation-response';
|
|
37
38
|
export { createCoreI18nEngine as createCoreI18n } from './core-i18n';
|
package/dist/index.js
CHANGED
|
@@ -51,6 +51,7 @@ __exportStar(require("./plugin-i18n-engine"), exports);
|
|
|
51
51
|
__exportStar(require("./registry-config"), exports);
|
|
52
52
|
__exportStar(require("./registry-error"), exports);
|
|
53
53
|
__exportStar(require("./registry-error-type"), exports);
|
|
54
|
+
__exportStar(require("./translation-engine"), exports);
|
|
54
55
|
__exportStar(require("./translation-request"), exports);
|
|
55
56
|
__exportStar(require("./translation-response"), exports);
|
|
56
57
|
// Re-export for convenience
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal interface for translation engines
|
|
3
|
+
* Allows flexibility in what can be used as a translation engine
|
|
4
|
+
*/
|
|
5
|
+
export interface TranslationEngine<TStringKey extends string = string> {
|
|
6
|
+
translate: (key: TStringKey, vars?: Record<string, string | number>, lang?: any) => string;
|
|
7
|
+
safeTranslate: (componentId: string, key: TStringKey, vars?: Record<string, string | number>, lang?: any) => string;
|
|
8
|
+
}
|
package/dist/typed-error.d.ts
CHANGED
|
@@ -3,12 +3,8 @@ import { I18nEngine } from './i18n-engine';
|
|
|
3
3
|
import { CoreLanguageCode } from './core-i18n';
|
|
4
4
|
import { CoreStringKey } from './core-string-key';
|
|
5
5
|
import { PluginI18nEngine } from './plugin-i18n-engine';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*/
|
|
9
|
-
export interface TranslationEngine {
|
|
10
|
-
safeTranslate(componentId: string, stringKey: string, variables?: Record<string, string | number>, language?: string): string;
|
|
11
|
-
}
|
|
6
|
+
import { TranslationEngine } from './translation-engine';
|
|
7
|
+
export { TranslationEngine };
|
|
12
8
|
/**
|
|
13
9
|
* Type constraint to ensure reasonMap has entries for all enum values
|
|
14
10
|
*/
|
package/package.json
CHANGED
package/dist/core-language.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Default languages supported by the core I18n system
|
|
3
|
-
*/
|
|
4
|
-
export declare enum CoreLanguage {
|
|
5
|
-
EnglishUS = "en-US",
|
|
6
|
-
EnglishUK = "en-UK",
|
|
7
|
-
French = "fr",
|
|
8
|
-
Spanish = "es",
|
|
9
|
-
German = "de",
|
|
10
|
-
MandarinChinese = "zh-CN",
|
|
11
|
-
Japanese = "ja",
|
|
12
|
-
Ukrainian = "uk"
|
|
13
|
-
}
|
package/dist/core-language.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CoreLanguage = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Default languages supported by the core I18n system
|
|
6
|
-
*/
|
|
7
|
-
var CoreLanguage;
|
|
8
|
-
(function (CoreLanguage) {
|
|
9
|
-
CoreLanguage["EnglishUS"] = "en-US";
|
|
10
|
-
CoreLanguage["EnglishUK"] = "en-UK";
|
|
11
|
-
CoreLanguage["French"] = "fr";
|
|
12
|
-
CoreLanguage["Spanish"] = "es";
|
|
13
|
-
CoreLanguage["German"] = "de";
|
|
14
|
-
CoreLanguage["MandarinChinese"] = "zh-CN";
|
|
15
|
-
CoreLanguage["Japanese"] = "ja";
|
|
16
|
-
CoreLanguage["Ukrainian"] = "uk";
|
|
17
|
-
})(CoreLanguage || (exports.CoreLanguage = CoreLanguage = {}));
|
package/dist/package.json
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@digitaldefiance/i18n-lib",
|
|
3
|
-
"version": "1.0.23",
|
|
4
|
-
"description": "Generic i18n library with enum translation support",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"build": "yarn tsc",
|
|
9
|
-
"test": "yarn jest --detectOpenHandles",
|
|
10
|
-
"lint": "eslint src/**/*.ts tests/**/*.ts",
|
|
11
|
-
"lint:fix": "eslint src/**/*.ts tests/**/*.ts --fix",
|
|
12
|
-
"prettier:check": "prettier --check 'src/**/*.{ts,tsx}' 'tests/**/*.{ts,tsx}'",
|
|
13
|
-
"prettier:fix": "prettier --write 'src/**/*.{ts,tsx}' 'tests/**/*.{ts,tsx}'",
|
|
14
|
-
"format": "yarn prettier:fix && yarn lint:fix",
|
|
15
|
-
"prepublishOnly": "yarn build",
|
|
16
|
-
"publish:public": "npm publish --access public"
|
|
17
|
-
},
|
|
18
|
-
"devDependencies": {
|
|
19
|
-
"@types/jest": "^29.0.0",
|
|
20
|
-
"@typescript-eslint/eslint-plugin": "^8.31.1",
|
|
21
|
-
"@typescript-eslint/parser": "^8.31.1",
|
|
22
|
-
"eslint": "^9.8.0",
|
|
23
|
-
"eslint-config-prettier": "^10.1.2",
|
|
24
|
-
"eslint-plugin-import": "^2.32.0",
|
|
25
|
-
"eslint-plugin-prettier": "^5.3.1",
|
|
26
|
-
"jest": "^29.0.0",
|
|
27
|
-
"jest-util": "^30.0.5",
|
|
28
|
-
"prettier": "^2.6.2",
|
|
29
|
-
"prettier-plugin-organize-imports": "^4.1.0",
|
|
30
|
-
"ts-jest": "^29.0.0",
|
|
31
|
-
"typescript": "^5.9.2"
|
|
32
|
-
},
|
|
33
|
-
"files": [
|
|
34
|
-
"dist",
|
|
35
|
-
"README.md"
|
|
36
|
-
],
|
|
37
|
-
"keywords": [
|
|
38
|
-
"i18n",
|
|
39
|
-
"internationalization",
|
|
40
|
-
"typescript",
|
|
41
|
-
"enum",
|
|
42
|
-
"translation"
|
|
43
|
-
],
|
|
44
|
-
"author": "Digital Defiance",
|
|
45
|
-
"license": "MIT",
|
|
46
|
-
"packageManager": "yarn@4.10.3",
|
|
47
|
-
"dependencies": {
|
|
48
|
-
"moment": "^2.30.1",
|
|
49
|
-
"moment-timezone": "^0.6.0"
|
|
50
|
-
},
|
|
51
|
-
"type": "commonjs"
|
|
52
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { CurrencyCode } from './currency-code';
|
|
2
|
-
import { Timezone } from './timezone';
|
|
3
|
-
/**
|
|
4
|
-
* Translation context similar to existing I18nContext
|
|
5
|
-
*/
|
|
6
|
-
export interface RegistryContext<TLanguages extends string> {
|
|
7
|
-
currentLanguage: TLanguages;
|
|
8
|
-
fallbackLanguage: TLanguages;
|
|
9
|
-
currencyCode: CurrencyCode;
|
|
10
|
-
timezone: Timezone;
|
|
11
|
-
adminTimezone: Timezone;
|
|
12
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { LanguageContext } from './types';
|
|
2
|
-
import { Timezone } from './timezone';
|
|
3
|
-
import { CurrencyCode } from './currency-code';
|
|
4
|
-
export interface IActiveContext<TLanguage extends string> {
|
|
5
|
-
/**
|
|
6
|
-
* The default language for the user facing application
|
|
7
|
-
*/
|
|
8
|
-
language: TLanguage;
|
|
9
|
-
/**
|
|
10
|
-
* The default language for the admin interface
|
|
11
|
-
*/
|
|
12
|
-
adminLanguage: TLanguage;
|
|
13
|
-
/**
|
|
14
|
-
* The default currency code for the user facing application
|
|
15
|
-
*/
|
|
16
|
-
currencyCode: CurrencyCode;
|
|
17
|
-
/**
|
|
18
|
-
* The default language context for the current context
|
|
19
|
-
*/
|
|
20
|
-
currentContext: LanguageContext;
|
|
21
|
-
/**
|
|
22
|
-
* The default timezone for the user facing application
|
|
23
|
-
*/
|
|
24
|
-
timezone: Timezone;
|
|
25
|
-
/**
|
|
26
|
-
* The default timezone for the admin interface
|
|
27
|
-
*/
|
|
28
|
-
adminTimezone: Timezone;
|
|
29
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Context change management for i18n systems
|
|
3
|
-
*/
|
|
4
|
-
export type ContextChangeListener<T = any> = (property: string, oldValue: T, newValue: T) => void;
|
|
5
|
-
/**
|
|
6
|
-
* Manages context changes and notifies listeners.
|
|
7
|
-
*/
|
|
8
|
-
export declare class ContextManager<TContext extends Record<string, any>> {
|
|
9
|
-
protected listeners: ContextChangeListener[];
|
|
10
|
-
/**
|
|
11
|
-
* Adds a listener to be notified of context changes.
|
|
12
|
-
* @param listener - The listener function to add
|
|
13
|
-
*/
|
|
14
|
-
addListener(listener: ContextChangeListener): void;
|
|
15
|
-
/**
|
|
16
|
-
* Removes a listener from the notification list.
|
|
17
|
-
* @param listener - The listener function to remove
|
|
18
|
-
*/
|
|
19
|
-
removeListener(listener: ContextChangeListener): void;
|
|
20
|
-
/**
|
|
21
|
-
* Notifies all listeners of a context change.
|
|
22
|
-
* @param property - The property that changed
|
|
23
|
-
* @param oldValue - The old value of the property
|
|
24
|
-
* @param newValue - The new value of the property
|
|
25
|
-
*/
|
|
26
|
-
notifyChange<K extends keyof TContext>(property: K, oldValue: TContext[K], newValue: TContext[K]): void;
|
|
27
|
-
/**
|
|
28
|
-
* Creates a proxy for the given context to automatically notify listeners on changes.
|
|
29
|
-
* @param context - The context object to proxy
|
|
30
|
-
* @returns A proxied version of the context object
|
|
31
|
-
*/
|
|
32
|
-
createProxy(context: TContext): TContext;
|
|
33
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ContextManager = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Manages context changes and notifies listeners.
|
|
6
|
-
*/
|
|
7
|
-
class ContextManager {
|
|
8
|
-
constructor() {
|
|
9
|
-
this.listeners = [];
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Adds a listener to be notified of context changes.
|
|
13
|
-
* @param listener - The listener function to add
|
|
14
|
-
*/
|
|
15
|
-
addListener(listener) {
|
|
16
|
-
this.listeners.push(listener);
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Removes a listener from the notification list.
|
|
20
|
-
* @param listener - The listener function to remove
|
|
21
|
-
*/
|
|
22
|
-
removeListener(listener) {
|
|
23
|
-
const index = this.listeners.indexOf(listener);
|
|
24
|
-
if (index > -1) {
|
|
25
|
-
this.listeners.splice(index, 1);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Notifies all listeners of a context change.
|
|
30
|
-
* @param property - The property that changed
|
|
31
|
-
* @param oldValue - The old value of the property
|
|
32
|
-
* @param newValue - The new value of the property
|
|
33
|
-
*/
|
|
34
|
-
notifyChange(property, oldValue, newValue) {
|
|
35
|
-
this.listeners.forEach((listener) => {
|
|
36
|
-
try {
|
|
37
|
-
listener(property, oldValue, newValue);
|
|
38
|
-
}
|
|
39
|
-
catch (error) {
|
|
40
|
-
console.error('Error in context change listener:', error);
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Creates a proxy for the given context to automatically notify listeners on changes.
|
|
46
|
-
* @param context - The context object to proxy
|
|
47
|
-
* @returns A proxied version of the context object
|
|
48
|
-
*/
|
|
49
|
-
createProxy(context) {
|
|
50
|
-
const manager = this;
|
|
51
|
-
return new Proxy(context, {
|
|
52
|
-
set(target, property, value) {
|
|
53
|
-
const oldValue = target[property];
|
|
54
|
-
target[property] = value;
|
|
55
|
-
manager.notifyChange(property, oldValue, value);
|
|
56
|
-
return true;
|
|
57
|
-
},
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
exports.ContextManager = ContextManager;
|
package/dist/src/context.d.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { CurrencyCode } from './currency-code';
|
|
2
|
-
import { Timezone } from './timezone';
|
|
3
|
-
import { I18nContext, LanguageContext } from './types';
|
|
4
|
-
/**
|
|
5
|
-
* Creates a new I18n context with default values.
|
|
6
|
-
* @param defaultLanguage - The default language for the context
|
|
7
|
-
* @param defaultContext - The default language context
|
|
8
|
-
* @param defaultCurrencyCode - The default currency code (defaults to USD)
|
|
9
|
-
* @param defaultTimezone - The default timezone (defaults to UTC)
|
|
10
|
-
* @param defaultAdminTimezone - The default admin timezone (defaults to UTC)
|
|
11
|
-
* @returns A new I18nContext instance
|
|
12
|
-
*/
|
|
13
|
-
export declare function createContext<TLanguage extends string, TTranslationContext extends string = LanguageContext, TContext extends I18nContext<TLanguage, TTranslationContext> = I18nContext<TLanguage, TTranslationContext>>(defaultLanguage: TLanguage, defaultContext: TTranslationContext, defaultCurrencyCode?: CurrencyCode, defaultTimezone?: Timezone, defaultAdminTimezone?: Timezone): TContext;
|
|
14
|
-
/**
|
|
15
|
-
* Sets the language for the given I18n context.
|
|
16
|
-
* @param context - The I18n context to modify
|
|
17
|
-
* @param language - The language to set
|
|
18
|
-
*/
|
|
19
|
-
export declare function setLanguage<TLanguage extends string, TContext extends string = LanguageContext>(context: I18nContext<TLanguage, TContext>, language: TLanguage): void;
|
|
20
|
-
/**
|
|
21
|
-
* Sets the admin language for the given I18n context.
|
|
22
|
-
* @param context - The I18n context to modify
|
|
23
|
-
* @param language - The admin language to set
|
|
24
|
-
*/
|
|
25
|
-
export declare function setAdminLanguage<TLanguage extends string, TContext extends string = LanguageContext>(context: I18nContext<TLanguage, TContext>, language: TLanguage): void;
|
|
26
|
-
/**
|
|
27
|
-
* Sets the current context for the given I18n context.
|
|
28
|
-
* @param context - The I18n context to modify
|
|
29
|
-
* @param languageContext - The language context to set
|
|
30
|
-
*/
|
|
31
|
-
export declare function setContext<TLanguage extends string, TContext extends string = LanguageContext>(context: I18nContext<TLanguage, TContext>, languageContext: TContext): void;
|
|
32
|
-
/**
|
|
33
|
-
* Sets the timezone for the given I18n context.
|
|
34
|
-
* @param context - The I18n context to modify
|
|
35
|
-
* @param timezone - The timezone to set
|
|
36
|
-
*/
|
|
37
|
-
export declare function setTimezone<TLanguage extends string, TContext extends string = LanguageContext>(context: I18nContext<TLanguage, TContext>, timezone: Timezone): void;
|
|
38
|
-
/**
|
|
39
|
-
* Sets the admin timezone for the given I18n context.
|
|
40
|
-
* @param context - The I18n context to modify
|
|
41
|
-
* @param timezone - The admin timezone to set
|
|
42
|
-
*/
|
|
43
|
-
export declare function setAdminTimezone<TLanguage extends string, TContext extends string = LanguageContext>(context: I18nContext<TLanguage, TContext>, timezone: Timezone): void;
|
package/dist/src/context.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createContext = createContext;
|
|
4
|
-
exports.setLanguage = setLanguage;
|
|
5
|
-
exports.setAdminLanguage = setAdminLanguage;
|
|
6
|
-
exports.setContext = setContext;
|
|
7
|
-
exports.setTimezone = setTimezone;
|
|
8
|
-
exports.setAdminTimezone = setAdminTimezone;
|
|
9
|
-
const currency_code_1 = require("./currency-code");
|
|
10
|
-
const timezone_1 = require("./timezone");
|
|
11
|
-
/**
|
|
12
|
-
* Creates a new I18n context with default values.
|
|
13
|
-
* @param defaultLanguage - The default language for the context
|
|
14
|
-
* @param defaultContext - The default language context
|
|
15
|
-
* @param defaultCurrencyCode - The default currency code (defaults to USD)
|
|
16
|
-
* @param defaultTimezone - The default timezone (defaults to UTC)
|
|
17
|
-
* @param defaultAdminTimezone - The default admin timezone (defaults to UTC)
|
|
18
|
-
* @returns A new I18nContext instance
|
|
19
|
-
*/
|
|
20
|
-
function createContext(defaultLanguage, defaultContext, defaultCurrencyCode = new currency_code_1.CurrencyCode('USD'), defaultTimezone = new timezone_1.Timezone('UTC'), defaultAdminTimezone = new timezone_1.Timezone('UTC')) {
|
|
21
|
-
return {
|
|
22
|
-
language: defaultLanguage,
|
|
23
|
-
adminLanguage: defaultLanguage,
|
|
24
|
-
currencyCode: defaultCurrencyCode,
|
|
25
|
-
currentContext: defaultContext,
|
|
26
|
-
timezone: defaultTimezone,
|
|
27
|
-
adminTimezone: defaultAdminTimezone,
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Sets the language for the given I18n context.
|
|
32
|
-
* @param context - The I18n context to modify
|
|
33
|
-
* @param language - The language to set
|
|
34
|
-
*/
|
|
35
|
-
function setLanguage(context, language) {
|
|
36
|
-
context.language = language;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Sets the admin language for the given I18n context.
|
|
40
|
-
* @param context - The I18n context to modify
|
|
41
|
-
* @param language - The admin language to set
|
|
42
|
-
*/
|
|
43
|
-
function setAdminLanguage(context, language) {
|
|
44
|
-
context.adminLanguage = language;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Sets the current context for the given I18n context.
|
|
48
|
-
* @param context - The I18n context to modify
|
|
49
|
-
* @param languageContext - The language context to set
|
|
50
|
-
*/
|
|
51
|
-
function setContext(context, languageContext) {
|
|
52
|
-
context.currentContext = languageContext;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Sets the timezone for the given I18n context.
|
|
56
|
-
* @param context - The I18n context to modify
|
|
57
|
-
* @param timezone - The timezone to set
|
|
58
|
-
*/
|
|
59
|
-
function setTimezone(context, timezone) {
|
|
60
|
-
context.timezone = timezone;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Sets the admin timezone for the given I18n context.
|
|
64
|
-
* @param context - The I18n context to modify
|
|
65
|
-
* @param timezone - The admin timezone to set
|
|
66
|
-
*/
|
|
67
|
-
function setAdminTimezone(context, timezone) {
|
|
68
|
-
context.adminTimezone = timezone;
|
|
69
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Class representing a validated currency code.
|
|
3
|
-
*/
|
|
4
|
-
export declare class CurrencyCode {
|
|
5
|
-
private _value;
|
|
6
|
-
/**
|
|
7
|
-
* Gets the currency code value.
|
|
8
|
-
*/
|
|
9
|
-
get value(): string;
|
|
10
|
-
/**
|
|
11
|
-
* Sets the currency code value after validating it.
|
|
12
|
-
*/
|
|
13
|
-
set value(value: string);
|
|
14
|
-
/**
|
|
15
|
-
* Gets the list of all valid currency codes.
|
|
16
|
-
*/
|
|
17
|
-
static get values(): string[];
|
|
18
|
-
constructor(value?: string);
|
|
19
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CurrencyCode = void 0;
|
|
4
|
-
const currency_codes_1 = require("currency-codes");
|
|
5
|
-
const types_1 = require("./types");
|
|
6
|
-
/**
|
|
7
|
-
* Class representing a validated currency code.
|
|
8
|
-
*/
|
|
9
|
-
class CurrencyCode {
|
|
10
|
-
/**
|
|
11
|
-
* Gets the currency code value.
|
|
12
|
-
*/
|
|
13
|
-
get value() {
|
|
14
|
-
return this._value;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Sets the currency code value after validating it.
|
|
18
|
-
*/
|
|
19
|
-
set value(value) {
|
|
20
|
-
if (!CurrencyCode.values.includes(value)) {
|
|
21
|
-
throw new Error('Invalid currency code');
|
|
22
|
-
}
|
|
23
|
-
this._value = value;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Gets the list of all valid currency codes.
|
|
27
|
-
*/
|
|
28
|
-
static get values() {
|
|
29
|
-
return (0, currency_codes_1.codes)();
|
|
30
|
-
}
|
|
31
|
-
constructor(value = types_1.DefaultCurrencyCode) {
|
|
32
|
-
this._value = types_1.DefaultCurrencyCode;
|
|
33
|
-
this.value = value;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
exports.CurrencyCode = CurrencyCode;
|