@digitaldefiance/i18n-lib 1.3.11 → 1.3.13

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 (148) hide show
  1. package/README.md +8 -0
  2. package/package.json +12 -27
  3. package/src/active-context.ts +30 -0
  4. package/src/component-definition.ts +11 -0
  5. package/src/component-registration.ts +13 -0
  6. package/src/component-registry.ts +392 -0
  7. package/src/context-error-type.ts +3 -0
  8. package/src/context-error.ts +16 -0
  9. package/src/context-manager.ts +71 -0
  10. package/src/context.ts +90 -0
  11. package/src/core-i18n.ts +609 -0
  12. package/src/core-string-key.ts +49 -0
  13. package/src/create-translation-adapter.ts +47 -0
  14. package/src/currency-code.ts +35 -0
  15. package/{dist/currency-format.d.ts → src/currency-format.ts} +5 -4
  16. package/src/currency.ts +52 -0
  17. package/src/default-config.ts +199 -0
  18. package/src/enum-registry.ts +138 -0
  19. package/src/global-active-context.ts +255 -0
  20. package/src/handleable.ts +79 -0
  21. package/src/i-global-active-context.ts +59 -0
  22. package/src/i-handleable-error-options.ts +6 -0
  23. package/src/i-handleable.ts +5 -0
  24. package/src/i18n-config.ts +29 -0
  25. package/{dist/i18n-context.d.ts → src/i18n-context.ts} +7 -6
  26. package/src/i18n-engine.ts +491 -0
  27. package/{dist/index.d.ts → src/index.ts} +10 -1
  28. package/{dist/language-codes.d.ts → src/language-codes.ts} +23 -11
  29. package/src/language-definition.ts +13 -0
  30. package/src/language-registry.ts +292 -0
  31. package/src/plugin-i18n-engine.ts +520 -0
  32. package/src/plugin-translatable-generic-error.ts +106 -0
  33. package/src/plugin-translatable-handleable-generic.ts +60 -0
  34. package/src/plugin-typed-handleable.ts +77 -0
  35. package/src/registry-config.ts +15 -0
  36. package/src/registry-error-type.ts +12 -0
  37. package/src/registry-error.ts +74 -0
  38. package/src/strict-types.ts +35 -0
  39. package/src/template.ts +63 -0
  40. package/src/timezone.ts +20 -0
  41. package/src/translatable.ts +15 -0
  42. package/src/translation-engine.ts +8 -0
  43. package/src/translation-request.ts +12 -0
  44. package/src/translation-response.ts +8 -0
  45. package/src/typed-error.ts +384 -0
  46. package/src/typed-handleable.ts +70 -0
  47. package/{dist/types.d.ts → src/types.ts} +75 -20
  48. package/src/unified-translator.ts +96 -0
  49. package/src/utils.ts +213 -0
  50. package/src/validation-config.ts +11 -0
  51. package/src/validation-result.ts +12 -0
  52. package/dist/active-context.d.ts +0 -29
  53. package/dist/active-context.js +0 -2
  54. package/dist/component-definition.d.ts +0 -11
  55. package/dist/component-definition.js +0 -2
  56. package/dist/component-registration.d.ts +0 -9
  57. package/dist/component-registration.js +0 -2
  58. package/dist/component-registry.d.ts +0 -68
  59. package/dist/component-registry.js +0 -245
  60. package/dist/context-error-type.d.ts +0 -3
  61. package/dist/context-error-type.js +0 -7
  62. package/dist/context-error.d.ts +0 -6
  63. package/dist/context-error.js +0 -15
  64. package/dist/context-manager.d.ts +0 -33
  65. package/dist/context-manager.js +0 -61
  66. package/dist/context.d.ts +0 -44
  67. package/dist/context.js +0 -69
  68. package/dist/core-i18n.d.ts +0 -62
  69. package/dist/core-i18n.js +0 -477
  70. package/dist/core-string-key.d.ts +0 -42
  71. package/dist/core-string-key.js +0 -50
  72. package/dist/create-translation-adapter.d.ts +0 -20
  73. package/dist/create-translation-adapter.js +0 -36
  74. package/dist/currency-code.d.ts +0 -19
  75. package/dist/currency-code.js +0 -36
  76. package/dist/currency-format.js +0 -2
  77. package/dist/currency.d.ts +0 -11
  78. package/dist/currency.js +0 -48
  79. package/dist/default-config.d.ts +0 -32
  80. package/dist/default-config.js +0 -101
  81. package/dist/enum-registry.d.ts +0 -44
  82. package/dist/enum-registry.js +0 -100
  83. package/dist/global-active-context.d.ts +0 -50
  84. package/dist/global-active-context.js +0 -177
  85. package/dist/handleable.d.ts +0 -13
  86. package/dist/handleable.js +0 -56
  87. package/dist/i-global-active-context.d.ts +0 -22
  88. package/dist/i-global-active-context.js +0 -2
  89. package/dist/i-handleable-error-options.d.ts +0 -6
  90. package/dist/i-handleable-error-options.js +0 -2
  91. package/dist/i-handleable.d.ts +0 -5
  92. package/dist/i-handleable.js +0 -2
  93. package/dist/i18n-config.d.ts +0 -20
  94. package/dist/i18n-config.js +0 -2
  95. package/dist/i18n-context.js +0 -2
  96. package/dist/i18n-engine.d.ts +0 -178
  97. package/dist/i18n-engine.js +0 -338
  98. package/dist/index.js +0 -83
  99. package/dist/language-codes.js +0 -31
  100. package/dist/language-definition.d.ts +0 -13
  101. package/dist/language-definition.js +0 -2
  102. package/dist/language-registry.d.ts +0 -113
  103. package/dist/language-registry.js +0 -216
  104. package/dist/plugin-i18n-engine.d.ts +0 -146
  105. package/dist/plugin-i18n-engine.js +0 -360
  106. package/dist/plugin-translatable-generic-error.d.ts +0 -29
  107. package/dist/plugin-translatable-generic-error.js +0 -66
  108. package/dist/plugin-translatable-handleable-generic.d.ts +0 -28
  109. package/dist/plugin-translatable-handleable-generic.js +0 -40
  110. package/dist/plugin-typed-handleable.d.ts +0 -14
  111. package/dist/plugin-typed-handleable.js +0 -45
  112. package/dist/registry-config.d.ts +0 -14
  113. package/dist/registry-config.js +0 -2
  114. package/dist/registry-error-type.d.ts +0 -12
  115. package/dist/registry-error-type.js +0 -16
  116. package/dist/registry-error.d.ts +0 -18
  117. package/dist/registry-error.js +0 -45
  118. package/dist/strict-types.d.ts +0 -18
  119. package/dist/strict-types.js +0 -17
  120. package/dist/template.d.ts +0 -12
  121. package/dist/template.js +0 -30
  122. package/dist/timezone.d.ts +0 -11
  123. package/dist/timezone.js +0 -22
  124. package/dist/translatable-generic-error.d.ts +0 -29
  125. package/dist/translatable-generic-error.js +0 -66
  126. package/dist/translatable-handleable-generic.d.ts +0 -28
  127. package/dist/translatable-handleable-generic.js +0 -40
  128. package/dist/translatable.d.ts +0 -5
  129. package/dist/translatable.js +0 -11
  130. package/dist/translation-engine.d.ts +0 -8
  131. package/dist/translation-engine.js +0 -2
  132. package/dist/translation-request.d.ts +0 -9
  133. package/dist/translation-request.js +0 -2
  134. package/dist/translation-response.d.ts +0 -8
  135. package/dist/translation-response.js +0 -2
  136. package/dist/typed-error.d.ts +0 -72
  137. package/dist/typed-error.js +0 -251
  138. package/dist/typed-handleable.d.ts +0 -14
  139. package/dist/typed-handleable.js +0 -40
  140. package/dist/types.js +0 -18
  141. package/dist/unified-translator.d.ts +0 -30
  142. package/dist/unified-translator.js +0 -68
  143. package/dist/utils.d.ts +0 -64
  144. package/dist/utils.js +0 -130
  145. package/dist/validation-config.d.ts +0 -11
  146. package/dist/validation-config.js +0 -2
  147. package/dist/validation-result.d.ts +0 -12
  148. package/dist/validation-result.js +0 -2
@@ -0,0 +1,77 @@
1
+ import { HandleableError } from './handleable';
2
+ import { IHandleable } from './i-handleable';
3
+ import { HandleableErrorOptions } from './i-handleable-error-options';
4
+ import { PluginI18nEngine } from './plugin-i18n-engine';
5
+ import { CompleteReasonMap, PluginTypedError } from './typed-error';
6
+
7
+ type ErrorConstructorWithStack = ErrorConstructor & {
8
+ captureStackTrace?: (target: Error, constructorOpt?: Function) => void;
9
+ };
10
+
11
+ export class PluginTypedHandleableError<
12
+ TEnum extends Record<string, string>,
13
+ TStringKey extends string,
14
+ TLanguages extends string = string,
15
+ >
16
+ extends PluginTypedError<TEnum, TStringKey, TLanguages>
17
+ implements IHandleable
18
+ {
19
+ public override readonly cause?: Error;
20
+ public readonly statusCode: number;
21
+ public readonly sourceData?: unknown;
22
+ private _handled: boolean;
23
+
24
+ constructor(
25
+ engine: PluginI18nEngine<TLanguages>,
26
+ componentId: string,
27
+ type: TEnum[keyof TEnum],
28
+ reasonMap: CompleteReasonMap<TEnum, TStringKey>,
29
+ source: Error,
30
+ options?: HandleableErrorOptions,
31
+ language?: TLanguages,
32
+ otherVars?: Record<string, string | number>,
33
+ ) {
34
+ super(engine, componentId, type, reasonMap, language, otherVars);
35
+ this.cause = options?.cause ?? source;
36
+ this.statusCode = options?.statusCode ?? 500;
37
+ this._handled = options?.handled ?? false;
38
+ this.sourceData = options?.sourceData;
39
+
40
+ // Capture stack trace - prioritize source stack, then capture new one
41
+ if (source.stack) {
42
+ this.stack = source.stack;
43
+ } else if ((Error as ErrorConstructorWithStack).captureStackTrace) {
44
+ (Error as ErrorConstructorWithStack).captureStackTrace?.(
45
+ this,
46
+ this.constructor,
47
+ );
48
+ } else {
49
+ this.stack = new Error().stack;
50
+ }
51
+ this.name = this.constructor.name;
52
+ }
53
+
54
+ public get handled(): boolean {
55
+ return this._handled;
56
+ }
57
+
58
+ public set handled(value: boolean) {
59
+ this._handled = value;
60
+ }
61
+
62
+ public toJSON(): Record<string, unknown> {
63
+ return {
64
+ name: this.name,
65
+ message: this.message,
66
+ statusCode: this.statusCode,
67
+ handled: this.handled,
68
+ stack: this.stack,
69
+ cause:
70
+ this.cause instanceof HandleableError ||
71
+ this.cause instanceof PluginTypedHandleableError
72
+ ? this.cause.toJSON()
73
+ : this.cause?.message,
74
+ ...(this.sourceData ? { sourceData: this.sourceData } : {}),
75
+ };
76
+ }
77
+ }
@@ -0,0 +1,15 @@
1
+ import { CurrencyCode } from './currency-code';
2
+ import { Timezone } from './timezone';
3
+ import { ValidationConfig } from './validation-config';
4
+
5
+ /**
6
+ * Registry configuration
7
+ */
8
+ export interface RegistryConfig<TLanguages extends string> {
9
+ readonly defaultLanguage: TLanguages;
10
+ readonly fallbackLanguage: TLanguages;
11
+ readonly defaultCurrencyCode: CurrencyCode;
12
+ readonly timezone: Timezone;
13
+ readonly adminTimezone: Timezone;
14
+ readonly validation: ValidationConfig;
15
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Error types for registry operations
3
+ */
4
+ export enum RegistryErrorType {
5
+ ComponentNotFound = 'COMPONENT_NOT_FOUND',
6
+ LanguageNotFound = 'LANGUAGE_NOT_FOUND',
7
+ StringKeyNotFound = 'STRING_KEY_NOT_FOUND',
8
+ IncompleteRegistration = 'INCOMPLETE_REGISTRATION',
9
+ DuplicateComponent = 'DUPLICATE_COMPONENT',
10
+ DuplicateLanguage = 'DUPLICATE_LANGUAGE',
11
+ ValidationFailed = 'VALIDATION_FAILED',
12
+ }
@@ -0,0 +1,74 @@
1
+ // CoreLanguageCode is deprecated - using string
2
+ import { CoreStringKey } from './core-string-key';
3
+ import { RegistryErrorType } from './registry-error-type';
4
+ import { TranslationEngine, createTranslatedError } from './typed-error';
5
+
6
+ /**
7
+ * Reason map for registry errors
8
+ */
9
+ const REGISTRY_ERROR_REASON_MAP = {
10
+ [RegistryErrorType.ComponentNotFound]:
11
+ CoreStringKey.Error_ComponentNotFoundTemplate,
12
+ [RegistryErrorType.DuplicateComponent]:
13
+ CoreStringKey.Error_DuplicateComponentTemplate,
14
+ [RegistryErrorType.DuplicateLanguage]:
15
+ CoreStringKey.Error_DuplicateLanguageTemplate,
16
+ [RegistryErrorType.IncompleteRegistration]:
17
+ CoreStringKey.Error_IncompleteRegistrationTemplate,
18
+ [RegistryErrorType.LanguageNotFound]:
19
+ CoreStringKey.Error_LanguageNotFoundTemplate,
20
+ [RegistryErrorType.StringKeyNotFound]:
21
+ CoreStringKey.Error_StringKeyNotFoundTemplate,
22
+ [RegistryErrorType.ValidationFailed]:
23
+ CoreStringKey.Error_ValidationFailedTemplate,
24
+ } as const;
25
+
26
+ /**
27
+ * Registry error class that can work with plugin engines
28
+ */
29
+ export class RegistryError extends Error {
30
+ constructor(
31
+ public readonly type: RegistryErrorType,
32
+ message: string,
33
+ public readonly metadata?: Record<string, any>,
34
+ ) {
35
+ super(message);
36
+ this.name = 'RegistryError';
37
+ }
38
+
39
+ /**
40
+ * Create a registry error with translation support
41
+ */
42
+ static createWithEngine(
43
+ engine: TranslationEngine,
44
+ type: RegistryErrorType,
45
+ variables?: Record<string, string | number>,
46
+ language?: string,
47
+ metadata?: Record<string, any>,
48
+ ): RegistryError {
49
+ const error = createTranslatedError(
50
+ engine,
51
+ 'core',
52
+ type,
53
+ REGISTRY_ERROR_REASON_MAP,
54
+ variables,
55
+ language,
56
+ metadata,
57
+ 'RegistryError',
58
+ );
59
+
60
+ // Convert to RegistryError instance
61
+ return new RegistryError(type, error.message, metadata);
62
+ }
63
+
64
+ /**
65
+ * Create a simple RegistryError without engine dependency
66
+ */
67
+ static createSimple(
68
+ type: RegistryErrorType,
69
+ message: string,
70
+ metadata?: Record<string, any>,
71
+ ): RegistryError {
72
+ return new RegistryError(type, message, metadata);
73
+ }
74
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Enforces that for each language L, all string keys K are present.
3
+ */
4
+ export type CompleteLanguageStrings<TStringKey extends string> = {
5
+ [K in TStringKey]: string;
6
+ };
7
+
8
+ export type CompleteComponentLanguageStrings<
9
+ TStringKey extends string,
10
+ TLanguages extends string,
11
+ > = {
12
+ [L in TLanguages]: CompleteLanguageStrings<TStringKey>;
13
+ };
14
+
15
+ /**
16
+ * Helper to assert at compile-time that an object is a complete component language map.
17
+ * Returns the object unchanged at runtime.
18
+ */
19
+ export function createCompleteComponentStrings<
20
+ TStringKey extends string,
21
+ TLanguages extends string,
22
+ >(
23
+ obj: CompleteComponentLanguageStrings<TStringKey, TLanguages>,
24
+ ): CompleteComponentLanguageStrings<TStringKey, TLanguages> {
25
+ return obj;
26
+ }
27
+
28
+ /**
29
+ * Utility to extract missing keys at compile time (identity, purely for readability)
30
+ */
31
+ export function defineLanguageStrings<TStringKey extends string>(
32
+ strings: CompleteLanguageStrings<TStringKey>,
33
+ ): CompleteLanguageStrings<TStringKey> {
34
+ return strings;
35
+ }
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Template processing utilities for i18n
3
+ */
4
+ export type EnumKeys<T> = keyof T;
5
+
6
+ /**
7
+ * Recursive type to validate that all enum keys in the template string exist in the provided enum type
8
+ */
9
+ export type IsValidEnumTemplate<
10
+ T extends string,
11
+ TEnum,
12
+ > = T extends `${string}{{${string}.${infer Key}}}${infer Rest}`
13
+ ? Key extends EnumKeys<TEnum>
14
+ ? IsValidEnumTemplate<Rest, TEnum>
15
+ : false
16
+ : true;
17
+
18
+ /**
19
+ * Template function that processes {{EnumName.EnumKey}} patterns
20
+ */
21
+ export function createTemplateProcessor<
22
+ TEnum extends Record<string, string>,
23
+ TLanguage extends string,
24
+ >(
25
+ enumObj: TEnum,
26
+ translateFn: (
27
+ key: TEnum[keyof TEnum],
28
+ vars?: Record<string, string | number>,
29
+ language?: TLanguage,
30
+ ) => string,
31
+ enumName: string,
32
+ ) {
33
+ return function t<T extends string>(
34
+ str: IsValidEnumTemplate<T, TEnum> extends true ? T : never,
35
+ language?: TLanguage,
36
+ ...otherVars: Record<string, string | number>[]
37
+ ): string {
38
+ let varIndex = 0;
39
+ const pattern = new RegExp(`\\{\\{${enumName}\\.(\\w+)\\}\\}`, 'g');
40
+
41
+ // First replace enum patterns
42
+ let result = str.replace(pattern, (match, enumKey) => {
43
+ const enumValue = enumObj[enumKey as keyof TEnum];
44
+ if (!enumValue) {
45
+ return match; // Return original if enum key not found
46
+ }
47
+
48
+ const needsVars = (enumValue as string)
49
+ .toLowerCase()
50
+ .endsWith('template');
51
+ const vars = needsVars ? otherVars[varIndex++] ?? {} : {};
52
+ return translateFn(enumValue, vars, language);
53
+ });
54
+
55
+ // Then replace any remaining variables from all otherVars
56
+ const allVars = otherVars.reduce((acc, vars) => ({ ...acc, ...vars }), {});
57
+ result = result.replace(/\{(\w+)\}/g, (match, varName) => {
58
+ return allVars[varName] !== undefined ? String(allVars[varName]) : match;
59
+ });
60
+
61
+ return result;
62
+ };
63
+ }
@@ -0,0 +1,20 @@
1
+ import { isValidTimezone } from './utils';
2
+
3
+ /**
4
+ * Class representing a validated timezone.
5
+ */
6
+ export class Timezone {
7
+ private readonly _timezone: string;
8
+ constructor(timezone: string) {
9
+ if (!isValidTimezone(timezone)) {
10
+ throw new Error(`Invalid timezone: ${timezone}`);
11
+ }
12
+ this._timezone = timezone;
13
+ }
14
+ /**
15
+ * Gets the timezone value.
16
+ */
17
+ public get value(): string {
18
+ return this._timezone;
19
+ }
20
+ }
@@ -0,0 +1,15 @@
1
+ import { I18nEngine } from './i18n-engine';
2
+ import { Language } from './default-config';
3
+
4
+ export class TranslatableError<TStringKey extends string> extends Error {
5
+ constructor(
6
+ engine: I18nEngine<TStringKey, Language, any, any>,
7
+ string: TStringKey,
8
+ otherVars?: Record<string, string | number>,
9
+ language?: Language,
10
+ ) {
11
+ super(engine.translate(string, otherVars, language));
12
+ this.name = 'TranslatableError';
13
+ Object.setPrototypeOf(this, TranslatableError.prototype);
14
+ }
15
+ }
@@ -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: (key: TStringKey, vars?: Record<string, string | number>, lang?: any) => string;
8
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Translation request interface
3
+ */
4
+ export interface TranslationRequest<
5
+ TStringKeys extends string,
6
+ TLanguages extends string,
7
+ > {
8
+ readonly componentId: string;
9
+ readonly stringKey: TStringKeys;
10
+ readonly language?: TLanguages;
11
+ readonly variables?: Record<string, string | number>;
12
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Translation response interface
3
+ */
4
+ export interface TranslationResponse {
5
+ readonly translation: string;
6
+ readonly actualLanguage: string;
7
+ readonly wasFallback: boolean;
8
+ }