@digitaldefiance/i18n-lib 1.3.13 → 1.3.14
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 +4 -0
- package/package.json +4 -3
- package/src/active-context.d.ts +30 -0
- package/src/active-context.d.ts.map +1 -0
- package/src/active-context.js +3 -0
- package/src/active-context.js.map +1 -0
- package/src/component-definition.d.ts +12 -0
- package/src/component-definition.d.ts.map +1 -0
- package/src/component-definition.js +3 -0
- package/src/component-definition.js.map +1 -0
- package/src/component-registration.d.ts +10 -0
- package/src/component-registration.d.ts.map +1 -0
- package/src/component-registration.js +3 -0
- package/src/component-registration.js.map +1 -0
- package/src/component-registry.d.ts +69 -0
- package/src/component-registry.d.ts.map +1 -0
- package/src/component-registry.js +248 -0
- package/src/component-registry.js.map +1 -0
- package/src/context-error-type.d.ts +4 -0
- package/src/context-error-type.d.ts.map +1 -0
- package/src/context-error-type.js +8 -0
- package/src/context-error-type.js.map +1 -0
- package/src/context-error.d.ts +7 -0
- package/src/context-error.d.ts.map +1 -0
- package/src/context-error.js +18 -0
- package/src/context-error.js.map +1 -0
- package/src/context-manager.d.ts +34 -0
- package/src/context-manager.d.ts.map +1 -0
- package/src/context-manager.js +60 -0
- package/src/context-manager.js.map +1 -0
- package/src/context.d.ts +45 -0
- package/src/context.d.ts.map +1 -0
- package/src/context.js +70 -0
- package/src/context.js.map +1 -0
- package/src/core-i18n.d.ts +63 -0
- package/src/core-i18n.d.ts.map +1 -0
- package/src/core-i18n.js +478 -0
- package/src/core-i18n.js.map +1 -0
- package/src/core-string-key.d.ts +43 -0
- package/src/core-string-key.d.ts.map +1 -0
- package/src/core-string-key.js +51 -0
- package/src/core-string-key.js.map +1 -0
- package/src/create-translation-adapter.d.ts +21 -0
- package/src/create-translation-adapter.d.ts.map +1 -0
- package/src/create-translation-adapter.js +37 -0
- package/src/create-translation-adapter.js.map +1 -0
- package/src/currency-code.d.ts +20 -0
- package/src/currency-code.d.ts.map +1 -0
- package/src/currency-code.js +37 -0
- package/src/currency-code.js.map +1 -0
- package/src/{currency-format.ts → currency-format.d.ts} +5 -5
- package/src/currency-format.d.ts.map +1 -0
- package/src/currency-format.js +3 -0
- package/src/currency-format.js.map +1 -0
- package/src/currency.d.ts +12 -0
- package/src/currency.d.ts.map +1 -0
- package/src/currency.js +49 -0
- package/src/currency.js.map +1 -0
- package/src/default-config.d.ts +33 -0
- package/src/default-config.d.ts.map +1 -0
- package/src/default-config.js +102 -0
- package/src/default-config.js.map +1 -0
- package/src/enum-registry.d.ts +45 -0
- package/src/enum-registry.d.ts.map +1 -0
- package/src/enum-registry.js +103 -0
- package/src/enum-registry.js.map +1 -0
- package/src/global-active-context.d.ts +51 -0
- package/src/global-active-context.d.ts.map +1 -0
- package/src/global-active-context.js +179 -0
- package/src/global-active-context.js.map +1 -0
- package/src/handleable.d.ts +14 -0
- package/src/handleable.d.ts.map +1 -0
- package/src/handleable.js +66 -0
- package/src/handleable.js.map +1 -0
- package/src/i-global-active-context.d.ts +23 -0
- package/src/i-global-active-context.d.ts.map +1 -0
- package/src/i-global-active-context.js +3 -0
- package/src/i-global-active-context.js.map +1 -0
- package/src/i-handleable-error-options.d.ts +7 -0
- package/src/i-handleable-error-options.d.ts.map +1 -0
- package/src/i-handleable-error-options.js +3 -0
- package/src/i-handleable-error-options.js.map +1 -0
- package/src/i-handleable.d.ts +6 -0
- package/src/i-handleable.d.ts.map +1 -0
- package/src/i-handleable.js +3 -0
- package/src/i-handleable.js.map +1 -0
- package/src/i18n-config.d.ts +21 -0
- package/src/i18n-config.d.ts.map +1 -0
- package/src/i18n-config.js +3 -0
- package/src/i18n-config.js.map +1 -0
- package/src/{i18n-context.ts → i18n-context.d.ts} +7 -7
- package/src/i18n-context.d.ts.map +1 -0
- package/src/i18n-context.js +3 -0
- package/src/i18n-context.js.map +1 -0
- package/src/i18n-engine.d.ts +179 -0
- package/src/i18n-engine.d.ts.map +1 -0
- package/src/i18n-engine.js +355 -0
- package/src/i18n-engine.js.map +1 -0
- package/src/{index.ts → index.d.ts} +2 -10
- package/src/index.d.ts.map +1 -0
- package/src/index.js +71 -0
- package/src/index.js.map +1 -0
- package/src/{language-codes.ts → language-codes.d.ts} +12 -23
- package/src/language-codes.d.ts.map +1 -0
- package/src/language-codes.js +32 -0
- package/src/language-codes.js.map +1 -0
- package/src/language-definition.d.ts +14 -0
- package/src/language-definition.d.ts.map +1 -0
- package/src/language-definition.js +3 -0
- package/src/language-definition.js.map +1 -0
- package/src/language-registry.d.ts +114 -0
- package/src/language-registry.d.ts.map +1 -0
- package/src/language-registry.js +217 -0
- package/src/language-registry.js.map +1 -0
- package/src/plugin-i18n-engine.d.ts +147 -0
- package/src/plugin-i18n-engine.d.ts.map +1 -0
- package/src/plugin-i18n-engine.js +369 -0
- package/src/plugin-i18n-engine.js.map +1 -0
- package/src/plugin-translatable-generic-error.d.ts +30 -0
- package/src/plugin-translatable-generic-error.d.ts.map +1 -0
- package/src/plugin-translatable-generic-error.js +72 -0
- package/src/plugin-translatable-generic-error.js.map +1 -0
- package/src/plugin-translatable-handleable-generic.d.ts +29 -0
- package/src/plugin-translatable-handleable-generic.d.ts.map +1 -0
- package/src/plugin-translatable-handleable-generic.js +44 -0
- package/src/plugin-translatable-handleable-generic.js.map +1 -0
- package/src/plugin-typed-handleable.d.ts +15 -0
- package/src/plugin-typed-handleable.d.ts.map +1 -0
- package/src/plugin-typed-handleable.js +51 -0
- package/src/plugin-typed-handleable.js.map +1 -0
- package/src/registry-config.d.ts +15 -0
- package/src/registry-config.d.ts.map +1 -0
- package/src/registry-config.js +3 -0
- package/src/registry-config.js.map +1 -0
- package/src/registry-error-type.d.ts +13 -0
- package/src/registry-error-type.d.ts.map +1 -0
- package/src/registry-error-type.js +17 -0
- package/src/registry-error-type.js.map +1 -0
- package/src/registry-error.d.ts +19 -0
- package/src/registry-error.d.ts.map +1 -0
- package/src/registry-error.js +48 -0
- package/src/registry-error.js.map +1 -0
- package/src/strict-types.d.ts +19 -0
- package/src/strict-types.d.ts.map +1 -0
- package/src/strict-types.js +18 -0
- package/src/strict-types.js.map +1 -0
- package/src/template.d.ts +13 -0
- package/src/template.d.ts.map +1 -0
- package/src/template.js +31 -0
- package/src/template.js.map +1 -0
- package/src/timezone.d.ts +12 -0
- package/src/timezone.d.ts.map +1 -0
- package/src/timezone.js +24 -0
- package/src/timezone.js.map +1 -0
- package/src/translatable.d.ts +6 -0
- package/src/translatable.d.ts.map +1 -0
- package/src/translatable.js +12 -0
- package/src/translatable.js.map +1 -0
- package/src/translation-engine.d.ts +9 -0
- package/src/translation-engine.d.ts.map +1 -0
- package/src/translation-engine.js +3 -0
- package/src/translation-engine.js.map +1 -0
- package/src/translation-request.d.ts +10 -0
- package/src/translation-request.d.ts.map +1 -0
- package/src/translation-request.js +3 -0
- package/src/translation-request.js.map +1 -0
- package/src/translation-response.d.ts +9 -0
- package/src/translation-response.d.ts.map +1 -0
- package/src/translation-response.js +3 -0
- package/src/translation-response.js.map +1 -0
- package/src/typed-error.d.ts +73 -0
- package/src/typed-error.d.ts.map +1 -0
- package/src/typed-error.js +267 -0
- package/src/typed-error.js.map +1 -0
- package/src/typed-handleable.d.ts +15 -0
- package/src/typed-handleable.d.ts.map +1 -0
- package/src/typed-handleable.js +46 -0
- package/src/typed-handleable.js.map +1 -0
- package/src/{types.ts → types.d.ts} +21 -75
- package/src/types.d.ts.map +1 -0
- package/src/types.js +19 -0
- package/src/types.js.map +1 -0
- package/src/unified-translator.d.ts +31 -0
- package/src/unified-translator.d.ts.map +1 -0
- package/src/unified-translator.js +71 -0
- package/src/unified-translator.js.map +1 -0
- package/src/utils.d.ts +65 -0
- package/src/utils.d.ts.map +1 -0
- package/src/utils.js +129 -0
- package/src/utils.js.map +1 -0
- package/src/validation-config.d.ts +12 -0
- package/src/validation-config.d.ts.map +1 -0
- package/src/validation-config.js +3 -0
- package/src/validation-config.js.map +1 -0
- package/src/validation-result.d.ts +13 -0
- package/src/validation-result.d.ts.map +1 -0
- package/src/validation-result.js +3 -0
- package/src/validation-result.js.map +1 -0
- package/LICENSE +0 -21
- package/src/active-context.ts +0 -30
- package/src/component-definition.ts +0 -11
- package/src/component-registration.ts +0 -13
- package/src/component-registry.ts +0 -392
- package/src/context-error-type.ts +0 -3
- package/src/context-error.ts +0 -16
- package/src/context-manager.ts +0 -71
- package/src/context.ts +0 -90
- package/src/core-i18n.ts +0 -609
- package/src/core-string-key.ts +0 -49
- package/src/create-translation-adapter.ts +0 -47
- package/src/currency-code.ts +0 -35
- package/src/currency.ts +0 -52
- package/src/default-config.ts +0 -199
- package/src/enum-registry.ts +0 -138
- package/src/global-active-context.ts +0 -255
- package/src/handleable.ts +0 -79
- package/src/i-global-active-context.ts +0 -59
- package/src/i-handleable-error-options.ts +0 -6
- package/src/i-handleable.ts +0 -5
- package/src/i18n-config.ts +0 -29
- package/src/i18n-engine.ts +0 -491
- package/src/language-definition.ts +0 -13
- package/src/language-registry.ts +0 -292
- package/src/plugin-i18n-engine.ts +0 -520
- package/src/plugin-translatable-generic-error.ts +0 -106
- package/src/plugin-translatable-handleable-generic.ts +0 -60
- package/src/plugin-typed-handleable.ts +0 -77
- package/src/registry-config.ts +0 -15
- package/src/registry-error-type.ts +0 -12
- package/src/registry-error.ts +0 -74
- package/src/strict-types.ts +0 -35
- package/src/template.ts +0 -63
- package/src/timezone.ts +0 -20
- package/src/translatable.ts +0 -15
- package/src/translation-engine.ts +0 -8
- package/src/translation-request.ts +0 -12
- package/src/translation-response.ts +0 -8
- package/src/typed-error.ts +0 -384
- package/src/typed-handleable.ts +0 -70
- package/src/unified-translator.ts +0 -96
- package/src/utils.ts +0 -213
- package/src/validation-config.ts +0 -11
- package/src/validation-result.ts +0 -12
package/src/typed-error.ts
DELETED
|
@@ -1,384 +0,0 @@
|
|
|
1
|
-
// Legacy imports (for backward compatibility)
|
|
2
|
-
import { DefaultStringKey, Language, StringKey } from './default-config';
|
|
3
|
-
import { I18nEngine } from './i18n-engine';
|
|
4
|
-
|
|
5
|
-
// New plugin architecture imports
|
|
6
|
-
// CoreLanguageCode is deprecated - using string for flexibility
|
|
7
|
-
import { CoreStringKey } from './core-string-key';
|
|
8
|
-
import { PluginI18nEngine } from './plugin-i18n-engine';
|
|
9
|
-
import { TranslationEngine } from './translation-engine';
|
|
10
|
-
|
|
11
|
-
export type { TranslationEngine };
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Type constraint to ensure reasonMap has entries for all enum values
|
|
15
|
-
*/
|
|
16
|
-
export type CompleteReasonMap<
|
|
17
|
-
TEnum extends Record<string, string | number>,
|
|
18
|
-
TStringKey extends string,
|
|
19
|
-
> = Record<TEnum[keyof TEnum], TStringKey>;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Base typed error class with common patterns
|
|
23
|
-
*/
|
|
24
|
-
export abstract class BaseTypedError<
|
|
25
|
-
TEnum extends Record<string, string>,
|
|
26
|
-
> extends Error {
|
|
27
|
-
constructor(
|
|
28
|
-
public readonly type: TEnum[keyof TEnum],
|
|
29
|
-
message: string,
|
|
30
|
-
public readonly metadata?: Record<string, any>,
|
|
31
|
-
) {
|
|
32
|
-
super(message);
|
|
33
|
-
this.name = this.constructor.name;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Create a simple typed error without engine dependency
|
|
38
|
-
*/
|
|
39
|
-
static createSimple<
|
|
40
|
-
TEnum extends Record<string, string>,
|
|
41
|
-
TError extends BaseTypedError<TEnum>,
|
|
42
|
-
>(
|
|
43
|
-
this: new (
|
|
44
|
-
type: TEnum[keyof TEnum],
|
|
45
|
-
message: string,
|
|
46
|
-
metadata?: Record<string, any>,
|
|
47
|
-
) => TError,
|
|
48
|
-
type: TEnum[keyof TEnum],
|
|
49
|
-
message: string,
|
|
50
|
-
metadata?: Record<string, any>,
|
|
51
|
-
): TError {
|
|
52
|
-
return new this(type, message, metadata);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Create a typed error with translation support
|
|
57
|
-
*/
|
|
58
|
-
static createTranslated<
|
|
59
|
-
TEnum extends Record<string, string>,
|
|
60
|
-
TStringKey extends string,
|
|
61
|
-
TError extends BaseTypedError<TEnum>,
|
|
62
|
-
>(
|
|
63
|
-
this: new (
|
|
64
|
-
type: TEnum[keyof TEnum],
|
|
65
|
-
message: string,
|
|
66
|
-
metadata?: Record<string, any>,
|
|
67
|
-
) => TError,
|
|
68
|
-
engine: TranslationEngine,
|
|
69
|
-
type: TEnum[keyof TEnum],
|
|
70
|
-
reasonMap: CompleteReasonMap<TEnum, TStringKey>,
|
|
71
|
-
variables?: Record<string, string | number>,
|
|
72
|
-
language?: string,
|
|
73
|
-
metadata?: Record<string, any>,
|
|
74
|
-
): TError {
|
|
75
|
-
const key = reasonMap[type];
|
|
76
|
-
let message: string;
|
|
77
|
-
|
|
78
|
-
if (key && engine) {
|
|
79
|
-
// Try to translate the error message using the engine
|
|
80
|
-
message = engine.safeTranslate(key, variables, language);
|
|
81
|
-
} else {
|
|
82
|
-
// Fallback to a basic English message
|
|
83
|
-
message = `Error: ${type}${
|
|
84
|
-
metadata ? ` - ${JSON.stringify(metadata)}` : ''
|
|
85
|
-
}`;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return new this(type, message, metadata);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Legacy TypedError that ensures complete enum coverage (for backward compatibility)
|
|
94
|
-
*/
|
|
95
|
-
export abstract class TypedError<
|
|
96
|
-
TEnum extends Record<string, string>,
|
|
97
|
-
TStringKey extends string = StringKey,
|
|
98
|
-
> extends Error {
|
|
99
|
-
constructor(
|
|
100
|
-
engine: I18nEngine<TStringKey, Language, Record<string, any>>,
|
|
101
|
-
public readonly type: TEnum[keyof TEnum],
|
|
102
|
-
public readonly reasonMap: CompleteReasonMap<TEnum, TStringKey>,
|
|
103
|
-
public readonly language?: Language,
|
|
104
|
-
public readonly otherVars?: Record<string, string | number>,
|
|
105
|
-
) {
|
|
106
|
-
const key = reasonMap[type];
|
|
107
|
-
if (!key)
|
|
108
|
-
throw new Error(
|
|
109
|
-
engine.translate(
|
|
110
|
-
DefaultStringKey.Error_MissingTranslationKeyTemplate as any,
|
|
111
|
-
{ type },
|
|
112
|
-
language,
|
|
113
|
-
),
|
|
114
|
-
);
|
|
115
|
-
super(engine.translate(key, otherVars, language));
|
|
116
|
-
this.name = this.constructor.name;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Plugin-based TypedError that works with the new component registration system
|
|
122
|
-
*/
|
|
123
|
-
export abstract class PluginTypedError<
|
|
124
|
-
TEnum extends Record<string, string>,
|
|
125
|
-
TStringKey extends string,
|
|
126
|
-
TLanguages extends string = string,
|
|
127
|
-
> extends Error {
|
|
128
|
-
constructor(
|
|
129
|
-
engine: PluginI18nEngine<TLanguages>,
|
|
130
|
-
public readonly componentId: string,
|
|
131
|
-
public readonly type: TEnum[keyof TEnum],
|
|
132
|
-
public readonly reasonMap: CompleteReasonMap<TEnum, TStringKey>,
|
|
133
|
-
public readonly language?: TLanguages,
|
|
134
|
-
public readonly otherVars?: Record<string, string | number>,
|
|
135
|
-
) {
|
|
136
|
-
const key = reasonMap[type];
|
|
137
|
-
|
|
138
|
-
// If key is not found in the reason map, use core error message
|
|
139
|
-
if (!key) {
|
|
140
|
-
const errorMsg = engine.safeTranslate(
|
|
141
|
-
'core',
|
|
142
|
-
CoreStringKey.Error_StringKeyNotFoundTemplate,
|
|
143
|
-
{
|
|
144
|
-
stringKey: String(type),
|
|
145
|
-
componentId: componentId,
|
|
146
|
-
},
|
|
147
|
-
language,
|
|
148
|
-
);
|
|
149
|
-
throw new Error(errorMsg);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// Translate the error message using the component and string key
|
|
153
|
-
const translatedMessage = engine.safeTranslate(
|
|
154
|
-
componentId,
|
|
155
|
-
key,
|
|
156
|
-
otherVars,
|
|
157
|
-
language,
|
|
158
|
-
);
|
|
159
|
-
super(translatedMessage);
|
|
160
|
-
this.name = this.constructor.name;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Core system TypedError using the core component strings
|
|
166
|
-
*/
|
|
167
|
-
export abstract class CoreTypedError<
|
|
168
|
-
TEnum extends Record<string, string>,
|
|
169
|
-
> extends Error {
|
|
170
|
-
constructor(
|
|
171
|
-
engine: PluginI18nEngine<string>,
|
|
172
|
-
public readonly type: TEnum[keyof TEnum],
|
|
173
|
-
public readonly reasonMap: CompleteReasonMap<TEnum, CoreStringKey>,
|
|
174
|
-
public readonly language?: string,
|
|
175
|
-
public readonly otherVars?: Record<string, string | number>,
|
|
176
|
-
) {
|
|
177
|
-
const key = reasonMap[type];
|
|
178
|
-
|
|
179
|
-
// If key is not found in the reason map, use a fallback error
|
|
180
|
-
if (!key) {
|
|
181
|
-
const errorMsg = engine.safeTranslate(
|
|
182
|
-
'core',
|
|
183
|
-
CoreStringKey.Error_StringKeyNotFoundTemplate,
|
|
184
|
-
{
|
|
185
|
-
stringKey: String(type),
|
|
186
|
-
componentId: 'core',
|
|
187
|
-
},
|
|
188
|
-
language,
|
|
189
|
-
);
|
|
190
|
-
throw new Error(errorMsg);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// Translate the error message using the core component
|
|
194
|
-
const translatedMessage = engine.safeTranslate(
|
|
195
|
-
'core',
|
|
196
|
-
key,
|
|
197
|
-
otherVars,
|
|
198
|
-
language,
|
|
199
|
-
);
|
|
200
|
-
super(translatedMessage);
|
|
201
|
-
this.name = this.constructor.name;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Helper function to create a plugin-based TypedError with automatic engine detection
|
|
207
|
-
*/
|
|
208
|
-
export function createPluginTypedError<
|
|
209
|
-
TEnum extends Record<string, string>,
|
|
210
|
-
TStringKey extends string,
|
|
211
|
-
TLanguages extends string = string,
|
|
212
|
-
>(
|
|
213
|
-
componentId: string,
|
|
214
|
-
type: TEnum[keyof TEnum],
|
|
215
|
-
reasonMap: CompleteReasonMap<TEnum, TStringKey>,
|
|
216
|
-
otherVars?: Record<string, string | number>,
|
|
217
|
-
language?: TLanguages,
|
|
218
|
-
instanceKey?: string,
|
|
219
|
-
): Error {
|
|
220
|
-
const engine = PluginI18nEngine.getInstance<TLanguages>(instanceKey);
|
|
221
|
-
|
|
222
|
-
return new (class extends PluginTypedError<TEnum, TStringKey, TLanguages> {
|
|
223
|
-
constructor() {
|
|
224
|
-
super(engine, componentId, type, reasonMap, language, otherVars);
|
|
225
|
-
}
|
|
226
|
-
})();
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Helper function to create a core system TypedError with automatic engine detection
|
|
231
|
-
*/
|
|
232
|
-
export function createCoreTypedError<TEnum extends Record<string, string>>(
|
|
233
|
-
type: TEnum[keyof TEnum],
|
|
234
|
-
reasonMap: CompleteReasonMap<TEnum, CoreStringKey>,
|
|
235
|
-
otherVars?: Record<string, string | number>,
|
|
236
|
-
language?: string,
|
|
237
|
-
instanceKey?: string,
|
|
238
|
-
): Error {
|
|
239
|
-
const engine = PluginI18nEngine.getInstance<string>(instanceKey);
|
|
240
|
-
|
|
241
|
-
return new (class extends CoreTypedError<TEnum> {
|
|
242
|
-
constructor() {
|
|
243
|
-
super(engine, type, reasonMap, language, otherVars);
|
|
244
|
-
}
|
|
245
|
-
})();
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Create a simple error with translation support (generalized pattern from RegistryError)
|
|
250
|
-
*/
|
|
251
|
-
export function createTranslatedError<
|
|
252
|
-
TEnum extends Record<string, string>,
|
|
253
|
-
TStringKey extends string,
|
|
254
|
-
>(
|
|
255
|
-
engine: TranslationEngine,
|
|
256
|
-
componentId: string,
|
|
257
|
-
type: TEnum[keyof TEnum],
|
|
258
|
-
reasonMap: Record<TEnum[keyof TEnum], TStringKey>,
|
|
259
|
-
variables?: Record<string, string | number>,
|
|
260
|
-
language?: string,
|
|
261
|
-
metadata?: Record<string, any>,
|
|
262
|
-
errorName?: string,
|
|
263
|
-
): Error {
|
|
264
|
-
const key = reasonMap[type];
|
|
265
|
-
let message: string;
|
|
266
|
-
|
|
267
|
-
if (key && engine) {
|
|
268
|
-
try {
|
|
269
|
-
// Try to translate the error message using the engine
|
|
270
|
-
message = engine.safeTranslate(key, variables, language);
|
|
271
|
-
} catch (translationError) {
|
|
272
|
-
// Fallback if translation fails
|
|
273
|
-
message = `Error: ${type}${
|
|
274
|
-
metadata ? ` - ${JSON.stringify(metadata)}` : ''
|
|
275
|
-
}`;
|
|
276
|
-
}
|
|
277
|
-
} else {
|
|
278
|
-
// Fallback to a basic English message
|
|
279
|
-
message = `Error: ${type}${
|
|
280
|
-
metadata ? ` - ${JSON.stringify(metadata)}` : ''
|
|
281
|
-
}`;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
const error = new Error(message);
|
|
285
|
-
error.name = errorName || 'TranslatedError';
|
|
286
|
-
(error as any).type = type;
|
|
287
|
-
(error as any).metadata = metadata;
|
|
288
|
-
|
|
289
|
-
return error;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* Example usage of the new plugin-based TypedError system
|
|
294
|
-
*/
|
|
295
|
-
|
|
296
|
-
// Example 1: Core system error using CoreStringKey
|
|
297
|
-
/*
|
|
298
|
-
enum DatabaseErrorType {
|
|
299
|
-
ConnectionFailed = 'connectionFailed',
|
|
300
|
-
QueryTimeout = 'queryTimeout',
|
|
301
|
-
AccessDenied = 'accessDenied'
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
const coreErrorReasonMap: CompleteReasonMap<typeof DatabaseErrorType, CoreStringKey> = {
|
|
305
|
-
[DatabaseErrorType.ConnectionFailed]: CoreStringKey.Error_NetworkError,
|
|
306
|
-
[DatabaseErrorType.QueryTimeout]: CoreStringKey.Error_InternalServer,
|
|
307
|
-
[DatabaseErrorType.AccessDenied]: CoreStringKey.Error_AccessDenied
|
|
308
|
-
};
|
|
309
|
-
|
|
310
|
-
class DatabaseError extends CoreTypedError<typeof DatabaseErrorType> {
|
|
311
|
-
constructor(
|
|
312
|
-
engine: PluginI18nEngine<CoreLanguageCode>,
|
|
313
|
-
type: DatabaseErrorType,
|
|
314
|
-
otherVars?: Record<string, string | number>,
|
|
315
|
-
language?: CoreLanguage
|
|
316
|
-
) {
|
|
317
|
-
super(engine, type, coreErrorReasonMap, language, otherVars);
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
// Usage:
|
|
322
|
-
// const engine = PluginI18nEngine.getInstance<string>();
|
|
323
|
-
// throw new DatabaseError(engine, DatabaseErrorType.ConnectionFailed);
|
|
324
|
-
*/
|
|
325
|
-
|
|
326
|
-
// Example 2: Custom component error with custom strings
|
|
327
|
-
/*
|
|
328
|
-
enum UserErrorType {
|
|
329
|
-
UserNotFound = 'userNotFound',
|
|
330
|
-
InvalidCredentials = 'invalidCredentials',
|
|
331
|
-
AccountLocked = 'accountLocked'
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
enum UserErrorStringKey {
|
|
335
|
-
UserNotFoundMessage = 'userNotFoundMessage',
|
|
336
|
-
InvalidCredentialsMessage = 'invalidCredentialsMessage',
|
|
337
|
-
AccountLockedMessage = 'accountLockedMessage'
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
const userErrorReasonMap: CompleteReasonMap<typeof UserErrorType, UserErrorStringKey> = {
|
|
341
|
-
[UserErrorType.UserNotFound]: UserErrorStringKey.UserNotFoundMessage,
|
|
342
|
-
[UserErrorType.InvalidCredentials]: UserErrorStringKey.InvalidCredentialsMessage,
|
|
343
|
-
[UserErrorType.AccountLocked]: UserErrorStringKey.AccountLockedMessage
|
|
344
|
-
};
|
|
345
|
-
|
|
346
|
-
class UserError extends PluginTypedError<typeof UserErrorType, UserErrorStringKey, string> {
|
|
347
|
-
constructor(
|
|
348
|
-
engine: PluginI18nEngine<string>,
|
|
349
|
-
type: UserErrorType,
|
|
350
|
-
otherVars?: Record<string, string | number>,
|
|
351
|
-
language?: string
|
|
352
|
-
) {
|
|
353
|
-
super(engine, 'user-system', type, userErrorReasonMap, language, otherVars);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
// Usage:
|
|
358
|
-
// const engine = PluginI18nEngine.getInstance<string>();
|
|
359
|
-
// throw new UserError(engine, UserErrorType.UserNotFound, { username: 'john_doe' });
|
|
360
|
-
*/
|
|
361
|
-
|
|
362
|
-
// Example 3: Using helper functions for simpler error creation
|
|
363
|
-
/*
|
|
364
|
-
// Define your error types and mappings
|
|
365
|
-
enum ApiErrorType {
|
|
366
|
-
Timeout = 'timeout',
|
|
367
|
-
NotFound = 'notFound'
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
const apiErrorMap: CompleteReasonMap<typeof ApiErrorType, CoreStringKey> = {
|
|
371
|
-
[ApiErrorType.Timeout]: CoreStringKey.Error_NetworkError,
|
|
372
|
-
[ApiErrorType.NotFound]: CoreStringKey.Error_NotFound
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
// Create errors using helper functions
|
|
376
|
-
function throwApiError(type: ApiErrorType, vars?: Record<string, string | number>) {
|
|
377
|
-
throw createCoreTypedError(type, apiErrorMap, vars);
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
// Usage:
|
|
381
|
-
// throwApiError(ApiErrorType.NotFound, { resource: 'user' });
|
|
382
|
-
*/
|
|
383
|
-
|
|
384
|
-
// Export the type for external use (already exported above)
|
package/src/typed-handleable.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { CoreLanguageCode } from './core-i18n';
|
|
2
|
-
import { CoreStringKey } from './core-string-key';
|
|
3
|
-
import { HandleableError } from './handleable';
|
|
4
|
-
import { IHandleable } from './i-handleable';
|
|
5
|
-
import { HandleableErrorOptions } from './i-handleable-error-options';
|
|
6
|
-
import { PluginI18nEngine } from './plugin-i18n-engine';
|
|
7
|
-
import { CompleteReasonMap, TranslationEngine } from './typed-error';
|
|
8
|
-
|
|
9
|
-
export class TypedHandleableError<
|
|
10
|
-
TEnum extends Record<string, string>,
|
|
11
|
-
TStringKey extends string,
|
|
12
|
-
TLanguage extends CoreLanguageCode = CoreLanguageCode,
|
|
13
|
-
>
|
|
14
|
-
extends HandleableError
|
|
15
|
-
implements IHandleable
|
|
16
|
-
{
|
|
17
|
-
public readonly type: TEnum[keyof TEnum];
|
|
18
|
-
public readonly reasonMap: CompleteReasonMap<TEnum, TStringKey>;
|
|
19
|
-
public readonly engine: TranslationEngine<TStringKey>;
|
|
20
|
-
public readonly language?: TLanguage;
|
|
21
|
-
public readonly otherVars?: Record<string, string | number>;
|
|
22
|
-
|
|
23
|
-
constructor(
|
|
24
|
-
type: TEnum[keyof TEnum],
|
|
25
|
-
reasonMap: CompleteReasonMap<TEnum, TStringKey>,
|
|
26
|
-
engine: TranslationEngine<TStringKey>,
|
|
27
|
-
language?: TLanguage,
|
|
28
|
-
otherVars?: Record<string, string | number>,
|
|
29
|
-
options?: HandleableErrorOptions,
|
|
30
|
-
) {
|
|
31
|
-
const key = reasonMap[type];
|
|
32
|
-
if (!key) {
|
|
33
|
-
const coreEngine = PluginI18nEngine.getInstance<TLanguage>();
|
|
34
|
-
throw new Error(
|
|
35
|
-
coreEngine.translate(
|
|
36
|
-
'core',
|
|
37
|
-
CoreStringKey.Error_MissingTranslationKeyTemplate,
|
|
38
|
-
{
|
|
39
|
-
stringKey: key as string,
|
|
40
|
-
},
|
|
41
|
-
),
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
let message: string = String(type);
|
|
46
|
-
try {
|
|
47
|
-
const keyString = key as TStringKey;
|
|
48
|
-
const translated = engine.translate(keyString, otherVars, language);
|
|
49
|
-
message = String(translated || type);
|
|
50
|
-
} catch (error) {
|
|
51
|
-
message = String(type);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
super(new Error(message), options);
|
|
55
|
-
|
|
56
|
-
this.type = type;
|
|
57
|
-
this.reasonMap = reasonMap;
|
|
58
|
-
this.language = language;
|
|
59
|
-
this.otherVars = otherVars;
|
|
60
|
-
this.engine = engine;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public override toJSON(): Record<string, unknown> {
|
|
64
|
-
const baseJson = super.toJSON();
|
|
65
|
-
return {
|
|
66
|
-
...baseJson,
|
|
67
|
-
type: this.type,
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { PluginI18nEngine } from './plugin-i18n-engine';
|
|
2
|
-
import { I18nEngine } from './i18n-engine';
|
|
3
|
-
|
|
4
|
-
interface PluginSource {
|
|
5
|
-
type: 'plugin';
|
|
6
|
-
engine: PluginI18nEngine<any>;
|
|
7
|
-
componentId: string;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
interface LegacySource {
|
|
11
|
-
type: 'legacy';
|
|
12
|
-
engine: I18nEngine<any, any, any, any>;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
type TranslationSource = PluginSource | LegacySource;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Unified translator with explicit component/engine specification
|
|
19
|
-
*/
|
|
20
|
-
export class UnifiedTranslator<TLanguage extends string = string> {
|
|
21
|
-
private sources = new Map<string, TranslationSource>();
|
|
22
|
-
private defaultLanguage: TLanguage;
|
|
23
|
-
private defaultSource?: string;
|
|
24
|
-
|
|
25
|
-
constructor(defaultLanguage: TLanguage) {
|
|
26
|
-
this.defaultLanguage = defaultLanguage;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Register a plugin component
|
|
31
|
-
*/
|
|
32
|
-
registerPlugin(
|
|
33
|
-
id: string,
|
|
34
|
-
engine: PluginI18nEngine<any>,
|
|
35
|
-
componentId: string,
|
|
36
|
-
): void {
|
|
37
|
-
this.sources.set(id, { type: 'plugin', engine, componentId });
|
|
38
|
-
if (!this.defaultSource) this.defaultSource = id;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Register a legacy engine
|
|
43
|
-
*/
|
|
44
|
-
registerLegacy(id: string, engine: I18nEngine<any, any, any, any>): void {
|
|
45
|
-
this.sources.set(id, { type: 'legacy', engine });
|
|
46
|
-
if (!this.defaultSource) this.defaultSource = id;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Set default source for unqualified keys
|
|
51
|
-
*/
|
|
52
|
-
setDefaultSource(id: string): void {
|
|
53
|
-
if (!this.sources.has(id)) {
|
|
54
|
-
throw new Error(`Source '${id}' not registered`);
|
|
55
|
-
}
|
|
56
|
-
this.defaultSource = id;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Translate with explicit source: 'source:key' or just 'key' (uses default)
|
|
61
|
-
*/
|
|
62
|
-
translate(
|
|
63
|
-
key: string,
|
|
64
|
-
vars?: Record<string, string | number>,
|
|
65
|
-
language?: TLanguage,
|
|
66
|
-
): string {
|
|
67
|
-
const lang = language || this.defaultLanguage;
|
|
68
|
-
const [sourceName, actualKey] = key.includes(':')
|
|
69
|
-
? key.split(':', 2)
|
|
70
|
-
: [this.defaultSource, key];
|
|
71
|
-
|
|
72
|
-
if (!sourceName) return `[${key}]`;
|
|
73
|
-
|
|
74
|
-
const source = this.sources.get(sourceName);
|
|
75
|
-
if (!source) return `[${key}]`;
|
|
76
|
-
|
|
77
|
-
if (source.type === 'plugin') {
|
|
78
|
-
return source.engine.safeTranslate(source.componentId, actualKey, vars, lang);
|
|
79
|
-
} else {
|
|
80
|
-
return source.engine.safeTranslate(actualKey as any, vars, lang);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
setLanguage(language: TLanguage): void {
|
|
85
|
-
this.defaultLanguage = language;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
getLanguage(): TLanguage {
|
|
89
|
-
return this.defaultLanguage;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
clearSources(): void {
|
|
93
|
-
this.sources.clear();
|
|
94
|
-
this.defaultSource = undefined;
|
|
95
|
-
}
|
|
96
|
-
}
|