@digitaldefiance/i18n-lib 1.0.33 → 1.1.0
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 +440 -4
- package/dist/component-definition.d.ts +11 -0
- package/dist/component-definition.js +2 -0
- package/dist/component-registration.d.ts +9 -0
- package/dist/component-registration.js +2 -0
- package/dist/component-registry.d.ts +64 -0
- package/dist/component-registry.js +238 -0
- package/dist/context.d.ts +2 -1
- package/dist/core-i18n.d.ts +330 -0
- package/dist/core-i18n.js +435 -0
- package/dist/core-language.d.ts +13 -0
- package/dist/core-language.js +17 -0
- package/dist/core-string-key.d.ts +39 -0
- package/dist/core-string-key.js +47 -0
- package/dist/default-config.d.ts +2 -1
- package/dist/default-config.js +24 -24
- package/dist/i18n-config.d.ts +20 -0
- package/dist/i18n-config.js +2 -0
- package/dist/i18n-context.d.ts +14 -0
- package/dist/i18n-context.js +2 -0
- package/dist/i18n-engine.d.ts +3 -1
- package/dist/index.d.ts +21 -1
- package/dist/index.js +25 -2
- package/dist/language-definition.d.ts +13 -0
- package/dist/language-definition.js +2 -0
- package/dist/language-registry.d.ts +106 -0
- package/dist/language-registry.js +194 -0
- package/dist/plugin-i18n-engine.d.ts +126 -0
- package/dist/plugin-i18n-engine.js +266 -0
- package/dist/registry-config.d.ts +14 -0
- package/dist/registry-config.js +2 -0
- package/dist/registry-context.d.ts +12 -0
- package/dist/registry-context.js +2 -0
- package/dist/registry-error-type.d.ts +12 -0
- package/dist/registry-error-type.js +16 -0
- package/dist/registry-error.d.ts +19 -0
- package/dist/registry-error.js +44 -0
- package/dist/translation-request.d.ts +9 -0
- package/dist/translation-request.js +2 -0
- package/dist/translation-response.d.ts +8 -0
- package/dist/translation-response.js +2 -0
- package/dist/typed-error.d.ts +63 -3
- package/dist/typed-error.js +230 -2
- package/dist/types.d.ts +42 -29
- package/dist/validation-config.d.ts +11 -0
- package/dist/validation-config.js +2 -0
- package/dist/validation-result.d.ts +12 -0
- package/dist/validation-result.js +2 -0
- package/package.json +2 -1
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin-based internationalization engine with component and language registration
|
|
3
|
+
*/
|
|
4
|
+
import { ComponentDefinition } from './component-definition';
|
|
5
|
+
import { ComponentRegistration } from './component-registration';
|
|
6
|
+
import { ComponentRegistry } from './component-registry';
|
|
7
|
+
import { EnumTranslationRegistry } from './enum-registry';
|
|
8
|
+
import { LanguageDefinition } from './language-definition';
|
|
9
|
+
import { LanguageRegistry } from './language-registry';
|
|
10
|
+
import { RegistryConfig } from './registry-config';
|
|
11
|
+
import { RegistryContext } from './registry-context';
|
|
12
|
+
import { TranslationResponse } from './translation-response';
|
|
13
|
+
import { EnumLanguageTranslation } from './types';
|
|
14
|
+
/**
|
|
15
|
+
* Plugin-based I18n Engine with registration capabilities
|
|
16
|
+
*/
|
|
17
|
+
export declare class PluginI18nEngine<TLanguages extends string> {
|
|
18
|
+
private readonly languageRegistry;
|
|
19
|
+
private readonly componentRegistry;
|
|
20
|
+
private readonly enumRegistry;
|
|
21
|
+
private readonly config;
|
|
22
|
+
private context;
|
|
23
|
+
/**
|
|
24
|
+
* Static instances for semi-singleton pattern
|
|
25
|
+
*/
|
|
26
|
+
private static _instances;
|
|
27
|
+
private static _defaultKey;
|
|
28
|
+
protected static readonly DefaultInstanceKey = "default";
|
|
29
|
+
constructor(initialLanguages: readonly LanguageDefinition[], config?: Partial<RegistryConfig<TLanguages>>);
|
|
30
|
+
/**
|
|
31
|
+
* Create a new instance with a specific key
|
|
32
|
+
*/
|
|
33
|
+
static createInstance<TLangs extends string>(key: string, initialLanguages: readonly LanguageDefinition[], config?: Partial<RegistryConfig<TLangs>>): PluginI18nEngine<TLangs>;
|
|
34
|
+
/**
|
|
35
|
+
* Get an existing instance by key
|
|
36
|
+
*/
|
|
37
|
+
static getInstance<TLangs extends string>(key?: string): PluginI18nEngine<TLangs>;
|
|
38
|
+
/**
|
|
39
|
+
* Register a new language
|
|
40
|
+
*/
|
|
41
|
+
registerLanguage(language: LanguageDefinition): void;
|
|
42
|
+
/**
|
|
43
|
+
* Register multiple languages
|
|
44
|
+
*/
|
|
45
|
+
registerLanguages(languages: readonly LanguageDefinition[]): void;
|
|
46
|
+
/**
|
|
47
|
+
* Register a component with its translations
|
|
48
|
+
*/
|
|
49
|
+
registerComponent<TStringKeys extends string>(registration: ComponentRegistration<TStringKeys, TLanguages>): import("./validation-result").ValidationResult;
|
|
50
|
+
/**
|
|
51
|
+
* Update strings for an existing component
|
|
52
|
+
*/
|
|
53
|
+
updateComponentStrings<TStringKeys extends string>(componentId: string, strings: Parameters<ComponentRegistry<TLanguages>['updateComponentStrings']>[1]): import("./validation-result").ValidationResult;
|
|
54
|
+
/**
|
|
55
|
+
* Register an enum with translations
|
|
56
|
+
*/
|
|
57
|
+
registerEnum<TEnum extends string | number>(enumObj: Record<string, TEnum>, translations: EnumLanguageTranslation<TEnum, TLanguages>, enumName: string): void;
|
|
58
|
+
/**
|
|
59
|
+
* Translate a string for a component
|
|
60
|
+
*/
|
|
61
|
+
translate<TStringKeys extends string>(componentId: string, stringKey: TStringKeys, variables?: Record<string, string | number>, language?: TLanguages): string;
|
|
62
|
+
/**
|
|
63
|
+
* Safe translate that returns a placeholder if translation fails
|
|
64
|
+
*/
|
|
65
|
+
safeTranslate(componentId: string, stringKey: string, variables?: Record<string, string | number>, language?: TLanguages): string;
|
|
66
|
+
/**
|
|
67
|
+
* Translate an enum value
|
|
68
|
+
*/
|
|
69
|
+
translateEnum<TEnum extends string | number>(enumObj: Record<string, TEnum>, value: TEnum, language?: TLanguages): string;
|
|
70
|
+
/**
|
|
71
|
+
* Get detailed translation response
|
|
72
|
+
*/
|
|
73
|
+
getTranslationDetails<TStringKeys extends string>(componentId: string, stringKey: TStringKeys, variables?: Record<string, string | number>, language?: TLanguages): TranslationResponse;
|
|
74
|
+
/**
|
|
75
|
+
* Get current context
|
|
76
|
+
*/
|
|
77
|
+
getContext(): RegistryContext<TLanguages>;
|
|
78
|
+
/**
|
|
79
|
+
* Update context
|
|
80
|
+
*/
|
|
81
|
+
updateContext(updates: Partial<RegistryContext<TLanguages>>): void;
|
|
82
|
+
/**
|
|
83
|
+
* Set current language
|
|
84
|
+
*/
|
|
85
|
+
setLanguage(language: TLanguages): void;
|
|
86
|
+
/**
|
|
87
|
+
* Get available languages
|
|
88
|
+
*/
|
|
89
|
+
getLanguages(): readonly LanguageDefinition[];
|
|
90
|
+
/**
|
|
91
|
+
* Get registered components
|
|
92
|
+
*/
|
|
93
|
+
getComponents(): ReadonlyArray<ComponentDefinition<any>>;
|
|
94
|
+
/**
|
|
95
|
+
* Check if a component is registered
|
|
96
|
+
*/
|
|
97
|
+
hasComponent(componentId: string): boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Check if a language is registered
|
|
100
|
+
*/
|
|
101
|
+
hasLanguage(language: TLanguages): boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Get language by code
|
|
104
|
+
*/
|
|
105
|
+
getLanguageByCode(code: string): LanguageDefinition | undefined;
|
|
106
|
+
/**
|
|
107
|
+
* Get language registry for direct access
|
|
108
|
+
*/
|
|
109
|
+
getLanguageRegistry(): LanguageRegistry<TLanguages>;
|
|
110
|
+
/**
|
|
111
|
+
* Get component registry for direct access
|
|
112
|
+
*/
|
|
113
|
+
getComponentRegistry(): ComponentRegistry<TLanguages>;
|
|
114
|
+
/**
|
|
115
|
+
* Get enum registry for direct access
|
|
116
|
+
*/
|
|
117
|
+
getEnumRegistry(): EnumTranslationRegistry<string, TLanguages>;
|
|
118
|
+
/**
|
|
119
|
+
* Validate that all components have complete translations
|
|
120
|
+
*/
|
|
121
|
+
validateAllComponents(): {
|
|
122
|
+
isValid: boolean;
|
|
123
|
+
errors: string[];
|
|
124
|
+
warnings: string[];
|
|
125
|
+
};
|
|
126
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Plugin-based internationalization engine with component and language registration
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.PluginI18nEngine = void 0;
|
|
7
|
+
const component_registry_1 = require("./component-registry");
|
|
8
|
+
const currency_code_1 = require("./currency-code");
|
|
9
|
+
const enum_registry_1 = require("./enum-registry");
|
|
10
|
+
const language_registry_1 = require("./language-registry");
|
|
11
|
+
const registry_error_1 = require("./registry-error");
|
|
12
|
+
const registry_error_type_1 = require("./registry-error-type");
|
|
13
|
+
const timezone_1 = require("./timezone");
|
|
14
|
+
/**
|
|
15
|
+
* Plugin-based I18n Engine with registration capabilities
|
|
16
|
+
*/
|
|
17
|
+
class PluginI18nEngine {
|
|
18
|
+
constructor(initialLanguages, config = {}) {
|
|
19
|
+
// Find default language from initialLanguages or use the first one
|
|
20
|
+
const defaultLang = initialLanguages.find((l) => l.isDefault) || initialLanguages[0];
|
|
21
|
+
if (!defaultLang) {
|
|
22
|
+
throw new Error('At least one language must be provided');
|
|
23
|
+
}
|
|
24
|
+
// Set up configuration with defaults
|
|
25
|
+
this.config = {
|
|
26
|
+
defaultLanguage: defaultLang.id,
|
|
27
|
+
fallbackLanguage: defaultLang.id,
|
|
28
|
+
defaultCurrencyCode: new currency_code_1.CurrencyCode('USD'),
|
|
29
|
+
timezone: new timezone_1.Timezone('UTC'),
|
|
30
|
+
adminTimezone: new timezone_1.Timezone('UTC'),
|
|
31
|
+
validation: {
|
|
32
|
+
requireCompleteStrings: false,
|
|
33
|
+
allowPartialRegistration: true,
|
|
34
|
+
fallbackLanguageId: defaultLang.id,
|
|
35
|
+
},
|
|
36
|
+
...config,
|
|
37
|
+
};
|
|
38
|
+
// Initialize registries
|
|
39
|
+
this.languageRegistry = new language_registry_1.LanguageRegistry();
|
|
40
|
+
this.componentRegistry = new component_registry_1.ComponentRegistry(initialLanguages.map((l) => l.id), this.config.validation);
|
|
41
|
+
this.enumRegistry = new enum_registry_1.EnumTranslationRegistry(initialLanguages.map((l) => l.id), (key, vars) => this.safeTranslate('core', key, vars));
|
|
42
|
+
// Register initial languages
|
|
43
|
+
this.languageRegistry.registerLanguages(initialLanguages);
|
|
44
|
+
// Initialize context
|
|
45
|
+
this.context = {
|
|
46
|
+
currentLanguage: this.config.defaultLanguage,
|
|
47
|
+
fallbackLanguage: this.config.fallbackLanguage,
|
|
48
|
+
currencyCode: this.config.defaultCurrencyCode,
|
|
49
|
+
timezone: this.config.timezone,
|
|
50
|
+
adminTimezone: this.config.adminTimezone,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Create a new instance with a specific key
|
|
55
|
+
*/
|
|
56
|
+
static createInstance(key, initialLanguages, config) {
|
|
57
|
+
if (PluginI18nEngine._instances.has(key)) {
|
|
58
|
+
throw registry_error_1.RegistryError.createSimple(registry_error_type_1.RegistryErrorType.DuplicateComponent, `I18n instance with key '${key}' already exists`, { key });
|
|
59
|
+
}
|
|
60
|
+
const instance = new PluginI18nEngine(initialLanguages, config);
|
|
61
|
+
PluginI18nEngine._instances.set(key, instance);
|
|
62
|
+
if (!PluginI18nEngine._defaultKey) {
|
|
63
|
+
PluginI18nEngine._defaultKey = key;
|
|
64
|
+
}
|
|
65
|
+
return instance;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Get an existing instance by key
|
|
69
|
+
*/
|
|
70
|
+
static getInstance(key) {
|
|
71
|
+
const instanceKey = key ||
|
|
72
|
+
PluginI18nEngine._defaultKey ||
|
|
73
|
+
PluginI18nEngine.DefaultInstanceKey;
|
|
74
|
+
const instance = PluginI18nEngine._instances.get(instanceKey);
|
|
75
|
+
if (!instance) {
|
|
76
|
+
throw registry_error_1.RegistryError.createSimple(registry_error_type_1.RegistryErrorType.ComponentNotFound, `I18n instance with key '${instanceKey}' not found`, { key: instanceKey });
|
|
77
|
+
}
|
|
78
|
+
return instance;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Register a new language
|
|
82
|
+
*/
|
|
83
|
+
registerLanguage(language) {
|
|
84
|
+
this.languageRegistry.registerLanguage(language);
|
|
85
|
+
// Update component registry with new language
|
|
86
|
+
const newLanguages = this.languageRegistry.getLanguageIds();
|
|
87
|
+
this.componentRegistry.updateRegisteredLanguages(newLanguages);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Register multiple languages
|
|
91
|
+
*/
|
|
92
|
+
registerLanguages(languages) {
|
|
93
|
+
for (const language of languages) {
|
|
94
|
+
this.registerLanguage(language);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Register a component with its translations
|
|
99
|
+
*/
|
|
100
|
+
registerComponent(registration) {
|
|
101
|
+
return this.componentRegistry.registerComponent(registration);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Update strings for an existing component
|
|
105
|
+
*/
|
|
106
|
+
updateComponentStrings(componentId, strings) {
|
|
107
|
+
return this.componentRegistry.updateComponentStrings(componentId, strings);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Register an enum with translations
|
|
111
|
+
*/
|
|
112
|
+
registerEnum(enumObj, translations, enumName) {
|
|
113
|
+
this.enumRegistry.register(enumObj, translations, enumName);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Translate a string for a component
|
|
117
|
+
*/
|
|
118
|
+
translate(componentId, stringKey, variables, language) {
|
|
119
|
+
const request = {
|
|
120
|
+
componentId,
|
|
121
|
+
stringKey,
|
|
122
|
+
language: language || this.context.currentLanguage,
|
|
123
|
+
variables,
|
|
124
|
+
};
|
|
125
|
+
const response = this.componentRegistry.getTranslation(request);
|
|
126
|
+
return response.translation;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Safe translate that returns a placeholder if translation fails
|
|
130
|
+
*/
|
|
131
|
+
safeTranslate(componentId, stringKey, variables, language) {
|
|
132
|
+
try {
|
|
133
|
+
return this.translate(componentId, stringKey, variables, language);
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
// Return a placeholder if translation fails
|
|
137
|
+
return `[${componentId}.${stringKey}]`;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Translate an enum value
|
|
142
|
+
*/
|
|
143
|
+
translateEnum(enumObj, value, language) {
|
|
144
|
+
return this.enumRegistry.translate(enumObj, value, language || this.context.currentLanguage);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Get detailed translation response
|
|
148
|
+
*/
|
|
149
|
+
getTranslationDetails(componentId, stringKey, variables, language) {
|
|
150
|
+
const request = {
|
|
151
|
+
componentId,
|
|
152
|
+
stringKey,
|
|
153
|
+
language: language || this.context.currentLanguage,
|
|
154
|
+
variables,
|
|
155
|
+
};
|
|
156
|
+
return this.componentRegistry.getTranslation(request);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Get current context
|
|
160
|
+
*/
|
|
161
|
+
getContext() {
|
|
162
|
+
return { ...this.context };
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Update context
|
|
166
|
+
*/
|
|
167
|
+
updateContext(updates) {
|
|
168
|
+
this.context = { ...this.context, ...updates };
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Set current language
|
|
172
|
+
*/
|
|
173
|
+
setLanguage(language) {
|
|
174
|
+
if (!this.languageRegistry.hasLanguage(language)) {
|
|
175
|
+
throw registry_error_1.RegistryError.createSimple(registry_error_type_1.RegistryErrorType.LanguageNotFound, `Language '${language}' is not registered`, { language });
|
|
176
|
+
}
|
|
177
|
+
this.context.currentLanguage = language;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Get available languages
|
|
181
|
+
*/
|
|
182
|
+
getLanguages() {
|
|
183
|
+
return this.languageRegistry.getAllLanguages();
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Get registered components
|
|
187
|
+
*/
|
|
188
|
+
getComponents() {
|
|
189
|
+
return this.componentRegistry.getComponents();
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Check if a component is registered
|
|
193
|
+
*/
|
|
194
|
+
hasComponent(componentId) {
|
|
195
|
+
return this.componentRegistry.hasComponent(componentId);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Check if a language is registered
|
|
199
|
+
*/
|
|
200
|
+
hasLanguage(language) {
|
|
201
|
+
return this.languageRegistry.hasLanguage(language);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Get language by code
|
|
205
|
+
*/
|
|
206
|
+
getLanguageByCode(code) {
|
|
207
|
+
return this.languageRegistry.getLanguageByCode(code);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Get language registry for direct access
|
|
211
|
+
*/
|
|
212
|
+
getLanguageRegistry() {
|
|
213
|
+
return this.languageRegistry;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Get component registry for direct access
|
|
217
|
+
*/
|
|
218
|
+
getComponentRegistry() {
|
|
219
|
+
return this.componentRegistry;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Get enum registry for direct access
|
|
223
|
+
*/
|
|
224
|
+
getEnumRegistry() {
|
|
225
|
+
return this.enumRegistry;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Validate that all components have complete translations
|
|
229
|
+
*/
|
|
230
|
+
validateAllComponents() {
|
|
231
|
+
const errors = [];
|
|
232
|
+
const warnings = [];
|
|
233
|
+
let isValid = true;
|
|
234
|
+
const components = this.getComponents();
|
|
235
|
+
const languages = this.languageRegistry.getLanguageIds();
|
|
236
|
+
for (const component of components) {
|
|
237
|
+
const componentStrings = this.componentRegistry.getComponentStrings(component.id);
|
|
238
|
+
if (!componentStrings) {
|
|
239
|
+
errors.push(`Component '${component.id}' has no registered strings`);
|
|
240
|
+
isValid = false;
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
for (const language of languages) {
|
|
244
|
+
const languageStrings = componentStrings[language];
|
|
245
|
+
if (!languageStrings) {
|
|
246
|
+
errors.push(`Component '${component.id}' missing strings for language '${language}'`);
|
|
247
|
+
isValid = false;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
for (const stringKey of component.stringKeys) {
|
|
251
|
+
if (!languageStrings[stringKey]) {
|
|
252
|
+
warnings.push(`Component '${component.id}' missing key '${stringKey}' for language '${language}'`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return { isValid, errors, warnings };
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
exports.PluginI18nEngine = PluginI18nEngine;
|
|
261
|
+
/**
|
|
262
|
+
* Static instances for semi-singleton pattern
|
|
263
|
+
*/
|
|
264
|
+
PluginI18nEngine._instances = new Map();
|
|
265
|
+
PluginI18nEngine._defaultKey = null;
|
|
266
|
+
PluginI18nEngine.DefaultInstanceKey = 'default';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CurrencyCode } from './currency-code';
|
|
2
|
+
import { Timezone } from './timezone';
|
|
3
|
+
import { ValidationConfig } from './validation-config';
|
|
4
|
+
/**
|
|
5
|
+
* Registry configuration
|
|
6
|
+
*/
|
|
7
|
+
export interface RegistryConfig<TLanguages extends string> {
|
|
8
|
+
readonly defaultLanguage: TLanguages;
|
|
9
|
+
readonly fallbackLanguage: TLanguages;
|
|
10
|
+
readonly defaultCurrencyCode: CurrencyCode;
|
|
11
|
+
readonly timezone: Timezone;
|
|
12
|
+
readonly adminTimezone: Timezone;
|
|
13
|
+
readonly validation: ValidationConfig;
|
|
14
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error types for registry operations
|
|
3
|
+
*/
|
|
4
|
+
export declare 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,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RegistryErrorType = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Error types for registry operations
|
|
6
|
+
*/
|
|
7
|
+
var RegistryErrorType;
|
|
8
|
+
(function (RegistryErrorType) {
|
|
9
|
+
RegistryErrorType["ComponentNotFound"] = "COMPONENT_NOT_FOUND";
|
|
10
|
+
RegistryErrorType["LanguageNotFound"] = "LANGUAGE_NOT_FOUND";
|
|
11
|
+
RegistryErrorType["StringKeyNotFound"] = "STRING_KEY_NOT_FOUND";
|
|
12
|
+
RegistryErrorType["IncompleteRegistration"] = "INCOMPLETE_REGISTRATION";
|
|
13
|
+
RegistryErrorType["DuplicateComponent"] = "DUPLICATE_COMPONENT";
|
|
14
|
+
RegistryErrorType["DuplicateLanguage"] = "DUPLICATE_LANGUAGE";
|
|
15
|
+
RegistryErrorType["ValidationFailed"] = "VALIDATION_FAILED";
|
|
16
|
+
})(RegistryErrorType || (exports.RegistryErrorType = RegistryErrorType = {}));
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CoreLanguage } from './core-language';
|
|
2
|
+
import { RegistryErrorType } from './registry-error-type';
|
|
3
|
+
import { TranslationEngine } from './typed-error';
|
|
4
|
+
/**
|
|
5
|
+
* Registry error class that can work with plugin engines
|
|
6
|
+
*/
|
|
7
|
+
export declare class RegistryError extends Error {
|
|
8
|
+
readonly type: RegistryErrorType;
|
|
9
|
+
readonly metadata?: Record<string, any> | undefined;
|
|
10
|
+
constructor(type: RegistryErrorType, message: string, metadata?: Record<string, any> | undefined);
|
|
11
|
+
/**
|
|
12
|
+
* Create a registry error with translation support
|
|
13
|
+
*/
|
|
14
|
+
static createWithEngine(engine: TranslationEngine, type: RegistryErrorType, variables?: Record<string, string | number>, language?: CoreLanguage, metadata?: Record<string, any>): RegistryError;
|
|
15
|
+
/**
|
|
16
|
+
* Create a simple RegistryError without engine dependency
|
|
17
|
+
*/
|
|
18
|
+
static createSimple(type: RegistryErrorType, message: string, metadata?: Record<string, any>): RegistryError;
|
|
19
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RegistryError = void 0;
|
|
4
|
+
const core_string_key_1 = require("./core-string-key");
|
|
5
|
+
const registry_error_type_1 = require("./registry-error-type");
|
|
6
|
+
const typed_error_1 = require("./typed-error");
|
|
7
|
+
/**
|
|
8
|
+
* Reason map for registry errors
|
|
9
|
+
*/
|
|
10
|
+
const REGISTRY_ERROR_REASON_MAP = {
|
|
11
|
+
[registry_error_type_1.RegistryErrorType.ComponentNotFound]: core_string_key_1.CoreStringKey.Error_ComponentNotFoundTemplate,
|
|
12
|
+
[registry_error_type_1.RegistryErrorType.DuplicateComponent]: core_string_key_1.CoreStringKey.Error_DuplicateComponentTemplate,
|
|
13
|
+
[registry_error_type_1.RegistryErrorType.DuplicateLanguage]: core_string_key_1.CoreStringKey.Error_DuplicateLanguageTemplate,
|
|
14
|
+
[registry_error_type_1.RegistryErrorType.IncompleteRegistration]: core_string_key_1.CoreStringKey.Error_IncompleteRegistrationTemplate,
|
|
15
|
+
[registry_error_type_1.RegistryErrorType.LanguageNotFound]: core_string_key_1.CoreStringKey.Error_LanguageNotFoundTemplate,
|
|
16
|
+
[registry_error_type_1.RegistryErrorType.StringKeyNotFound]: core_string_key_1.CoreStringKey.Error_StringKeyNotFoundTemplate,
|
|
17
|
+
[registry_error_type_1.RegistryErrorType.ValidationFailed]: core_string_key_1.CoreStringKey.Error_ValidationFailedTemplate,
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Registry error class that can work with plugin engines
|
|
21
|
+
*/
|
|
22
|
+
class RegistryError extends Error {
|
|
23
|
+
constructor(type, message, metadata) {
|
|
24
|
+
super(message);
|
|
25
|
+
this.type = type;
|
|
26
|
+
this.metadata = metadata;
|
|
27
|
+
this.name = 'RegistryError';
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Create a registry error with translation support
|
|
31
|
+
*/
|
|
32
|
+
static createWithEngine(engine, type, variables, language, metadata) {
|
|
33
|
+
const error = (0, typed_error_1.createTranslatedError)(engine, 'core', type, REGISTRY_ERROR_REASON_MAP, variables, language, metadata, 'RegistryError');
|
|
34
|
+
// Convert to RegistryError instance
|
|
35
|
+
return new RegistryError(type, error.message, metadata);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Create a simple RegistryError without engine dependency
|
|
39
|
+
*/
|
|
40
|
+
static createSimple(type, message, metadata) {
|
|
41
|
+
return new RegistryError(type, message, metadata);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.RegistryError = RegistryError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Translation request interface
|
|
3
|
+
*/
|
|
4
|
+
export interface TranslationRequest<TStringKeys extends string, TLanguages extends string> {
|
|
5
|
+
readonly componentId: string;
|
|
6
|
+
readonly stringKey: TStringKeys;
|
|
7
|
+
readonly language?: TLanguages;
|
|
8
|
+
readonly variables?: Record<string, string | number>;
|
|
9
|
+
}
|
package/dist/typed-error.d.ts
CHANGED
|
@@ -1,11 +1,36 @@
|
|
|
1
1
|
import { Language, StringKey } from './default-config';
|
|
2
2
|
import { I18nEngine } from './i18n-engine';
|
|
3
|
+
import { CoreLanguage } from './core-language';
|
|
4
|
+
import { CoreStringKey } from './core-string-key';
|
|
5
|
+
import { PluginI18nEngine } from './plugin-i18n-engine';
|
|
6
|
+
/**
|
|
7
|
+
* Interface for engines that can translate strings (unified interface)
|
|
8
|
+
*/
|
|
9
|
+
export interface TranslationEngine {
|
|
10
|
+
safeTranslate(componentId: string, stringKey: string, variables?: Record<string, string | number>, language?: string): string;
|
|
11
|
+
}
|
|
3
12
|
/**
|
|
4
13
|
* Type constraint to ensure reasonMap has entries for all enum values
|
|
5
14
|
*/
|
|
6
|
-
type CompleteReasonMap<TEnum extends Record<string, string | number>, TStringKey extends string> = Record<TEnum[keyof TEnum], TStringKey>;
|
|
15
|
+
export type CompleteReasonMap<TEnum extends Record<string, string | number>, TStringKey extends string> = Record<TEnum[keyof TEnum], TStringKey>;
|
|
16
|
+
/**
|
|
17
|
+
* Base typed error class with common patterns
|
|
18
|
+
*/
|
|
19
|
+
export declare abstract class BaseTypedError<TEnum extends Record<string, string>, TStringKey extends string> extends Error {
|
|
20
|
+
readonly type: TEnum[keyof TEnum];
|
|
21
|
+
readonly metadata?: Record<string, any> | undefined;
|
|
22
|
+
constructor(type: TEnum[keyof TEnum], message: string, metadata?: Record<string, any> | undefined);
|
|
23
|
+
/**
|
|
24
|
+
* Create a simple typed error without engine dependency
|
|
25
|
+
*/
|
|
26
|
+
static createSimple<TEnum extends Record<string, string>, TStringKey extends string, TError extends BaseTypedError<TEnum, TStringKey>>(this: new (type: TEnum[keyof TEnum], message: string, metadata?: Record<string, any>) => TError, type: TEnum[keyof TEnum], message: string, metadata?: Record<string, any>): TError;
|
|
27
|
+
/**
|
|
28
|
+
* Create a typed error with translation support
|
|
29
|
+
*/
|
|
30
|
+
static createTranslated<TEnum extends Record<string, string>, TStringKey extends string, TError extends BaseTypedError<TEnum, TStringKey>>(this: new (type: TEnum[keyof TEnum], message: string, metadata?: Record<string, any>) => TError, engine: TranslationEngine, componentId: string, type: TEnum[keyof TEnum], reasonMap: CompleteReasonMap<TEnum, TStringKey>, variables?: Record<string, string | number>, language?: string, metadata?: Record<string, any>): TError;
|
|
31
|
+
}
|
|
7
32
|
/**
|
|
8
|
-
*
|
|
33
|
+
* Legacy TypedError that ensures complete enum coverage (for backward compatibility)
|
|
9
34
|
*/
|
|
10
35
|
export declare abstract class TypedError<TEnum extends Record<string, string>, TStringKey extends string = StringKey> extends Error {
|
|
11
36
|
readonly type: TEnum[keyof TEnum];
|
|
@@ -14,4 +39,39 @@ export declare abstract class TypedError<TEnum extends Record<string, string>, T
|
|
|
14
39
|
readonly otherVars?: Record<string, string | number> | undefined;
|
|
15
40
|
constructor(engine: I18nEngine<TStringKey, Language, Record<string, any>, string>, type: TEnum[keyof TEnum], reasonMap: CompleteReasonMap<TEnum, TStringKey>, language?: Language | undefined, otherVars?: Record<string, string | number> | undefined);
|
|
16
41
|
}
|
|
17
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Plugin-based TypedError that works with the new component registration system
|
|
44
|
+
*/
|
|
45
|
+
export declare abstract class PluginTypedError<TEnum extends Record<string, string>, TStringKey extends string, TLanguages extends string = CoreLanguage> extends Error {
|
|
46
|
+
readonly componentId: string;
|
|
47
|
+
readonly type: TEnum[keyof TEnum];
|
|
48
|
+
readonly reasonMap: CompleteReasonMap<TEnum, TStringKey>;
|
|
49
|
+
readonly language?: TLanguages | undefined;
|
|
50
|
+
readonly otherVars?: Record<string, string | number> | undefined;
|
|
51
|
+
constructor(engine: PluginI18nEngine<TLanguages>, componentId: string, type: TEnum[keyof TEnum], reasonMap: CompleteReasonMap<TEnum, TStringKey>, language?: TLanguages | undefined, otherVars?: Record<string, string | number> | undefined);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Core system TypedError using the core component strings
|
|
55
|
+
*/
|
|
56
|
+
export declare abstract class CoreTypedError<TEnum extends Record<string, string>> extends Error {
|
|
57
|
+
readonly type: TEnum[keyof TEnum];
|
|
58
|
+
readonly reasonMap: CompleteReasonMap<TEnum, CoreStringKey>;
|
|
59
|
+
readonly language?: CoreLanguage | undefined;
|
|
60
|
+
readonly otherVars?: Record<string, string | number> | undefined;
|
|
61
|
+
constructor(engine: PluginI18nEngine<CoreLanguage>, type: TEnum[keyof TEnum], reasonMap: CompleteReasonMap<TEnum, CoreStringKey>, language?: CoreLanguage | undefined, otherVars?: Record<string, string | number> | undefined);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Helper function to create a plugin-based TypedError with automatic engine detection
|
|
65
|
+
*/
|
|
66
|
+
export declare function createPluginTypedError<TEnum extends Record<string, string>, TStringKey extends string, TLanguages extends string = CoreLanguage>(componentId: string, type: TEnum[keyof TEnum], reasonMap: CompleteReasonMap<TEnum, TStringKey>, otherVars?: Record<string, string | number>, language?: TLanguages, instanceKey?: string): Error;
|
|
67
|
+
/**
|
|
68
|
+
* Helper function to create a core system TypedError with automatic engine detection
|
|
69
|
+
*/
|
|
70
|
+
export declare function createCoreTypedError<TEnum extends Record<string, string>>(type: TEnum[keyof TEnum], reasonMap: CompleteReasonMap<TEnum, CoreStringKey>, otherVars?: Record<string, string | number>, language?: CoreLanguage, instanceKey?: string): Error;
|
|
71
|
+
/**
|
|
72
|
+
* Create a simple error with translation support (generalized pattern from RegistryError)
|
|
73
|
+
*/
|
|
74
|
+
export declare function createTranslatedError<TEnum extends Record<string, string>, TStringKey extends string>(engine: TranslationEngine, componentId: string, type: TEnum[keyof TEnum], reasonMap: Record<TEnum[keyof TEnum], TStringKey>, variables?: Record<string, string | number>, language?: string, metadata?: Record<string, any>, errorName?: string): Error;
|
|
75
|
+
/**
|
|
76
|
+
* Example usage of the new plugin-based TypedError system
|
|
77
|
+
*/
|