@digitaldefiance/i18n-lib 1.3.12 → 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.
- package/README.md +4 -0
- package/package.json +12 -27
- package/src/active-context.ts +30 -0
- package/src/component-definition.ts +11 -0
- package/src/component-registration.ts +13 -0
- package/src/component-registry.ts +392 -0
- package/src/context-error-type.ts +3 -0
- package/src/context-error.ts +16 -0
- package/src/context-manager.ts +71 -0
- package/src/context.ts +90 -0
- package/src/core-i18n.ts +609 -0
- package/src/core-string-key.ts +49 -0
- package/src/create-translation-adapter.ts +47 -0
- package/src/currency-code.ts +35 -0
- package/{dist/currency-format.d.ts → src/currency-format.ts} +5 -4
- package/src/currency.ts +52 -0
- package/src/default-config.ts +199 -0
- package/src/enum-registry.ts +138 -0
- package/src/global-active-context.ts +255 -0
- package/src/handleable.ts +79 -0
- package/src/i-global-active-context.ts +59 -0
- package/src/i-handleable-error-options.ts +6 -0
- package/src/i-handleable.ts +5 -0
- package/src/i18n-config.ts +29 -0
- package/{dist/i18n-context.d.ts → src/i18n-context.ts} +7 -6
- package/src/i18n-engine.ts +491 -0
- package/{dist/index.d.ts → src/index.ts} +10 -1
- package/{dist/language-codes.d.ts → src/language-codes.ts} +23 -11
- package/src/language-definition.ts +13 -0
- package/src/language-registry.ts +292 -0
- package/src/plugin-i18n-engine.ts +520 -0
- package/src/plugin-translatable-generic-error.ts +106 -0
- package/src/plugin-translatable-handleable-generic.ts +60 -0
- package/src/plugin-typed-handleable.ts +77 -0
- package/src/registry-config.ts +15 -0
- package/src/registry-error-type.ts +12 -0
- package/src/registry-error.ts +74 -0
- package/src/strict-types.ts +35 -0
- package/src/template.ts +63 -0
- package/src/timezone.ts +20 -0
- package/src/translatable.ts +15 -0
- package/src/translation-engine.ts +8 -0
- package/src/translation-request.ts +12 -0
- package/src/translation-response.ts +8 -0
- package/src/typed-error.ts +384 -0
- package/src/typed-handleable.ts +70 -0
- package/{dist/types.d.ts → src/types.ts} +75 -20
- package/src/unified-translator.ts +96 -0
- package/src/utils.ts +213 -0
- package/src/validation-config.ts +11 -0
- package/src/validation-result.ts +12 -0
- package/dist/active-context.d.ts +0 -29
- package/dist/active-context.js +0 -2
- package/dist/component-definition.d.ts +0 -11
- package/dist/component-definition.js +0 -2
- package/dist/component-registration.d.ts +0 -9
- package/dist/component-registration.js +0 -2
- package/dist/component-registry.d.ts +0 -68
- package/dist/component-registry.js +0 -245
- package/dist/context-error-type.d.ts +0 -3
- package/dist/context-error-type.js +0 -7
- package/dist/context-error.d.ts +0 -6
- package/dist/context-error.js +0 -15
- package/dist/context-manager.d.ts +0 -33
- package/dist/context-manager.js +0 -61
- package/dist/context.d.ts +0 -44
- package/dist/context.js +0 -69
- package/dist/core-i18n.d.ts +0 -62
- package/dist/core-i18n.js +0 -477
- package/dist/core-string-key.d.ts +0 -42
- package/dist/core-string-key.js +0 -50
- package/dist/create-translation-adapter.d.ts +0 -20
- package/dist/create-translation-adapter.js +0 -36
- package/dist/currency-code.d.ts +0 -19
- package/dist/currency-code.js +0 -36
- package/dist/currency-format.js +0 -2
- package/dist/currency.d.ts +0 -11
- package/dist/currency.js +0 -48
- package/dist/default-config.d.ts +0 -32
- package/dist/default-config.js +0 -101
- package/dist/enum-registry.d.ts +0 -44
- package/dist/enum-registry.js +0 -100
- package/dist/global-active-context.d.ts +0 -50
- package/dist/global-active-context.js +0 -177
- package/dist/handleable.d.ts +0 -13
- package/dist/handleable.js +0 -56
- package/dist/i-global-active-context.d.ts +0 -22
- package/dist/i-global-active-context.js +0 -2
- package/dist/i-handleable-error-options.d.ts +0 -6
- package/dist/i-handleable-error-options.js +0 -2
- package/dist/i-handleable.d.ts +0 -5
- package/dist/i-handleable.js +0 -2
- package/dist/i18n-config.d.ts +0 -20
- package/dist/i18n-config.js +0 -2
- package/dist/i18n-context.js +0 -2
- package/dist/i18n-engine.d.ts +0 -178
- package/dist/i18n-engine.js +0 -338
- package/dist/index.js +0 -83
- package/dist/language-codes.js +0 -31
- package/dist/language-definition.d.ts +0 -13
- package/dist/language-definition.js +0 -2
- package/dist/language-registry.d.ts +0 -113
- package/dist/language-registry.js +0 -216
- package/dist/plugin-i18n-engine.d.ts +0 -146
- package/dist/plugin-i18n-engine.js +0 -360
- package/dist/plugin-translatable-generic-error.d.ts +0 -29
- package/dist/plugin-translatable-generic-error.js +0 -66
- package/dist/plugin-translatable-handleable-generic.d.ts +0 -28
- package/dist/plugin-translatable-handleable-generic.js +0 -40
- package/dist/plugin-typed-handleable.d.ts +0 -14
- package/dist/plugin-typed-handleable.js +0 -45
- package/dist/registry-config.d.ts +0 -14
- package/dist/registry-config.js +0 -2
- package/dist/registry-error-type.d.ts +0 -12
- package/dist/registry-error-type.js +0 -16
- package/dist/registry-error.d.ts +0 -18
- package/dist/registry-error.js +0 -45
- package/dist/strict-types.d.ts +0 -18
- package/dist/strict-types.js +0 -17
- package/dist/template.d.ts +0 -12
- package/dist/template.js +0 -30
- package/dist/timezone.d.ts +0 -11
- package/dist/timezone.js +0 -22
- package/dist/translatable-generic-error.d.ts +0 -29
- package/dist/translatable-generic-error.js +0 -66
- package/dist/translatable-handleable-generic.d.ts +0 -28
- package/dist/translatable-handleable-generic.js +0 -40
- package/dist/translatable.d.ts +0 -5
- package/dist/translatable.js +0 -11
- package/dist/translation-engine.d.ts +0 -8
- package/dist/translation-engine.js +0 -2
- package/dist/translation-request.d.ts +0 -9
- package/dist/translation-request.js +0 -2
- package/dist/translation-response.d.ts +0 -8
- package/dist/translation-response.js +0 -2
- package/dist/typed-error.d.ts +0 -72
- package/dist/typed-error.js +0 -251
- package/dist/typed-handleable.d.ts +0 -14
- package/dist/typed-handleable.js +0 -40
- package/dist/types.js +0 -18
- package/dist/unified-translator.d.ts +0 -30
- package/dist/unified-translator.js +0 -68
- package/dist/utils.d.ts +0 -64
- package/dist/utils.js +0 -130
- package/dist/validation-config.d.ts +0 -11
- package/dist/validation-config.js +0 -2
- package/dist/validation-result.d.ts +0 -12
- package/dist/validation-result.js +0 -2
package/dist/typed-error.js
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CoreTypedError = exports.PluginTypedError = exports.TypedError = exports.BaseTypedError = void 0;
|
|
4
|
-
exports.createPluginTypedError = createPluginTypedError;
|
|
5
|
-
exports.createCoreTypedError = createCoreTypedError;
|
|
6
|
-
exports.createTranslatedError = createTranslatedError;
|
|
7
|
-
// Legacy imports (for backward compatibility)
|
|
8
|
-
const default_config_1 = require("./default-config");
|
|
9
|
-
// New plugin architecture imports
|
|
10
|
-
// CoreLanguageCode is deprecated - using string for flexibility
|
|
11
|
-
const core_string_key_1 = require("./core-string-key");
|
|
12
|
-
const plugin_i18n_engine_1 = require("./plugin-i18n-engine");
|
|
13
|
-
/**
|
|
14
|
-
* Base typed error class with common patterns
|
|
15
|
-
*/
|
|
16
|
-
class BaseTypedError extends Error {
|
|
17
|
-
constructor(type, message, metadata) {
|
|
18
|
-
super(message);
|
|
19
|
-
this.type = type;
|
|
20
|
-
this.metadata = metadata;
|
|
21
|
-
this.name = this.constructor.name;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Create a simple typed error without engine dependency
|
|
25
|
-
*/
|
|
26
|
-
static createSimple(type, message, metadata) {
|
|
27
|
-
return new this(type, message, metadata);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Create a typed error with translation support
|
|
31
|
-
*/
|
|
32
|
-
static createTranslated(engine, type, reasonMap, variables, language, metadata) {
|
|
33
|
-
const key = reasonMap[type];
|
|
34
|
-
let message;
|
|
35
|
-
if (key && engine) {
|
|
36
|
-
// Try to translate the error message using the engine
|
|
37
|
-
message = engine.safeTranslate(key, variables, language);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
// Fallback to a basic English message
|
|
41
|
-
message = `Error: ${type}${metadata ? ` - ${JSON.stringify(metadata)}` : ''}`;
|
|
42
|
-
}
|
|
43
|
-
return new this(type, message, metadata);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
exports.BaseTypedError = BaseTypedError;
|
|
47
|
-
/**
|
|
48
|
-
* Legacy TypedError that ensures complete enum coverage (for backward compatibility)
|
|
49
|
-
*/
|
|
50
|
-
class TypedError extends Error {
|
|
51
|
-
constructor(engine, type, reasonMap, language, otherVars) {
|
|
52
|
-
const key = reasonMap[type];
|
|
53
|
-
if (!key)
|
|
54
|
-
throw new Error(engine.translate(default_config_1.DefaultStringKey.Error_MissingTranslationKeyTemplate, { type }, language));
|
|
55
|
-
super(engine.translate(key, otherVars, language));
|
|
56
|
-
this.type = type;
|
|
57
|
-
this.reasonMap = reasonMap;
|
|
58
|
-
this.language = language;
|
|
59
|
-
this.otherVars = otherVars;
|
|
60
|
-
this.name = this.constructor.name;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
exports.TypedError = TypedError;
|
|
64
|
-
/**
|
|
65
|
-
* Plugin-based TypedError that works with the new component registration system
|
|
66
|
-
*/
|
|
67
|
-
class PluginTypedError extends Error {
|
|
68
|
-
constructor(engine, componentId, type, reasonMap, language, otherVars) {
|
|
69
|
-
const key = reasonMap[type];
|
|
70
|
-
// If key is not found in the reason map, use core error message
|
|
71
|
-
if (!key) {
|
|
72
|
-
const errorMsg = engine.safeTranslate('core', core_string_key_1.CoreStringKey.Error_StringKeyNotFoundTemplate, {
|
|
73
|
-
stringKey: String(type),
|
|
74
|
-
componentId: componentId,
|
|
75
|
-
}, language);
|
|
76
|
-
throw new Error(errorMsg);
|
|
77
|
-
}
|
|
78
|
-
// Translate the error message using the component and string key
|
|
79
|
-
const translatedMessage = engine.safeTranslate(componentId, key, otherVars, language);
|
|
80
|
-
super(translatedMessage);
|
|
81
|
-
this.componentId = componentId;
|
|
82
|
-
this.type = type;
|
|
83
|
-
this.reasonMap = reasonMap;
|
|
84
|
-
this.language = language;
|
|
85
|
-
this.otherVars = otherVars;
|
|
86
|
-
this.name = this.constructor.name;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
exports.PluginTypedError = PluginTypedError;
|
|
90
|
-
/**
|
|
91
|
-
* Core system TypedError using the core component strings
|
|
92
|
-
*/
|
|
93
|
-
class CoreTypedError extends Error {
|
|
94
|
-
constructor(engine, type, reasonMap, language, otherVars) {
|
|
95
|
-
const key = reasonMap[type];
|
|
96
|
-
// If key is not found in the reason map, use a fallback error
|
|
97
|
-
if (!key) {
|
|
98
|
-
const errorMsg = engine.safeTranslate('core', core_string_key_1.CoreStringKey.Error_StringKeyNotFoundTemplate, {
|
|
99
|
-
stringKey: String(type),
|
|
100
|
-
componentId: 'core',
|
|
101
|
-
}, language);
|
|
102
|
-
throw new Error(errorMsg);
|
|
103
|
-
}
|
|
104
|
-
// Translate the error message using the core component
|
|
105
|
-
const translatedMessage = engine.safeTranslate('core', key, otherVars, language);
|
|
106
|
-
super(translatedMessage);
|
|
107
|
-
this.type = type;
|
|
108
|
-
this.reasonMap = reasonMap;
|
|
109
|
-
this.language = language;
|
|
110
|
-
this.otherVars = otherVars;
|
|
111
|
-
this.name = this.constructor.name;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
exports.CoreTypedError = CoreTypedError;
|
|
115
|
-
/**
|
|
116
|
-
* Helper function to create a plugin-based TypedError with automatic engine detection
|
|
117
|
-
*/
|
|
118
|
-
function createPluginTypedError(componentId, type, reasonMap, otherVars, language, instanceKey) {
|
|
119
|
-
const engine = plugin_i18n_engine_1.PluginI18nEngine.getInstance(instanceKey);
|
|
120
|
-
return new (class extends PluginTypedError {
|
|
121
|
-
constructor() {
|
|
122
|
-
super(engine, componentId, type, reasonMap, language, otherVars);
|
|
123
|
-
}
|
|
124
|
-
})();
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Helper function to create a core system TypedError with automatic engine detection
|
|
128
|
-
*/
|
|
129
|
-
function createCoreTypedError(type, reasonMap, otherVars, language, instanceKey) {
|
|
130
|
-
const engine = plugin_i18n_engine_1.PluginI18nEngine.getInstance(instanceKey);
|
|
131
|
-
return new (class extends CoreTypedError {
|
|
132
|
-
constructor() {
|
|
133
|
-
super(engine, type, reasonMap, language, otherVars);
|
|
134
|
-
}
|
|
135
|
-
})();
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Create a simple error with translation support (generalized pattern from RegistryError)
|
|
139
|
-
*/
|
|
140
|
-
function createTranslatedError(engine, componentId, type, reasonMap, variables, language, metadata, errorName) {
|
|
141
|
-
const key = reasonMap[type];
|
|
142
|
-
let message;
|
|
143
|
-
if (key && engine) {
|
|
144
|
-
try {
|
|
145
|
-
// Try to translate the error message using the engine
|
|
146
|
-
message = engine.safeTranslate(key, variables, language);
|
|
147
|
-
}
|
|
148
|
-
catch (translationError) {
|
|
149
|
-
// Fallback if translation fails
|
|
150
|
-
message = `Error: ${type}${metadata ? ` - ${JSON.stringify(metadata)}` : ''}`;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
// Fallback to a basic English message
|
|
155
|
-
message = `Error: ${type}${metadata ? ` - ${JSON.stringify(metadata)}` : ''}`;
|
|
156
|
-
}
|
|
157
|
-
const error = new Error(message);
|
|
158
|
-
error.name = errorName || 'TranslatedError';
|
|
159
|
-
error.type = type;
|
|
160
|
-
error.metadata = metadata;
|
|
161
|
-
return error;
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Example usage of the new plugin-based TypedError system
|
|
165
|
-
*/
|
|
166
|
-
// Example 1: Core system error using CoreStringKey
|
|
167
|
-
/*
|
|
168
|
-
enum DatabaseErrorType {
|
|
169
|
-
ConnectionFailed = 'connectionFailed',
|
|
170
|
-
QueryTimeout = 'queryTimeout',
|
|
171
|
-
AccessDenied = 'accessDenied'
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const coreErrorReasonMap: CompleteReasonMap<typeof DatabaseErrorType, CoreStringKey> = {
|
|
175
|
-
[DatabaseErrorType.ConnectionFailed]: CoreStringKey.Error_NetworkError,
|
|
176
|
-
[DatabaseErrorType.QueryTimeout]: CoreStringKey.Error_InternalServer,
|
|
177
|
-
[DatabaseErrorType.AccessDenied]: CoreStringKey.Error_AccessDenied
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
class DatabaseError extends CoreTypedError<typeof DatabaseErrorType> {
|
|
181
|
-
constructor(
|
|
182
|
-
engine: PluginI18nEngine<CoreLanguageCode>,
|
|
183
|
-
type: DatabaseErrorType,
|
|
184
|
-
otherVars?: Record<string, string | number>,
|
|
185
|
-
language?: CoreLanguage
|
|
186
|
-
) {
|
|
187
|
-
super(engine, type, coreErrorReasonMap, language, otherVars);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// Usage:
|
|
192
|
-
// const engine = PluginI18nEngine.getInstance<string>();
|
|
193
|
-
// throw new DatabaseError(engine, DatabaseErrorType.ConnectionFailed);
|
|
194
|
-
*/
|
|
195
|
-
// Example 2: Custom component error with custom strings
|
|
196
|
-
/*
|
|
197
|
-
enum UserErrorType {
|
|
198
|
-
UserNotFound = 'userNotFound',
|
|
199
|
-
InvalidCredentials = 'invalidCredentials',
|
|
200
|
-
AccountLocked = 'accountLocked'
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
enum UserErrorStringKey {
|
|
204
|
-
UserNotFoundMessage = 'userNotFoundMessage',
|
|
205
|
-
InvalidCredentialsMessage = 'invalidCredentialsMessage',
|
|
206
|
-
AccountLockedMessage = 'accountLockedMessage'
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const userErrorReasonMap: CompleteReasonMap<typeof UserErrorType, UserErrorStringKey> = {
|
|
210
|
-
[UserErrorType.UserNotFound]: UserErrorStringKey.UserNotFoundMessage,
|
|
211
|
-
[UserErrorType.InvalidCredentials]: UserErrorStringKey.InvalidCredentialsMessage,
|
|
212
|
-
[UserErrorType.AccountLocked]: UserErrorStringKey.AccountLockedMessage
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
class UserError extends PluginTypedError<typeof UserErrorType, UserErrorStringKey, string> {
|
|
216
|
-
constructor(
|
|
217
|
-
engine: PluginI18nEngine<string>,
|
|
218
|
-
type: UserErrorType,
|
|
219
|
-
otherVars?: Record<string, string | number>,
|
|
220
|
-
language?: string
|
|
221
|
-
) {
|
|
222
|
-
super(engine, 'user-system', type, userErrorReasonMap, language, otherVars);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
// Usage:
|
|
227
|
-
// const engine = PluginI18nEngine.getInstance<string>();
|
|
228
|
-
// throw new UserError(engine, UserErrorType.UserNotFound, { username: 'john_doe' });
|
|
229
|
-
*/
|
|
230
|
-
// Example 3: Using helper functions for simpler error creation
|
|
231
|
-
/*
|
|
232
|
-
// Define your error types and mappings
|
|
233
|
-
enum ApiErrorType {
|
|
234
|
-
Timeout = 'timeout',
|
|
235
|
-
NotFound = 'notFound'
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
const apiErrorMap: CompleteReasonMap<typeof ApiErrorType, CoreStringKey> = {
|
|
239
|
-
[ApiErrorType.Timeout]: CoreStringKey.Error_NetworkError,
|
|
240
|
-
[ApiErrorType.NotFound]: CoreStringKey.Error_NotFound
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
// Create errors using helper functions
|
|
244
|
-
function throwApiError(type: ApiErrorType, vars?: Record<string, string | number>) {
|
|
245
|
-
throw createCoreTypedError(type, apiErrorMap, vars);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// Usage:
|
|
249
|
-
// throwApiError(ApiErrorType.NotFound, { resource: 'user' });
|
|
250
|
-
*/
|
|
251
|
-
// Export the type for external use (already exported above)
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { HandleableErrorOptions } from './i-handleable-error-options';
|
|
2
|
-
import { IHandleable } from './i-handleable';
|
|
3
|
-
import { HandleableError } from './handleable';
|
|
4
|
-
import { CompleteReasonMap, TranslationEngine } from './typed-error';
|
|
5
|
-
import { CoreLanguageCode } from './core-i18n';
|
|
6
|
-
export declare class TypedHandleableError<TEnum extends Record<string, string>, TStringKey extends string, TLanguage extends CoreLanguageCode = CoreLanguageCode> extends HandleableError implements IHandleable {
|
|
7
|
-
readonly type: TEnum[keyof TEnum];
|
|
8
|
-
readonly reasonMap: CompleteReasonMap<TEnum, TStringKey>;
|
|
9
|
-
readonly engine: TranslationEngine<TStringKey>;
|
|
10
|
-
readonly language?: TLanguage;
|
|
11
|
-
readonly otherVars?: Record<string, string | number>;
|
|
12
|
-
constructor(type: TEnum[keyof TEnum], reasonMap: CompleteReasonMap<TEnum, TStringKey>, engine: TranslationEngine<TStringKey>, language?: TLanguage, otherVars?: Record<string, string | number>, options?: HandleableErrorOptions);
|
|
13
|
-
toJSON(): Record<string, unknown>;
|
|
14
|
-
}
|
package/dist/typed-handleable.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TypedHandleableError = void 0;
|
|
4
|
-
const handleable_1 = require("./handleable");
|
|
5
|
-
const plugin_i18n_engine_1 = require("./plugin-i18n-engine");
|
|
6
|
-
const core_string_key_1 = require("./core-string-key");
|
|
7
|
-
class TypedHandleableError extends handleable_1.HandleableError {
|
|
8
|
-
constructor(type, reasonMap, engine, language, otherVars, options) {
|
|
9
|
-
const key = reasonMap[type];
|
|
10
|
-
if (!key) {
|
|
11
|
-
const coreEngine = plugin_i18n_engine_1.PluginI18nEngine.getInstance();
|
|
12
|
-
throw new Error(coreEngine.translate('core', core_string_key_1.CoreStringKey.Error_MissingTranslationKeyTemplate, {
|
|
13
|
-
stringKey: key,
|
|
14
|
-
}));
|
|
15
|
-
}
|
|
16
|
-
let message = String(type);
|
|
17
|
-
try {
|
|
18
|
-
const keyString = key;
|
|
19
|
-
const translated = engine.translate(keyString, otherVars, language);
|
|
20
|
-
message = String(translated || type);
|
|
21
|
-
}
|
|
22
|
-
catch (error) {
|
|
23
|
-
message = String(type);
|
|
24
|
-
}
|
|
25
|
-
super(new Error(message), options);
|
|
26
|
-
this.type = type;
|
|
27
|
-
this.reasonMap = reasonMap;
|
|
28
|
-
this.language = language;
|
|
29
|
-
this.otherVars = otherVars;
|
|
30
|
-
this.engine = engine;
|
|
31
|
-
}
|
|
32
|
-
toJSON() {
|
|
33
|
-
const baseJson = super.toJSON();
|
|
34
|
-
return {
|
|
35
|
-
...baseJson,
|
|
36
|
-
type: this.type,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
exports.TypedHandleableError = TypedHandleableError;
|
package/dist/types.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DefaultTimezone = exports.DefaultCurrencyCode = void 0;
|
|
4
|
-
exports.createTranslations = createTranslations;
|
|
5
|
-
/**
|
|
6
|
-
* Default currency code
|
|
7
|
-
*/
|
|
8
|
-
exports.DefaultCurrencyCode = 'USD';
|
|
9
|
-
/**
|
|
10
|
-
* Default timezone
|
|
11
|
-
*/
|
|
12
|
-
exports.DefaultTimezone = 'UTC';
|
|
13
|
-
/**
|
|
14
|
-
* Helper function to create typed translations for an enumeration
|
|
15
|
-
*/
|
|
16
|
-
function createTranslations(translations) {
|
|
17
|
-
return translations;
|
|
18
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { PluginI18nEngine } from './plugin-i18n-engine';
|
|
2
|
-
import { I18nEngine } from './i18n-engine';
|
|
3
|
-
/**
|
|
4
|
-
* Unified translator with explicit component/engine specification
|
|
5
|
-
*/
|
|
6
|
-
export declare class UnifiedTranslator<TLanguage extends string = string> {
|
|
7
|
-
private sources;
|
|
8
|
-
private defaultLanguage;
|
|
9
|
-
private defaultSource?;
|
|
10
|
-
constructor(defaultLanguage: TLanguage);
|
|
11
|
-
/**
|
|
12
|
-
* Register a plugin component
|
|
13
|
-
*/
|
|
14
|
-
registerPlugin(id: string, engine: PluginI18nEngine<any>, componentId: string): void;
|
|
15
|
-
/**
|
|
16
|
-
* Register a legacy engine
|
|
17
|
-
*/
|
|
18
|
-
registerLegacy(id: string, engine: I18nEngine<any, any, any, any>): void;
|
|
19
|
-
/**
|
|
20
|
-
* Set default source for unqualified keys
|
|
21
|
-
*/
|
|
22
|
-
setDefaultSource(id: string): void;
|
|
23
|
-
/**
|
|
24
|
-
* Translate with explicit source: 'source:key' or just 'key' (uses default)
|
|
25
|
-
*/
|
|
26
|
-
translate(key: string, vars?: Record<string, string | number>, language?: TLanguage): string;
|
|
27
|
-
setLanguage(language: TLanguage): void;
|
|
28
|
-
getLanguage(): TLanguage;
|
|
29
|
-
clearSources(): void;
|
|
30
|
-
}
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.UnifiedTranslator = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Unified translator with explicit component/engine specification
|
|
6
|
-
*/
|
|
7
|
-
class UnifiedTranslator {
|
|
8
|
-
constructor(defaultLanguage) {
|
|
9
|
-
this.sources = new Map();
|
|
10
|
-
this.defaultLanguage = defaultLanguage;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Register a plugin component
|
|
14
|
-
*/
|
|
15
|
-
registerPlugin(id, engine, componentId) {
|
|
16
|
-
this.sources.set(id, { type: 'plugin', engine, componentId });
|
|
17
|
-
if (!this.defaultSource)
|
|
18
|
-
this.defaultSource = id;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Register a legacy engine
|
|
22
|
-
*/
|
|
23
|
-
registerLegacy(id, engine) {
|
|
24
|
-
this.sources.set(id, { type: 'legacy', engine });
|
|
25
|
-
if (!this.defaultSource)
|
|
26
|
-
this.defaultSource = id;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Set default source for unqualified keys
|
|
30
|
-
*/
|
|
31
|
-
setDefaultSource(id) {
|
|
32
|
-
if (!this.sources.has(id)) {
|
|
33
|
-
throw new Error(`Source '${id}' not registered`);
|
|
34
|
-
}
|
|
35
|
-
this.defaultSource = id;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Translate with explicit source: 'source:key' or just 'key' (uses default)
|
|
39
|
-
*/
|
|
40
|
-
translate(key, vars, language) {
|
|
41
|
-
const lang = language || this.defaultLanguage;
|
|
42
|
-
const [sourceName, actualKey] = key.includes(':')
|
|
43
|
-
? key.split(':', 2)
|
|
44
|
-
: [this.defaultSource, key];
|
|
45
|
-
if (!sourceName)
|
|
46
|
-
return `[${key}]`;
|
|
47
|
-
const source = this.sources.get(sourceName);
|
|
48
|
-
if (!source)
|
|
49
|
-
return `[${key}]`;
|
|
50
|
-
if (source.type === 'plugin') {
|
|
51
|
-
return source.engine.safeTranslate(source.componentId, actualKey, vars, lang);
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
return source.engine.safeTranslate(actualKey, vars, lang);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
setLanguage(language) {
|
|
58
|
-
this.defaultLanguage = language;
|
|
59
|
-
}
|
|
60
|
-
getLanguage() {
|
|
61
|
-
return this.defaultLanguage;
|
|
62
|
-
}
|
|
63
|
-
clearSources() {
|
|
64
|
-
this.sources.clear();
|
|
65
|
-
this.defaultSource = undefined;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
exports.UnifiedTranslator = UnifiedTranslator;
|
package/dist/utils.d.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Replaces variables in a string with their corresponding values from vars or constants.
|
|
3
|
-
* @param str - The string containing variables to replace
|
|
4
|
-
* @param vars - An object mapping variable names to their replacement values
|
|
5
|
-
* @param constants - An object containing constant values for replacement
|
|
6
|
-
* @returns The string with variables replaced
|
|
7
|
-
*/
|
|
8
|
-
export declare function replaceVariables(str: string, vars?: Record<string, string | number>, constants?: any): string;
|
|
9
|
-
/**
|
|
10
|
-
* Checks if a given key indicates a template string.
|
|
11
|
-
* @param key - The key to check
|
|
12
|
-
* @returns True if the key indicates a template, false otherwise
|
|
13
|
-
*/
|
|
14
|
-
export declare function isTemplate(key: string): boolean;
|
|
15
|
-
/**
|
|
16
|
-
* Checks if a given timezone string is valid.
|
|
17
|
-
* @param timezone - The timezone string to validate
|
|
18
|
-
* @returns
|
|
19
|
-
*/
|
|
20
|
-
export declare function isValidTimezone(timezone: string): boolean;
|
|
21
|
-
/**
|
|
22
|
-
* Converts parts to a single string key, joining with underscores.
|
|
23
|
-
* @param parts - The parts to join
|
|
24
|
-
* @returns The joined string key
|
|
25
|
-
*/
|
|
26
|
-
export declare function toStringKey<TStringKey extends string>(...parts: string[]): TStringKey;
|
|
27
|
-
/**
|
|
28
|
-
* Converts an enum value to a string key by joining parts with underscores and appending the enum value.
|
|
29
|
-
* @param value - The enum value
|
|
30
|
-
* @param parts - Additional parts to join
|
|
31
|
-
* @returns The constructed string key
|
|
32
|
-
*/
|
|
33
|
-
export declare function toStringKeyFromEnum<TStringKey extends string>(value: string, ...parts: string[]): TStringKey;
|
|
34
|
-
/**
|
|
35
|
-
* Type that constructs string keys from enum values with prefixes and optional template suffix
|
|
36
|
-
*/
|
|
37
|
-
type BuildStringKey<TEnumValue extends string, TPrefixes extends readonly string[], TIsTemplate extends boolean = false> = TPrefixes extends readonly [] ? `${TEnumValue}${TIsTemplate extends true ? 'Template' : ''}` : TPrefixes extends readonly [infer First, ...infer Rest] ? First extends string ? Rest extends readonly string[] ? `${First}_${BuildStringKey<TEnumValue, Rest, TIsTemplate>}` : never : never : never;
|
|
38
|
-
/**
|
|
39
|
-
* Type that maps all enum values to their corresponding string keys
|
|
40
|
-
*/
|
|
41
|
-
type ReasonMapFromEnum<TEnum extends Record<string, string>, TStringKey extends string, TPrefixes extends readonly string[] = [], TTemplateKeys extends Set<TEnum[keyof TEnum]> = never> = {
|
|
42
|
-
[K in TEnum[keyof TEnum]]: BuildStringKey<K, TPrefixes, TTemplateKeys extends Set<K> ? true : false> extends TStringKey ? BuildStringKey<K, TPrefixes, TTemplateKeys extends Set<K> ? true : false> : never;
|
|
43
|
-
};
|
|
44
|
-
/**
|
|
45
|
-
* Builds a reason map from an enum object, mapping each enum value to a string key.
|
|
46
|
-
* @param enumObj - The enum object
|
|
47
|
-
* @param prefixes - Prefixes to prepend to each string key
|
|
48
|
-
* @param templateKeys - Optional set of enum values that should have 'Template' suffix for template processing
|
|
49
|
-
* @returns The constructed reason map
|
|
50
|
-
*/
|
|
51
|
-
export declare function buildReasonMap<TEnum extends Record<string, string>, TStringKey extends string>(enumObj: TEnum, prefixes?: string[], templateKeys?: Set<TEnum[keyof TEnum]>): Record<TEnum[keyof TEnum], TStringKey>;
|
|
52
|
-
/**
|
|
53
|
-
* Type-safe version of buildReasonMap that ensures all enum values are mapped and all string keys exist
|
|
54
|
-
*/
|
|
55
|
-
export declare function buildTypeSafeReasonMap<TEnum extends Record<string, string>, TStringKey extends string, TPrefixes extends readonly string[] = [], TTemplateKeys extends Set<TEnum[keyof TEnum]> = never>(enumObj: TEnum, prefixes: TPrefixes, templateKeys?: TTemplateKeys): ReasonMapFromEnum<TEnum, TStringKey, TPrefixes, TTemplateKeys>;
|
|
56
|
-
/**
|
|
57
|
-
* Validates that a reason map has entries for all enum values
|
|
58
|
-
*/
|
|
59
|
-
export declare function validateReasonMap<TEnum extends Record<string, string>, TStringKey extends string>(enumObj: TEnum, reasonMap: Partial<Record<TEnum[keyof TEnum], TStringKey>>): reasonMap is Record<TEnum[keyof TEnum], TStringKey>;
|
|
60
|
-
/**
|
|
61
|
-
* Creates a complete reason map ensuring all enum values are covered
|
|
62
|
-
*/
|
|
63
|
-
export declare function createCompleteReasonMap<TEnum extends Record<string, string>, TStringKey extends string>(enumObj: TEnum, prefixes?: readonly string[], templateKeys?: Set<TEnum[keyof TEnum]>): Record<TEnum[keyof TEnum], TStringKey>;
|
|
64
|
-
export {};
|
package/dist/utils.js
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.replaceVariables = replaceVariables;
|
|
7
|
-
exports.isTemplate = isTemplate;
|
|
8
|
-
exports.isValidTimezone = isValidTimezone;
|
|
9
|
-
exports.toStringKey = toStringKey;
|
|
10
|
-
exports.toStringKeyFromEnum = toStringKeyFromEnum;
|
|
11
|
-
exports.buildReasonMap = buildReasonMap;
|
|
12
|
-
exports.buildTypeSafeReasonMap = buildTypeSafeReasonMap;
|
|
13
|
-
exports.validateReasonMap = validateReasonMap;
|
|
14
|
-
exports.createCompleteReasonMap = createCompleteReasonMap;
|
|
15
|
-
const moment_timezone_1 = __importDefault(require("moment-timezone"));
|
|
16
|
-
/**
|
|
17
|
-
* Replaces variables in a string with their corresponding values from vars or constants.
|
|
18
|
-
* @param str - The string containing variables to replace
|
|
19
|
-
* @param vars - An object mapping variable names to their replacement values
|
|
20
|
-
* @param constants - An object containing constant values for replacement
|
|
21
|
-
* @returns The string with variables replaced
|
|
22
|
-
*/
|
|
23
|
-
function replaceVariables(str, vars, constants) {
|
|
24
|
-
// Ensure input is a string
|
|
25
|
-
if (typeof str !== 'string') {
|
|
26
|
-
str = String(str);
|
|
27
|
-
}
|
|
28
|
-
const variables = str.match(/\{(.+?)\}/g);
|
|
29
|
-
if (!variables)
|
|
30
|
-
return str;
|
|
31
|
-
let result = str;
|
|
32
|
-
for (const variable of variables) {
|
|
33
|
-
const varName = variable.slice(1, -1);
|
|
34
|
-
let replacement = '';
|
|
35
|
-
if (vars && varName in vars) {
|
|
36
|
-
replacement = String(vars[varName]);
|
|
37
|
-
}
|
|
38
|
-
else if (constants && varName in constants) {
|
|
39
|
-
replacement = String(constants[varName]);
|
|
40
|
-
}
|
|
41
|
-
if (replacement) {
|
|
42
|
-
result = result.replace(variable, replacement);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return result;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Checks if a given key indicates a template string.
|
|
49
|
-
* @param key - The key to check
|
|
50
|
-
* @returns True if the key indicates a template, false otherwise
|
|
51
|
-
*/
|
|
52
|
-
function isTemplate(key) {
|
|
53
|
-
return key.trim().toLowerCase().endsWith('template');
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Checks if a given timezone string is valid.
|
|
57
|
-
* @param timezone - The timezone string to validate
|
|
58
|
-
* @returns
|
|
59
|
-
*/
|
|
60
|
-
function isValidTimezone(timezone) {
|
|
61
|
-
return moment_timezone_1.default.tz.zone(timezone) !== null;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Converts parts to a single string key, joining with underscores.
|
|
65
|
-
* @param parts - The parts to join
|
|
66
|
-
* @returns The joined string key
|
|
67
|
-
*/
|
|
68
|
-
function toStringKey(...parts) {
|
|
69
|
-
return parts.join('_');
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Converts an enum value to a string key by joining parts with underscores and appending the enum value.
|
|
73
|
-
* @param value - The enum value
|
|
74
|
-
* @param parts - Additional parts to join
|
|
75
|
-
* @returns The constructed string key
|
|
76
|
-
*/
|
|
77
|
-
function toStringKeyFromEnum(value, ...parts) {
|
|
78
|
-
const allParts = [...parts, value];
|
|
79
|
-
return allParts.join('_');
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Builds a reason map from an enum object, mapping each enum value to a string key.
|
|
83
|
-
* @param enumObj - The enum object
|
|
84
|
-
* @param prefixes - Prefixes to prepend to each string key
|
|
85
|
-
* @param templateKeys - Optional set of enum values that should have 'Template' suffix for template processing
|
|
86
|
-
* @returns The constructed reason map
|
|
87
|
-
*/
|
|
88
|
-
function buildReasonMap(enumObj, prefixes = [], templateKeys) {
|
|
89
|
-
const map = {};
|
|
90
|
-
Object.values(enumObj).forEach((value) => {
|
|
91
|
-
const baseKey = [...prefixes, value].join('_');
|
|
92
|
-
const finalKey = templateKeys?.has(value)
|
|
93
|
-
? baseKey + 'Template'
|
|
94
|
-
: baseKey;
|
|
95
|
-
map[value] = finalKey;
|
|
96
|
-
});
|
|
97
|
-
return map;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Type-safe version of buildReasonMap that ensures all enum values are mapped and all string keys exist
|
|
101
|
-
*/
|
|
102
|
-
function buildTypeSafeReasonMap(enumObj, prefixes, templateKeys) {
|
|
103
|
-
const map = {};
|
|
104
|
-
Object.values(enumObj).forEach((value) => {
|
|
105
|
-
const baseKey = [...prefixes, value].join('_');
|
|
106
|
-
const finalKey = templateKeys?.has(value)
|
|
107
|
-
? baseKey + 'Template'
|
|
108
|
-
: baseKey;
|
|
109
|
-
map[value] = finalKey;
|
|
110
|
-
});
|
|
111
|
-
return map;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Validates that a reason map has entries for all enum values
|
|
115
|
-
*/
|
|
116
|
-
function validateReasonMap(enumObj, reasonMap) {
|
|
117
|
-
return Object.values(enumObj).every((value) => value in reasonMap &&
|
|
118
|
-
reasonMap[value] !== undefined);
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Creates a complete reason map ensuring all enum values are covered
|
|
122
|
-
*/
|
|
123
|
-
function createCompleteReasonMap(enumObj, prefixes = [], templateKeys) {
|
|
124
|
-
const map = buildReasonMap(enumObj, [...prefixes], templateKeys);
|
|
125
|
-
if (!validateReasonMap(enumObj, map)) {
|
|
126
|
-
const missing = Object.values(enumObj).filter((value) => !(value in map));
|
|
127
|
-
throw new Error(`Missing reason map entries for: ${missing.join(', ')}`);
|
|
128
|
-
}
|
|
129
|
-
return map;
|
|
130
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Configuration for component registration validation
|
|
3
|
-
*/
|
|
4
|
-
export interface ValidationConfig {
|
|
5
|
-
/** Whether to require all languages to have all strings */
|
|
6
|
-
readonly requireCompleteStrings: boolean;
|
|
7
|
-
/** Whether to allow registration with missing strings (will use fallbacks) */
|
|
8
|
-
readonly allowPartialRegistration: boolean;
|
|
9
|
-
/** Default language to fall back to for missing strings */
|
|
10
|
-
readonly fallbackLanguageId: string;
|
|
11
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Validation result for component registration
|
|
3
|
-
*/
|
|
4
|
-
export interface ValidationResult {
|
|
5
|
-
readonly isValid: boolean;
|
|
6
|
-
readonly missingKeys: ReadonlyArray<{
|
|
7
|
-
readonly languageId: string;
|
|
8
|
-
readonly componentId: string;
|
|
9
|
-
readonly stringKey: string;
|
|
10
|
-
}>;
|
|
11
|
-
readonly errors: readonly string[];
|
|
12
|
-
}
|