@digitaldefiance/i18n-lib 1.3.7 → 1.3.9
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 +8 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.js +6 -1
- package/dist/plugin-translatable-generic-error.d.ts +29 -0
- package/dist/plugin-translatable-generic-error.js +66 -0
- package/dist/plugin-translatable-handleable-generic.d.ts +28 -0
- package/dist/plugin-translatable-handleable-generic.js +40 -0
- package/dist/plugin-typed-handleable.d.ts +14 -0
- package/dist/plugin-typed-handleable.js +45 -0
- package/dist/translatable.d.ts +5 -0
- package/dist/translatable.js +11 -0
- package/dist/typed-handleable.d.ts +14 -0
- package/dist/typed-handleable.js +40 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2274,6 +2274,14 @@ For issues, questions, or contributions:
|
|
|
2274
2274
|
|
|
2275
2275
|
## ChangeLog
|
|
2276
2276
|
|
|
2277
|
+
### Version 1.3.9
|
|
2278
|
+
|
|
2279
|
+
- Add more handleable/typed classes
|
|
2280
|
+
|
|
2281
|
+
### Version 1.3.8
|
|
2282
|
+
|
|
2283
|
+
- Add TypedHandleable
|
|
2284
|
+
|
|
2277
2285
|
### Version 1.3.7
|
|
2278
2286
|
|
|
2279
2287
|
- Add handleable at i18n level
|
package/dist/index.d.ts
CHANGED
|
@@ -21,8 +21,9 @@ export * from './i18n-engine';
|
|
|
21
21
|
export * from './language-definition';
|
|
22
22
|
export * from './template';
|
|
23
23
|
export * from './timezone';
|
|
24
|
-
export * from './translatable-generic-error';
|
|
24
|
+
export * from './plugin-translatable-generic-error';
|
|
25
25
|
export * from './typed-error';
|
|
26
|
+
export * from './typed-handleable';
|
|
26
27
|
export * from './types';
|
|
27
28
|
export * from './utils';
|
|
28
29
|
export * from './validation-config';
|
|
@@ -32,12 +33,16 @@ export * from './core-string-key';
|
|
|
32
33
|
export * from './language-codes';
|
|
33
34
|
export * from './language-registry';
|
|
34
35
|
export * from './plugin-i18n-engine';
|
|
36
|
+
export * from './plugin-translatable-generic-error';
|
|
37
|
+
export * from './plugin-translatable-handleable-generic';
|
|
38
|
+
export * from './plugin-typed-handleable';
|
|
35
39
|
export * from './registry-config';
|
|
36
40
|
export * from './registry-error';
|
|
37
41
|
export * from './registry-error-type';
|
|
38
42
|
export * from './translation-engine';
|
|
39
43
|
export * from './translation-request';
|
|
40
44
|
export * from './translation-response';
|
|
45
|
+
export * from './translatable';
|
|
41
46
|
export * from './create-translation-adapter';
|
|
42
47
|
export { createCoreI18nEngine as createCoreI18n } from './core-i18n';
|
|
43
48
|
export { I18nEngine as I18n } from './i18n-engine';
|
package/dist/index.js
CHANGED
|
@@ -40,8 +40,9 @@ __exportStar(require("./i18n-engine"), exports);
|
|
|
40
40
|
__exportStar(require("./language-definition"), exports);
|
|
41
41
|
__exportStar(require("./template"), exports);
|
|
42
42
|
__exportStar(require("./timezone"), exports);
|
|
43
|
-
__exportStar(require("./translatable-generic-error"), exports);
|
|
43
|
+
__exportStar(require("./plugin-translatable-generic-error"), exports);
|
|
44
44
|
__exportStar(require("./typed-error"), exports);
|
|
45
|
+
__exportStar(require("./typed-handleable"), exports);
|
|
45
46
|
__exportStar(require("./types"), exports);
|
|
46
47
|
__exportStar(require("./utils"), exports);
|
|
47
48
|
__exportStar(require("./validation-config"), exports);
|
|
@@ -51,12 +52,16 @@ __exportStar(require("./core-string-key"), exports);
|
|
|
51
52
|
__exportStar(require("./language-codes"), exports);
|
|
52
53
|
__exportStar(require("./language-registry"), exports);
|
|
53
54
|
__exportStar(require("./plugin-i18n-engine"), exports);
|
|
55
|
+
__exportStar(require("./plugin-translatable-generic-error"), exports);
|
|
56
|
+
__exportStar(require("./plugin-translatable-handleable-generic"), exports);
|
|
57
|
+
__exportStar(require("./plugin-typed-handleable"), exports);
|
|
54
58
|
__exportStar(require("./registry-config"), exports);
|
|
55
59
|
__exportStar(require("./registry-error"), exports);
|
|
56
60
|
__exportStar(require("./registry-error-type"), exports);
|
|
57
61
|
__exportStar(require("./translation-engine"), exports);
|
|
58
62
|
__exportStar(require("./translation-request"), exports);
|
|
59
63
|
__exportStar(require("./translation-response"), exports);
|
|
64
|
+
__exportStar(require("./translatable"), exports);
|
|
60
65
|
__exportStar(require("./create-translation-adapter"), exports);
|
|
61
66
|
// Re-export for convenience
|
|
62
67
|
var core_i18n_1 = require("./core-i18n");
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { PluginI18nEngine } from './plugin-i18n-engine';
|
|
2
|
+
/**
|
|
3
|
+
* Generic translatable error that works with any plugin engine and component
|
|
4
|
+
*/
|
|
5
|
+
export declare class PluginTranslatableGenericError<TStringKey extends string = string, TLanguage extends string = string> extends Error {
|
|
6
|
+
readonly stringKey: TStringKey;
|
|
7
|
+
readonly componentId: string;
|
|
8
|
+
readonly language?: TLanguage;
|
|
9
|
+
readonly variables?: Record<string, string | number>;
|
|
10
|
+
readonly metadata?: Record<string, any>;
|
|
11
|
+
/**
|
|
12
|
+
* Create a translatable error
|
|
13
|
+
* @param componentId - The component ID to translate from
|
|
14
|
+
* @param stringKey - The translation key
|
|
15
|
+
* @param variables - Variables for interpolation
|
|
16
|
+
* @param language - Optional language override
|
|
17
|
+
* @param metadata - Additional error metadata
|
|
18
|
+
* @param instanceKey - Optional engine instance key
|
|
19
|
+
*/
|
|
20
|
+
constructor(componentId: string, stringKey: TStringKey, variables?: Record<string, string | number>, language?: TLanguage, metadata?: Record<string, any>, instanceKey?: string);
|
|
21
|
+
/**
|
|
22
|
+
* Create error with explicit engine instance
|
|
23
|
+
*/
|
|
24
|
+
static withEngine<TStringKey extends string, TLanguage extends string>(engine: PluginI18nEngine<TLanguage>, componentId: string, stringKey: TStringKey, variables?: Record<string, string | number>, language?: TLanguage, metadata?: Record<string, any>): PluginTranslatableGenericError<TStringKey, TLanguage>;
|
|
25
|
+
/**
|
|
26
|
+
* Retranslate the error message in a different language
|
|
27
|
+
*/
|
|
28
|
+
retranslate(language: TLanguage, instanceKey?: string): string;
|
|
29
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PluginTranslatableGenericError = void 0;
|
|
4
|
+
const plugin_i18n_engine_1 = require("./plugin-i18n-engine");
|
|
5
|
+
/**
|
|
6
|
+
* Generic translatable error that works with any plugin engine and component
|
|
7
|
+
*/
|
|
8
|
+
class PluginTranslatableGenericError extends Error {
|
|
9
|
+
/**
|
|
10
|
+
* Create a translatable error
|
|
11
|
+
* @param componentId - The component ID to translate from
|
|
12
|
+
* @param stringKey - The translation key
|
|
13
|
+
* @param variables - Variables for interpolation
|
|
14
|
+
* @param language - Optional language override
|
|
15
|
+
* @param metadata - Additional error metadata
|
|
16
|
+
* @param instanceKey - Optional engine instance key
|
|
17
|
+
*/
|
|
18
|
+
constructor(componentId, stringKey, variables, language, metadata, instanceKey) {
|
|
19
|
+
let translatedMessage;
|
|
20
|
+
try {
|
|
21
|
+
const engine = plugin_i18n_engine_1.PluginI18nEngine.getInstance(instanceKey);
|
|
22
|
+
translatedMessage = engine.safeTranslate(componentId, stringKey, variables, language);
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
// If engine not found or translation fails, use fallback format
|
|
26
|
+
translatedMessage = `[${componentId}.${stringKey}]`;
|
|
27
|
+
}
|
|
28
|
+
super(translatedMessage);
|
|
29
|
+
this.name = 'TranslatableGenericError';
|
|
30
|
+
this.stringKey = stringKey;
|
|
31
|
+
this.componentId = componentId;
|
|
32
|
+
this.language = language;
|
|
33
|
+
this.variables = variables;
|
|
34
|
+
this.metadata = metadata;
|
|
35
|
+
// Ensure proper prototype chain for instanceof checks across transpiled targets
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Create error with explicit engine instance
|
|
39
|
+
*/
|
|
40
|
+
static withEngine(engine, componentId, stringKey, variables, language, metadata) {
|
|
41
|
+
const translatedMessage = engine.safeTranslate(componentId, stringKey, variables, language);
|
|
42
|
+
const error = Object.create(PluginTranslatableGenericError.prototype);
|
|
43
|
+
Error.call(error, translatedMessage);
|
|
44
|
+
error.name = 'TranslatableGenericError';
|
|
45
|
+
error.stringKey = stringKey;
|
|
46
|
+
error.componentId = componentId;
|
|
47
|
+
error.language = language;
|
|
48
|
+
error.variables = variables;
|
|
49
|
+
error.metadata = metadata;
|
|
50
|
+
error.message = translatedMessage;
|
|
51
|
+
return error;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Retranslate the error message in a different language
|
|
55
|
+
*/
|
|
56
|
+
retranslate(language, instanceKey) {
|
|
57
|
+
try {
|
|
58
|
+
const engine = plugin_i18n_engine_1.PluginI18nEngine.getInstance(instanceKey);
|
|
59
|
+
return engine.safeTranslate(this.componentId, this.stringKey, this.variables, language);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
return `[${this.componentId}.${this.stringKey}]`;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.PluginTranslatableGenericError = PluginTranslatableGenericError;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { IHandleable } from './i-handleable';
|
|
2
|
+
import { PluginTranslatableGenericError } from './plugin-translatable-generic-error';
|
|
3
|
+
/**
|
|
4
|
+
* Generic translatable error that works with any plugin engine and component
|
|
5
|
+
*/
|
|
6
|
+
export declare class PluginTranslatableHandleableGenericError<TStringKey extends string = string, TLanguage extends string = string> extends PluginTranslatableGenericError<TStringKey, TLanguage> implements IHandleable {
|
|
7
|
+
private _handled;
|
|
8
|
+
readonly cause?: Error;
|
|
9
|
+
readonly statusCode: number;
|
|
10
|
+
readonly sourceData?: unknown;
|
|
11
|
+
/**
|
|
12
|
+
* Create a translatable error
|
|
13
|
+
* @param componentId - The component ID to translate from
|
|
14
|
+
* @param stringKey - The translation key
|
|
15
|
+
* @param variables - Variables for interpolation
|
|
16
|
+
* @param language - Optional language override
|
|
17
|
+
* @param metadata - Additional error metadata
|
|
18
|
+
* @param instanceKey - Optional engine instance key
|
|
19
|
+
*/
|
|
20
|
+
constructor(componentId: string, stringKey: TStringKey, variables?: Record<string, string | number>, language?: TLanguage, metadata?: Record<string, any>, instanceKey?: string, handleableOptions?: {
|
|
21
|
+
statusCode?: number;
|
|
22
|
+
cause?: Error;
|
|
23
|
+
sourceData?: unknown;
|
|
24
|
+
});
|
|
25
|
+
get handled(): boolean;
|
|
26
|
+
set handled(value: boolean);
|
|
27
|
+
toJSON(): Record<string, unknown>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PluginTranslatableHandleableGenericError = void 0;
|
|
4
|
+
const plugin_translatable_generic_error_1 = require("./plugin-translatable-generic-error");
|
|
5
|
+
/**
|
|
6
|
+
* Generic translatable error that works with any plugin engine and component
|
|
7
|
+
*/
|
|
8
|
+
class PluginTranslatableHandleableGenericError extends plugin_translatable_generic_error_1.PluginTranslatableGenericError {
|
|
9
|
+
/**
|
|
10
|
+
* Create a translatable error
|
|
11
|
+
* @param componentId - The component ID to translate from
|
|
12
|
+
* @param stringKey - The translation key
|
|
13
|
+
* @param variables - Variables for interpolation
|
|
14
|
+
* @param language - Optional language override
|
|
15
|
+
* @param metadata - Additional error metadata
|
|
16
|
+
* @param instanceKey - Optional engine instance key
|
|
17
|
+
*/
|
|
18
|
+
constructor(componentId, stringKey, variables, language, metadata, instanceKey, handleableOptions) {
|
|
19
|
+
super(componentId, stringKey, variables, language, metadata, instanceKey);
|
|
20
|
+
this._handled = false;
|
|
21
|
+
this.statusCode = handleableOptions?.statusCode ?? 500;
|
|
22
|
+
this.cause = handleableOptions?.cause;
|
|
23
|
+
this.sourceData = handleableOptions?.sourceData;
|
|
24
|
+
}
|
|
25
|
+
get handled() {
|
|
26
|
+
return this._handled;
|
|
27
|
+
}
|
|
28
|
+
set handled(value) {
|
|
29
|
+
this._handled = value;
|
|
30
|
+
}
|
|
31
|
+
toJSON() {
|
|
32
|
+
return {
|
|
33
|
+
statusCode: this.statusCode,
|
|
34
|
+
message: this.message,
|
|
35
|
+
cause: this.cause,
|
|
36
|
+
sourceData: this.sourceData,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.PluginTranslatableHandleableGenericError = PluginTranslatableHandleableGenericError;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { HandleableErrorOptions } from './i-handleable-error-options';
|
|
2
|
+
import { IHandleable } from './i-handleable';
|
|
3
|
+
import { CompleteReasonMap, PluginTypedError } from './typed-error';
|
|
4
|
+
import { PluginI18nEngine } from './plugin-i18n-engine';
|
|
5
|
+
export declare class PluginTypedHandleableError<TEnum extends Record<string, string>, TStringKey extends string, TLanguages extends string = string> extends PluginTypedError<TEnum, TStringKey, TLanguages> implements IHandleable {
|
|
6
|
+
readonly cause?: Error;
|
|
7
|
+
readonly statusCode: number;
|
|
8
|
+
readonly sourceData?: unknown;
|
|
9
|
+
private _handled;
|
|
10
|
+
constructor(engine: PluginI18nEngine<TLanguages>, componentId: string, type: TEnum[keyof TEnum], reasonMap: CompleteReasonMap<TEnum, TStringKey>, source: Error, options?: HandleableErrorOptions, language?: TLanguages, otherVars?: Record<string, string | number>);
|
|
11
|
+
get handled(): boolean;
|
|
12
|
+
set handled(value: boolean);
|
|
13
|
+
toJSON(): Record<string, unknown>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PluginTypedHandleableError = void 0;
|
|
4
|
+
const handleable_1 = require("./handleable");
|
|
5
|
+
const typed_error_1 = require("./typed-error");
|
|
6
|
+
class PluginTypedHandleableError extends typed_error_1.PluginTypedError {
|
|
7
|
+
constructor(engine, componentId, type, reasonMap, source, options, language, otherVars) {
|
|
8
|
+
super(engine, componentId, type, reasonMap, language, otherVars);
|
|
9
|
+
this.cause = options?.cause ?? source;
|
|
10
|
+
this.statusCode = options?.statusCode ?? 500;
|
|
11
|
+
this._handled = options?.handled ?? false;
|
|
12
|
+
this.sourceData = options?.sourceData;
|
|
13
|
+
// Capture stack trace - prioritize source stack, then capture new one
|
|
14
|
+
if (source.stack) {
|
|
15
|
+
this.stack = source.stack;
|
|
16
|
+
}
|
|
17
|
+
else if (Error.captureStackTrace) {
|
|
18
|
+
Error.captureStackTrace(this, this.constructor);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
this.stack = new Error().stack;
|
|
22
|
+
}
|
|
23
|
+
this.name = this.constructor.name;
|
|
24
|
+
}
|
|
25
|
+
get handled() {
|
|
26
|
+
return this._handled;
|
|
27
|
+
}
|
|
28
|
+
set handled(value) {
|
|
29
|
+
this._handled = value;
|
|
30
|
+
}
|
|
31
|
+
toJSON() {
|
|
32
|
+
return {
|
|
33
|
+
name: this.name,
|
|
34
|
+
message: this.message,
|
|
35
|
+
statusCode: this.statusCode,
|
|
36
|
+
handled: this.handled,
|
|
37
|
+
stack: this.stack,
|
|
38
|
+
cause: this.cause instanceof handleable_1.HandleableError || this.cause instanceof PluginTypedHandleableError
|
|
39
|
+
? this.cause.toJSON()
|
|
40
|
+
: this.cause?.message,
|
|
41
|
+
...(this.sourceData ? { sourceData: this.sourceData } : {}),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.PluginTypedHandleableError = PluginTypedHandleableError;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { I18nEngine } from './i18n-engine';
|
|
2
|
+
import { Language } from './default-config';
|
|
3
|
+
export declare class TranslatableError<TStringKey extends string> extends Error {
|
|
4
|
+
constructor(engine: I18nEngine<TStringKey, Language, any, any>, string: TStringKey, otherVars?: Record<string, string | number>, language?: Language);
|
|
5
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TranslatableError = void 0;
|
|
4
|
+
class TranslatableError extends Error {
|
|
5
|
+
constructor(engine, string, otherVars, language) {
|
|
6
|
+
super(engine.translate(string, otherVars, language));
|
|
7
|
+
this.name = 'TranslatableError';
|
|
8
|
+
Object.setPrototypeOf(this, TranslatableError.prototype);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.TranslatableError = TranslatableError;
|
|
@@ -0,0 +1,14 @@
|
|
|
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 { Language } from './default-config';
|
|
6
|
+
export declare class TypedHandleableError<TEnum extends Record<string, string>, TStringKey extends string> extends HandleableError implements IHandleable {
|
|
7
|
+
readonly type: TEnum[keyof TEnum];
|
|
8
|
+
readonly reasonMap: CompleteReasonMap<TEnum, TStringKey>;
|
|
9
|
+
readonly engine: TranslationEngine<TStringKey>;
|
|
10
|
+
readonly language?: Language;
|
|
11
|
+
readonly otherVars?: Record<string, string | number>;
|
|
12
|
+
constructor(type: TEnum[keyof TEnum], reasonMap: CompleteReasonMap<TEnum, TStringKey>, engine: TranslationEngine<TStringKey>, language?: Language, otherVars?: Record<string, string | number>, options?: HandleableErrorOptions);
|
|
13
|
+
toJSON(): Record<string, unknown>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TypedHandleableError = void 0;
|
|
4
|
+
const handleable_1 = require("./handleable");
|
|
5
|
+
const i18n_engine_1 = require("./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 = i18n_engine_1.I18nEngine.getInstance();
|
|
12
|
+
throw new Error(coreEngine.translate(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;
|