@digitaldefiance/i18n-lib 1.3.13 → 1.3.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (243) hide show
  1. package/README.md +143 -87
  2. package/package.json +4 -3
  3. package/src/active-context.d.ts +30 -0
  4. package/src/active-context.d.ts.map +1 -0
  5. package/src/active-context.js +3 -0
  6. package/src/active-context.js.map +1 -0
  7. package/src/component-definition.d.ts +12 -0
  8. package/src/component-definition.d.ts.map +1 -0
  9. package/src/component-definition.js +3 -0
  10. package/src/component-definition.js.map +1 -0
  11. package/src/component-registration.d.ts +10 -0
  12. package/src/component-registration.d.ts.map +1 -0
  13. package/src/component-registration.js +3 -0
  14. package/src/component-registration.js.map +1 -0
  15. package/src/component-registry.d.ts +69 -0
  16. package/src/component-registry.d.ts.map +1 -0
  17. package/src/component-registry.js +248 -0
  18. package/src/component-registry.js.map +1 -0
  19. package/src/context-error-type.d.ts +4 -0
  20. package/src/context-error-type.d.ts.map +1 -0
  21. package/src/context-error-type.js +8 -0
  22. package/src/context-error-type.js.map +1 -0
  23. package/src/context-error.d.ts +7 -0
  24. package/src/context-error.d.ts.map +1 -0
  25. package/src/context-error.js +18 -0
  26. package/src/context-error.js.map +1 -0
  27. package/src/context-manager.d.ts +34 -0
  28. package/src/context-manager.d.ts.map +1 -0
  29. package/src/context-manager.js +60 -0
  30. package/src/context-manager.js.map +1 -0
  31. package/src/context.d.ts +45 -0
  32. package/src/context.d.ts.map +1 -0
  33. package/src/context.js +70 -0
  34. package/src/context.js.map +1 -0
  35. package/src/core-i18n.d.ts +63 -0
  36. package/src/core-i18n.d.ts.map +1 -0
  37. package/src/core-i18n.js +478 -0
  38. package/src/core-i18n.js.map +1 -0
  39. package/src/core-string-key.d.ts +43 -0
  40. package/src/core-string-key.d.ts.map +1 -0
  41. package/src/core-string-key.js +51 -0
  42. package/src/core-string-key.js.map +1 -0
  43. package/src/create-translation-adapter.d.ts +21 -0
  44. package/src/create-translation-adapter.d.ts.map +1 -0
  45. package/src/create-translation-adapter.js +37 -0
  46. package/src/create-translation-adapter.js.map +1 -0
  47. package/src/currency-code.d.ts +20 -0
  48. package/src/currency-code.d.ts.map +1 -0
  49. package/src/currency-code.js +37 -0
  50. package/src/currency-code.js.map +1 -0
  51. package/src/{currency-format.ts → currency-format.d.ts} +5 -5
  52. package/src/currency-format.d.ts.map +1 -0
  53. package/src/currency-format.js +3 -0
  54. package/src/currency-format.js.map +1 -0
  55. package/src/currency.d.ts +12 -0
  56. package/src/currency.d.ts.map +1 -0
  57. package/src/currency.js +49 -0
  58. package/src/currency.js.map +1 -0
  59. package/src/default-config.d.ts +33 -0
  60. package/src/default-config.d.ts.map +1 -0
  61. package/src/default-config.js +102 -0
  62. package/src/default-config.js.map +1 -0
  63. package/src/enum-registry.d.ts +45 -0
  64. package/src/enum-registry.d.ts.map +1 -0
  65. package/src/enum-registry.js +103 -0
  66. package/src/enum-registry.js.map +1 -0
  67. package/src/global-active-context.d.ts +51 -0
  68. package/src/global-active-context.d.ts.map +1 -0
  69. package/src/global-active-context.js +179 -0
  70. package/src/global-active-context.js.map +1 -0
  71. package/src/handleable.d.ts +14 -0
  72. package/src/handleable.d.ts.map +1 -0
  73. package/src/handleable.js +66 -0
  74. package/src/handleable.js.map +1 -0
  75. package/src/i-global-active-context.d.ts +23 -0
  76. package/src/i-global-active-context.d.ts.map +1 -0
  77. package/src/i-global-active-context.js +3 -0
  78. package/src/i-global-active-context.js.map +1 -0
  79. package/src/i-handleable-error-options.d.ts +7 -0
  80. package/src/i-handleable-error-options.d.ts.map +1 -0
  81. package/src/i-handleable-error-options.js +3 -0
  82. package/src/i-handleable-error-options.js.map +1 -0
  83. package/src/i-handleable.d.ts +6 -0
  84. package/src/i-handleable.d.ts.map +1 -0
  85. package/src/i-handleable.js +3 -0
  86. package/src/i-handleable.js.map +1 -0
  87. package/src/i18n-config.d.ts +21 -0
  88. package/src/i18n-config.d.ts.map +1 -0
  89. package/src/i18n-config.js +3 -0
  90. package/src/i18n-config.js.map +1 -0
  91. package/src/{i18n-context.ts → i18n-context.d.ts} +7 -7
  92. package/src/i18n-context.d.ts.map +1 -0
  93. package/src/i18n-context.js +3 -0
  94. package/src/i18n-context.js.map +1 -0
  95. package/src/i18n-engine.d.ts +179 -0
  96. package/src/i18n-engine.d.ts.map +1 -0
  97. package/src/i18n-engine.js +355 -0
  98. package/src/i18n-engine.js.map +1 -0
  99. package/src/{index.ts → index.d.ts} +2 -10
  100. package/src/index.d.ts.map +1 -0
  101. package/src/index.js +71 -0
  102. package/src/index.js.map +1 -0
  103. package/src/{language-codes.ts → language-codes.d.ts} +12 -23
  104. package/src/language-codes.d.ts.map +1 -0
  105. package/src/language-codes.js +32 -0
  106. package/src/language-codes.js.map +1 -0
  107. package/src/language-definition.d.ts +14 -0
  108. package/src/language-definition.d.ts.map +1 -0
  109. package/src/language-definition.js +3 -0
  110. package/src/language-definition.js.map +1 -0
  111. package/src/language-registry.d.ts +114 -0
  112. package/src/language-registry.d.ts.map +1 -0
  113. package/src/language-registry.js +217 -0
  114. package/src/language-registry.js.map +1 -0
  115. package/src/plugin-i18n-engine.d.ts +151 -0
  116. package/src/plugin-i18n-engine.d.ts.map +1 -0
  117. package/src/plugin-i18n-engine.js +380 -0
  118. package/src/plugin-i18n-engine.js.map +1 -0
  119. package/src/plugin-translatable-generic-error.d.ts +30 -0
  120. package/src/plugin-translatable-generic-error.d.ts.map +1 -0
  121. package/src/plugin-translatable-generic-error.js +72 -0
  122. package/src/plugin-translatable-generic-error.js.map +1 -0
  123. package/src/plugin-translatable-handleable-generic.d.ts +29 -0
  124. package/src/plugin-translatable-handleable-generic.d.ts.map +1 -0
  125. package/src/plugin-translatable-handleable-generic.js +44 -0
  126. package/src/plugin-translatable-handleable-generic.js.map +1 -0
  127. package/src/plugin-typed-handleable.d.ts +15 -0
  128. package/src/plugin-typed-handleable.d.ts.map +1 -0
  129. package/src/plugin-typed-handleable.js +51 -0
  130. package/src/plugin-typed-handleable.js.map +1 -0
  131. package/src/registry-config.d.ts +15 -0
  132. package/src/registry-config.d.ts.map +1 -0
  133. package/src/registry-config.js +3 -0
  134. package/src/registry-config.js.map +1 -0
  135. package/src/registry-error-type.d.ts +13 -0
  136. package/src/registry-error-type.d.ts.map +1 -0
  137. package/src/registry-error-type.js +17 -0
  138. package/src/registry-error-type.js.map +1 -0
  139. package/src/registry-error.d.ts +19 -0
  140. package/src/registry-error.d.ts.map +1 -0
  141. package/src/registry-error.js +48 -0
  142. package/src/registry-error.js.map +1 -0
  143. package/src/strict-types.d.ts +19 -0
  144. package/src/strict-types.d.ts.map +1 -0
  145. package/src/strict-types.js +18 -0
  146. package/src/strict-types.js.map +1 -0
  147. package/src/template.d.ts +13 -0
  148. package/src/template.d.ts.map +1 -0
  149. package/src/template.js +31 -0
  150. package/src/template.js.map +1 -0
  151. package/src/timezone.d.ts +12 -0
  152. package/src/timezone.d.ts.map +1 -0
  153. package/src/timezone.js +24 -0
  154. package/src/timezone.js.map +1 -0
  155. package/src/translatable.d.ts +6 -0
  156. package/src/translatable.d.ts.map +1 -0
  157. package/src/translatable.js +12 -0
  158. package/src/translatable.js.map +1 -0
  159. package/src/translation-engine.d.ts +9 -0
  160. package/src/translation-engine.d.ts.map +1 -0
  161. package/src/translation-engine.js +3 -0
  162. package/src/translation-engine.js.map +1 -0
  163. package/src/translation-request.d.ts +10 -0
  164. package/src/translation-request.d.ts.map +1 -0
  165. package/src/translation-request.js +3 -0
  166. package/src/translation-request.js.map +1 -0
  167. package/src/translation-response.d.ts +9 -0
  168. package/src/translation-response.d.ts.map +1 -0
  169. package/src/translation-response.js +3 -0
  170. package/src/translation-response.js.map +1 -0
  171. package/src/typed-error.d.ts +73 -0
  172. package/src/typed-error.d.ts.map +1 -0
  173. package/src/typed-error.js +267 -0
  174. package/src/typed-error.js.map +1 -0
  175. package/src/typed-handleable.d.ts +15 -0
  176. package/src/typed-handleable.d.ts.map +1 -0
  177. package/src/typed-handleable.js +46 -0
  178. package/src/typed-handleable.js.map +1 -0
  179. package/src/{types.ts → types.d.ts} +21 -75
  180. package/src/types.d.ts.map +1 -0
  181. package/src/types.js +19 -0
  182. package/src/types.js.map +1 -0
  183. package/src/unified-translator.d.ts +31 -0
  184. package/src/unified-translator.d.ts.map +1 -0
  185. package/src/unified-translator.js +71 -0
  186. package/src/unified-translator.js.map +1 -0
  187. package/src/utils.d.ts +65 -0
  188. package/src/utils.d.ts.map +1 -0
  189. package/src/utils.js +129 -0
  190. package/src/utils.js.map +1 -0
  191. package/src/validation-config.d.ts +12 -0
  192. package/src/validation-config.d.ts.map +1 -0
  193. package/src/validation-config.js +3 -0
  194. package/src/validation-config.js.map +1 -0
  195. package/src/validation-result.d.ts +13 -0
  196. package/src/validation-result.d.ts.map +1 -0
  197. package/src/validation-result.js +3 -0
  198. package/src/validation-result.js.map +1 -0
  199. package/LICENSE +0 -21
  200. package/src/active-context.ts +0 -30
  201. package/src/component-definition.ts +0 -11
  202. package/src/component-registration.ts +0 -13
  203. package/src/component-registry.ts +0 -392
  204. package/src/context-error-type.ts +0 -3
  205. package/src/context-error.ts +0 -16
  206. package/src/context-manager.ts +0 -71
  207. package/src/context.ts +0 -90
  208. package/src/core-i18n.ts +0 -609
  209. package/src/core-string-key.ts +0 -49
  210. package/src/create-translation-adapter.ts +0 -47
  211. package/src/currency-code.ts +0 -35
  212. package/src/currency.ts +0 -52
  213. package/src/default-config.ts +0 -199
  214. package/src/enum-registry.ts +0 -138
  215. package/src/global-active-context.ts +0 -255
  216. package/src/handleable.ts +0 -79
  217. package/src/i-global-active-context.ts +0 -59
  218. package/src/i-handleable-error-options.ts +0 -6
  219. package/src/i-handleable.ts +0 -5
  220. package/src/i18n-config.ts +0 -29
  221. package/src/i18n-engine.ts +0 -491
  222. package/src/language-definition.ts +0 -13
  223. package/src/language-registry.ts +0 -292
  224. package/src/plugin-i18n-engine.ts +0 -520
  225. package/src/plugin-translatable-generic-error.ts +0 -106
  226. package/src/plugin-translatable-handleable-generic.ts +0 -60
  227. package/src/plugin-typed-handleable.ts +0 -77
  228. package/src/registry-config.ts +0 -15
  229. package/src/registry-error-type.ts +0 -12
  230. package/src/registry-error.ts +0 -74
  231. package/src/strict-types.ts +0 -35
  232. package/src/template.ts +0 -63
  233. package/src/timezone.ts +0 -20
  234. package/src/translatable.ts +0 -15
  235. package/src/translation-engine.ts +0 -8
  236. package/src/translation-request.ts +0 -12
  237. package/src/translation-response.ts +0 -8
  238. package/src/typed-error.ts +0 -384
  239. package/src/typed-handleable.ts +0 -70
  240. package/src/unified-translator.ts +0 -96
  241. package/src/utils.ts +0 -213
  242. package/src/validation-config.ts +0 -11
  243. package/src/validation-result.ts +0 -12
@@ -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)
@@ -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
- }