@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.
- package/README.md +143 -87
- package/package.json +4 -3
- package/src/active-context.d.ts +30 -0
- package/src/active-context.d.ts.map +1 -0
- package/src/active-context.js +3 -0
- package/src/active-context.js.map +1 -0
- package/src/component-definition.d.ts +12 -0
- package/src/component-definition.d.ts.map +1 -0
- package/src/component-definition.js +3 -0
- package/src/component-definition.js.map +1 -0
- package/src/component-registration.d.ts +10 -0
- package/src/component-registration.d.ts.map +1 -0
- package/src/component-registration.js +3 -0
- package/src/component-registration.js.map +1 -0
- package/src/component-registry.d.ts +69 -0
- package/src/component-registry.d.ts.map +1 -0
- package/src/component-registry.js +248 -0
- package/src/component-registry.js.map +1 -0
- package/src/context-error-type.d.ts +4 -0
- package/src/context-error-type.d.ts.map +1 -0
- package/src/context-error-type.js +8 -0
- package/src/context-error-type.js.map +1 -0
- package/src/context-error.d.ts +7 -0
- package/src/context-error.d.ts.map +1 -0
- package/src/context-error.js +18 -0
- package/src/context-error.js.map +1 -0
- package/src/context-manager.d.ts +34 -0
- package/src/context-manager.d.ts.map +1 -0
- package/src/context-manager.js +60 -0
- package/src/context-manager.js.map +1 -0
- package/src/context.d.ts +45 -0
- package/src/context.d.ts.map +1 -0
- package/src/context.js +70 -0
- package/src/context.js.map +1 -0
- package/src/core-i18n.d.ts +63 -0
- package/src/core-i18n.d.ts.map +1 -0
- package/src/core-i18n.js +478 -0
- package/src/core-i18n.js.map +1 -0
- package/src/core-string-key.d.ts +43 -0
- package/src/core-string-key.d.ts.map +1 -0
- package/src/core-string-key.js +51 -0
- package/src/core-string-key.js.map +1 -0
- package/src/create-translation-adapter.d.ts +21 -0
- package/src/create-translation-adapter.d.ts.map +1 -0
- package/src/create-translation-adapter.js +37 -0
- package/src/create-translation-adapter.js.map +1 -0
- package/src/currency-code.d.ts +20 -0
- package/src/currency-code.d.ts.map +1 -0
- package/src/currency-code.js +37 -0
- package/src/currency-code.js.map +1 -0
- package/src/{currency-format.ts → currency-format.d.ts} +5 -5
- package/src/currency-format.d.ts.map +1 -0
- package/src/currency-format.js +3 -0
- package/src/currency-format.js.map +1 -0
- package/src/currency.d.ts +12 -0
- package/src/currency.d.ts.map +1 -0
- package/src/currency.js +49 -0
- package/src/currency.js.map +1 -0
- package/src/default-config.d.ts +33 -0
- package/src/default-config.d.ts.map +1 -0
- package/src/default-config.js +102 -0
- package/src/default-config.js.map +1 -0
- package/src/enum-registry.d.ts +45 -0
- package/src/enum-registry.d.ts.map +1 -0
- package/src/enum-registry.js +103 -0
- package/src/enum-registry.js.map +1 -0
- package/src/global-active-context.d.ts +51 -0
- package/src/global-active-context.d.ts.map +1 -0
- package/src/global-active-context.js +179 -0
- package/src/global-active-context.js.map +1 -0
- package/src/handleable.d.ts +14 -0
- package/src/handleable.d.ts.map +1 -0
- package/src/handleable.js +66 -0
- package/src/handleable.js.map +1 -0
- package/src/i-global-active-context.d.ts +23 -0
- package/src/i-global-active-context.d.ts.map +1 -0
- package/src/i-global-active-context.js +3 -0
- package/src/i-global-active-context.js.map +1 -0
- package/src/i-handleable-error-options.d.ts +7 -0
- package/src/i-handleable-error-options.d.ts.map +1 -0
- package/src/i-handleable-error-options.js +3 -0
- package/src/i-handleable-error-options.js.map +1 -0
- package/src/i-handleable.d.ts +6 -0
- package/src/i-handleable.d.ts.map +1 -0
- package/src/i-handleable.js +3 -0
- package/src/i-handleable.js.map +1 -0
- package/src/i18n-config.d.ts +21 -0
- package/src/i18n-config.d.ts.map +1 -0
- package/src/i18n-config.js +3 -0
- package/src/i18n-config.js.map +1 -0
- package/src/{i18n-context.ts → i18n-context.d.ts} +7 -7
- package/src/i18n-context.d.ts.map +1 -0
- package/src/i18n-context.js +3 -0
- package/src/i18n-context.js.map +1 -0
- package/src/i18n-engine.d.ts +179 -0
- package/src/i18n-engine.d.ts.map +1 -0
- package/src/i18n-engine.js +355 -0
- package/src/i18n-engine.js.map +1 -0
- package/src/{index.ts → index.d.ts} +2 -10
- package/src/index.d.ts.map +1 -0
- package/src/index.js +71 -0
- package/src/index.js.map +1 -0
- package/src/{language-codes.ts → language-codes.d.ts} +12 -23
- package/src/language-codes.d.ts.map +1 -0
- package/src/language-codes.js +32 -0
- package/src/language-codes.js.map +1 -0
- package/src/language-definition.d.ts +14 -0
- package/src/language-definition.d.ts.map +1 -0
- package/src/language-definition.js +3 -0
- package/src/language-definition.js.map +1 -0
- package/src/language-registry.d.ts +114 -0
- package/src/language-registry.d.ts.map +1 -0
- package/src/language-registry.js +217 -0
- package/src/language-registry.js.map +1 -0
- package/src/plugin-i18n-engine.d.ts +151 -0
- package/src/plugin-i18n-engine.d.ts.map +1 -0
- package/src/plugin-i18n-engine.js +380 -0
- package/src/plugin-i18n-engine.js.map +1 -0
- package/src/plugin-translatable-generic-error.d.ts +30 -0
- package/src/plugin-translatable-generic-error.d.ts.map +1 -0
- package/src/plugin-translatable-generic-error.js +72 -0
- package/src/plugin-translatable-generic-error.js.map +1 -0
- package/src/plugin-translatable-handleable-generic.d.ts +29 -0
- package/src/plugin-translatable-handleable-generic.d.ts.map +1 -0
- package/src/plugin-translatable-handleable-generic.js +44 -0
- package/src/plugin-translatable-handleable-generic.js.map +1 -0
- package/src/plugin-typed-handleable.d.ts +15 -0
- package/src/plugin-typed-handleable.d.ts.map +1 -0
- package/src/plugin-typed-handleable.js +51 -0
- package/src/plugin-typed-handleable.js.map +1 -0
- package/src/registry-config.d.ts +15 -0
- package/src/registry-config.d.ts.map +1 -0
- package/src/registry-config.js +3 -0
- package/src/registry-config.js.map +1 -0
- package/src/registry-error-type.d.ts +13 -0
- package/src/registry-error-type.d.ts.map +1 -0
- package/src/registry-error-type.js +17 -0
- package/src/registry-error-type.js.map +1 -0
- package/src/registry-error.d.ts +19 -0
- package/src/registry-error.d.ts.map +1 -0
- package/src/registry-error.js +48 -0
- package/src/registry-error.js.map +1 -0
- package/src/strict-types.d.ts +19 -0
- package/src/strict-types.d.ts.map +1 -0
- package/src/strict-types.js +18 -0
- package/src/strict-types.js.map +1 -0
- package/src/template.d.ts +13 -0
- package/src/template.d.ts.map +1 -0
- package/src/template.js +31 -0
- package/src/template.js.map +1 -0
- package/src/timezone.d.ts +12 -0
- package/src/timezone.d.ts.map +1 -0
- package/src/timezone.js +24 -0
- package/src/timezone.js.map +1 -0
- package/src/translatable.d.ts +6 -0
- package/src/translatable.d.ts.map +1 -0
- package/src/translatable.js +12 -0
- package/src/translatable.js.map +1 -0
- package/src/translation-engine.d.ts +9 -0
- package/src/translation-engine.d.ts.map +1 -0
- package/src/translation-engine.js +3 -0
- package/src/translation-engine.js.map +1 -0
- package/src/translation-request.d.ts +10 -0
- package/src/translation-request.d.ts.map +1 -0
- package/src/translation-request.js +3 -0
- package/src/translation-request.js.map +1 -0
- package/src/translation-response.d.ts +9 -0
- package/src/translation-response.d.ts.map +1 -0
- package/src/translation-response.js +3 -0
- package/src/translation-response.js.map +1 -0
- package/src/typed-error.d.ts +73 -0
- package/src/typed-error.d.ts.map +1 -0
- package/src/typed-error.js +267 -0
- package/src/typed-error.js.map +1 -0
- package/src/typed-handleable.d.ts +15 -0
- package/src/typed-handleable.d.ts.map +1 -0
- package/src/typed-handleable.js +46 -0
- package/src/typed-handleable.js.map +1 -0
- package/src/{types.ts → types.d.ts} +21 -75
- package/src/types.d.ts.map +1 -0
- package/src/types.js +19 -0
- package/src/types.js.map +1 -0
- package/src/unified-translator.d.ts +31 -0
- package/src/unified-translator.d.ts.map +1 -0
- package/src/unified-translator.js +71 -0
- package/src/unified-translator.js.map +1 -0
- package/src/utils.d.ts +65 -0
- package/src/utils.d.ts.map +1 -0
- package/src/utils.js +129 -0
- package/src/utils.js.map +1 -0
- package/src/validation-config.d.ts +12 -0
- package/src/validation-config.d.ts.map +1 -0
- package/src/validation-config.js +3 -0
- package/src/validation-config.js.map +1 -0
- package/src/validation-result.d.ts +13 -0
- package/src/validation-result.d.ts.map +1 -0
- package/src/validation-result.js +3 -0
- package/src/validation-result.js.map +1 -0
- package/LICENSE +0 -21
- package/src/active-context.ts +0 -30
- package/src/component-definition.ts +0 -11
- package/src/component-registration.ts +0 -13
- package/src/component-registry.ts +0 -392
- package/src/context-error-type.ts +0 -3
- package/src/context-error.ts +0 -16
- package/src/context-manager.ts +0 -71
- package/src/context.ts +0 -90
- package/src/core-i18n.ts +0 -609
- package/src/core-string-key.ts +0 -49
- package/src/create-translation-adapter.ts +0 -47
- package/src/currency-code.ts +0 -35
- package/src/currency.ts +0 -52
- package/src/default-config.ts +0 -199
- package/src/enum-registry.ts +0 -138
- package/src/global-active-context.ts +0 -255
- package/src/handleable.ts +0 -79
- package/src/i-global-active-context.ts +0 -59
- package/src/i-handleable-error-options.ts +0 -6
- package/src/i-handleable.ts +0 -5
- package/src/i18n-config.ts +0 -29
- package/src/i18n-engine.ts +0 -491
- package/src/language-definition.ts +0 -13
- package/src/language-registry.ts +0 -292
- package/src/plugin-i18n-engine.ts +0 -520
- package/src/plugin-translatable-generic-error.ts +0 -106
- package/src/plugin-translatable-handleable-generic.ts +0 -60
- package/src/plugin-typed-handleable.ts +0 -77
- package/src/registry-config.ts +0 -15
- package/src/registry-error-type.ts +0 -12
- package/src/registry-error.ts +0 -74
- package/src/strict-types.ts +0 -35
- package/src/template.ts +0 -63
- package/src/timezone.ts +0 -20
- package/src/translatable.ts +0 -15
- package/src/translation-engine.ts +0 -8
- package/src/translation-request.ts +0 -12
- package/src/translation-response.ts +0 -8
- package/src/typed-error.ts +0 -384
- package/src/typed-handleable.ts +0 -70
- package/src/unified-translator.ts +0 -96
- package/src/utils.ts +0 -213
- package/src/validation-config.ts +0 -11
- package/src/validation-result.ts +0 -12
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { HandleableError } from './handleable';
|
|
2
|
-
import { IHandleable } from './i-handleable';
|
|
3
|
-
import { HandleableErrorOptions } from './i-handleable-error-options';
|
|
4
|
-
import { PluginI18nEngine } from './plugin-i18n-engine';
|
|
5
|
-
import { CompleteReasonMap, PluginTypedError } from './typed-error';
|
|
6
|
-
|
|
7
|
-
type ErrorConstructorWithStack = ErrorConstructor & {
|
|
8
|
-
captureStackTrace?: (target: Error, constructorOpt?: Function) => void;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export class PluginTypedHandleableError<
|
|
12
|
-
TEnum extends Record<string, string>,
|
|
13
|
-
TStringKey extends string,
|
|
14
|
-
TLanguages extends string = string,
|
|
15
|
-
>
|
|
16
|
-
extends PluginTypedError<TEnum, TStringKey, TLanguages>
|
|
17
|
-
implements IHandleable
|
|
18
|
-
{
|
|
19
|
-
public override readonly cause?: Error;
|
|
20
|
-
public readonly statusCode: number;
|
|
21
|
-
public readonly sourceData?: unknown;
|
|
22
|
-
private _handled: boolean;
|
|
23
|
-
|
|
24
|
-
constructor(
|
|
25
|
-
engine: PluginI18nEngine<TLanguages>,
|
|
26
|
-
componentId: string,
|
|
27
|
-
type: TEnum[keyof TEnum],
|
|
28
|
-
reasonMap: CompleteReasonMap<TEnum, TStringKey>,
|
|
29
|
-
source: Error,
|
|
30
|
-
options?: HandleableErrorOptions,
|
|
31
|
-
language?: TLanguages,
|
|
32
|
-
otherVars?: Record<string, string | number>,
|
|
33
|
-
) {
|
|
34
|
-
super(engine, componentId, type, reasonMap, language, otherVars);
|
|
35
|
-
this.cause = options?.cause ?? source;
|
|
36
|
-
this.statusCode = options?.statusCode ?? 500;
|
|
37
|
-
this._handled = options?.handled ?? false;
|
|
38
|
-
this.sourceData = options?.sourceData;
|
|
39
|
-
|
|
40
|
-
// Capture stack trace - prioritize source stack, then capture new one
|
|
41
|
-
if (source.stack) {
|
|
42
|
-
this.stack = source.stack;
|
|
43
|
-
} else if ((Error as ErrorConstructorWithStack).captureStackTrace) {
|
|
44
|
-
(Error as ErrorConstructorWithStack).captureStackTrace?.(
|
|
45
|
-
this,
|
|
46
|
-
this.constructor,
|
|
47
|
-
);
|
|
48
|
-
} else {
|
|
49
|
-
this.stack = new Error().stack;
|
|
50
|
-
}
|
|
51
|
-
this.name = this.constructor.name;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
public get handled(): boolean {
|
|
55
|
-
return this._handled;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public set handled(value: boolean) {
|
|
59
|
-
this._handled = value;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
public toJSON(): Record<string, unknown> {
|
|
63
|
-
return {
|
|
64
|
-
name: this.name,
|
|
65
|
-
message: this.message,
|
|
66
|
-
statusCode: this.statusCode,
|
|
67
|
-
handled: this.handled,
|
|
68
|
-
stack: this.stack,
|
|
69
|
-
cause:
|
|
70
|
-
this.cause instanceof HandleableError ||
|
|
71
|
-
this.cause instanceof PluginTypedHandleableError
|
|
72
|
-
? this.cause.toJSON()
|
|
73
|
-
: this.cause?.message,
|
|
74
|
-
...(this.sourceData ? { sourceData: this.sourceData } : {}),
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
}
|
package/src/registry-config.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { CurrencyCode } from './currency-code';
|
|
2
|
-
import { Timezone } from './timezone';
|
|
3
|
-
import { ValidationConfig } from './validation-config';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Registry configuration
|
|
7
|
-
*/
|
|
8
|
-
export interface RegistryConfig<TLanguages extends string> {
|
|
9
|
-
readonly defaultLanguage: TLanguages;
|
|
10
|
-
readonly fallbackLanguage: TLanguages;
|
|
11
|
-
readonly defaultCurrencyCode: CurrencyCode;
|
|
12
|
-
readonly timezone: Timezone;
|
|
13
|
-
readonly adminTimezone: Timezone;
|
|
14
|
-
readonly validation: ValidationConfig;
|
|
15
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Error types for registry operations
|
|
3
|
-
*/
|
|
4
|
-
export 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
|
-
}
|
package/src/registry-error.ts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
// CoreLanguageCode is deprecated - using string
|
|
2
|
-
import { CoreStringKey } from './core-string-key';
|
|
3
|
-
import { RegistryErrorType } from './registry-error-type';
|
|
4
|
-
import { TranslationEngine, createTranslatedError } from './typed-error';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Reason map for registry errors
|
|
8
|
-
*/
|
|
9
|
-
const REGISTRY_ERROR_REASON_MAP = {
|
|
10
|
-
[RegistryErrorType.ComponentNotFound]:
|
|
11
|
-
CoreStringKey.Error_ComponentNotFoundTemplate,
|
|
12
|
-
[RegistryErrorType.DuplicateComponent]:
|
|
13
|
-
CoreStringKey.Error_DuplicateComponentTemplate,
|
|
14
|
-
[RegistryErrorType.DuplicateLanguage]:
|
|
15
|
-
CoreStringKey.Error_DuplicateLanguageTemplate,
|
|
16
|
-
[RegistryErrorType.IncompleteRegistration]:
|
|
17
|
-
CoreStringKey.Error_IncompleteRegistrationTemplate,
|
|
18
|
-
[RegistryErrorType.LanguageNotFound]:
|
|
19
|
-
CoreStringKey.Error_LanguageNotFoundTemplate,
|
|
20
|
-
[RegistryErrorType.StringKeyNotFound]:
|
|
21
|
-
CoreStringKey.Error_StringKeyNotFoundTemplate,
|
|
22
|
-
[RegistryErrorType.ValidationFailed]:
|
|
23
|
-
CoreStringKey.Error_ValidationFailedTemplate,
|
|
24
|
-
} as const;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Registry error class that can work with plugin engines
|
|
28
|
-
*/
|
|
29
|
-
export class RegistryError extends Error {
|
|
30
|
-
constructor(
|
|
31
|
-
public readonly type: RegistryErrorType,
|
|
32
|
-
message: string,
|
|
33
|
-
public readonly metadata?: Record<string, any>,
|
|
34
|
-
) {
|
|
35
|
-
super(message);
|
|
36
|
-
this.name = 'RegistryError';
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Create a registry error with translation support
|
|
41
|
-
*/
|
|
42
|
-
static createWithEngine(
|
|
43
|
-
engine: TranslationEngine,
|
|
44
|
-
type: RegistryErrorType,
|
|
45
|
-
variables?: Record<string, string | number>,
|
|
46
|
-
language?: string,
|
|
47
|
-
metadata?: Record<string, any>,
|
|
48
|
-
): RegistryError {
|
|
49
|
-
const error = createTranslatedError(
|
|
50
|
-
engine,
|
|
51
|
-
'core',
|
|
52
|
-
type,
|
|
53
|
-
REGISTRY_ERROR_REASON_MAP,
|
|
54
|
-
variables,
|
|
55
|
-
language,
|
|
56
|
-
metadata,
|
|
57
|
-
'RegistryError',
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
// Convert to RegistryError instance
|
|
61
|
-
return new RegistryError(type, error.message, metadata);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Create a simple RegistryError without engine dependency
|
|
66
|
-
*/
|
|
67
|
-
static createSimple(
|
|
68
|
-
type: RegistryErrorType,
|
|
69
|
-
message: string,
|
|
70
|
-
metadata?: Record<string, any>,
|
|
71
|
-
): RegistryError {
|
|
72
|
-
return new RegistryError(type, message, metadata);
|
|
73
|
-
}
|
|
74
|
-
}
|
package/src/strict-types.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Enforces that for each language L, all string keys K are present.
|
|
3
|
-
*/
|
|
4
|
-
export type CompleteLanguageStrings<TStringKey extends string> = {
|
|
5
|
-
[K in TStringKey]: string;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export type CompleteComponentLanguageStrings<
|
|
9
|
-
TStringKey extends string,
|
|
10
|
-
TLanguages extends string,
|
|
11
|
-
> = {
|
|
12
|
-
[L in TLanguages]: CompleteLanguageStrings<TStringKey>;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Helper to assert at compile-time that an object is a complete component language map.
|
|
17
|
-
* Returns the object unchanged at runtime.
|
|
18
|
-
*/
|
|
19
|
-
export function createCompleteComponentStrings<
|
|
20
|
-
TStringKey extends string,
|
|
21
|
-
TLanguages extends string,
|
|
22
|
-
>(
|
|
23
|
-
obj: CompleteComponentLanguageStrings<TStringKey, TLanguages>,
|
|
24
|
-
): CompleteComponentLanguageStrings<TStringKey, TLanguages> {
|
|
25
|
-
return obj;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Utility to extract missing keys at compile time (identity, purely for readability)
|
|
30
|
-
*/
|
|
31
|
-
export function defineLanguageStrings<TStringKey extends string>(
|
|
32
|
-
strings: CompleteLanguageStrings<TStringKey>,
|
|
33
|
-
): CompleteLanguageStrings<TStringKey> {
|
|
34
|
-
return strings;
|
|
35
|
-
}
|
package/src/template.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Template processing utilities for i18n
|
|
3
|
-
*/
|
|
4
|
-
export type EnumKeys<T> = keyof T;
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Recursive type to validate that all enum keys in the template string exist in the provided enum type
|
|
8
|
-
*/
|
|
9
|
-
export type IsValidEnumTemplate<
|
|
10
|
-
T extends string,
|
|
11
|
-
TEnum,
|
|
12
|
-
> = T extends `${string}{{${string}.${infer Key}}}${infer Rest}`
|
|
13
|
-
? Key extends EnumKeys<TEnum>
|
|
14
|
-
? IsValidEnumTemplate<Rest, TEnum>
|
|
15
|
-
: false
|
|
16
|
-
: true;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Template function that processes {{EnumName.EnumKey}} patterns
|
|
20
|
-
*/
|
|
21
|
-
export function createTemplateProcessor<
|
|
22
|
-
TEnum extends Record<string, string>,
|
|
23
|
-
TLanguage extends string,
|
|
24
|
-
>(
|
|
25
|
-
enumObj: TEnum,
|
|
26
|
-
translateFn: (
|
|
27
|
-
key: TEnum[keyof TEnum],
|
|
28
|
-
vars?: Record<string, string | number>,
|
|
29
|
-
language?: TLanguage,
|
|
30
|
-
) => string,
|
|
31
|
-
enumName: string,
|
|
32
|
-
) {
|
|
33
|
-
return function t<T extends string>(
|
|
34
|
-
str: IsValidEnumTemplate<T, TEnum> extends true ? T : never,
|
|
35
|
-
language?: TLanguage,
|
|
36
|
-
...otherVars: Record<string, string | number>[]
|
|
37
|
-
): string {
|
|
38
|
-
let varIndex = 0;
|
|
39
|
-
const pattern = new RegExp(`\\{\\{${enumName}\\.(\\w+)\\}\\}`, 'g');
|
|
40
|
-
|
|
41
|
-
// First replace enum patterns
|
|
42
|
-
let result = str.replace(pattern, (match, enumKey) => {
|
|
43
|
-
const enumValue = enumObj[enumKey as keyof TEnum];
|
|
44
|
-
if (!enumValue) {
|
|
45
|
-
return match; // Return original if enum key not found
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const needsVars = (enumValue as string)
|
|
49
|
-
.toLowerCase()
|
|
50
|
-
.endsWith('template');
|
|
51
|
-
const vars = needsVars ? otherVars[varIndex++] ?? {} : {};
|
|
52
|
-
return translateFn(enumValue, vars, language);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
// Then replace any remaining variables from all otherVars
|
|
56
|
-
const allVars = otherVars.reduce((acc, vars) => ({ ...acc, ...vars }), {});
|
|
57
|
-
result = result.replace(/\{(\w+)\}/g, (match, varName) => {
|
|
58
|
-
return allVars[varName] !== undefined ? String(allVars[varName]) : match;
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
return result;
|
|
62
|
-
};
|
|
63
|
-
}
|
package/src/timezone.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { isValidTimezone } from './utils';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Class representing a validated timezone.
|
|
5
|
-
*/
|
|
6
|
-
export class Timezone {
|
|
7
|
-
private readonly _timezone: string;
|
|
8
|
-
constructor(timezone: string) {
|
|
9
|
-
if (!isValidTimezone(timezone)) {
|
|
10
|
-
throw new Error(`Invalid timezone: ${timezone}`);
|
|
11
|
-
}
|
|
12
|
-
this._timezone = timezone;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Gets the timezone value.
|
|
16
|
-
*/
|
|
17
|
-
public get value(): string {
|
|
18
|
-
return this._timezone;
|
|
19
|
-
}
|
|
20
|
-
}
|
package/src/translatable.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { I18nEngine } from './i18n-engine';
|
|
2
|
-
import { Language } from './default-config';
|
|
3
|
-
|
|
4
|
-
export class TranslatableError<TStringKey extends string> extends Error {
|
|
5
|
-
constructor(
|
|
6
|
-
engine: I18nEngine<TStringKey, Language, any, any>,
|
|
7
|
-
string: TStringKey,
|
|
8
|
-
otherVars?: Record<string, string | number>,
|
|
9
|
-
language?: Language,
|
|
10
|
-
) {
|
|
11
|
-
super(engine.translate(string, otherVars, language));
|
|
12
|
-
this.name = 'TranslatableError';
|
|
13
|
-
Object.setPrototypeOf(this, TranslatableError.prototype);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Minimal interface for translation engines
|
|
3
|
-
* Allows flexibility in what can be used as a translation engine
|
|
4
|
-
*/
|
|
5
|
-
export interface TranslationEngine<TStringKey extends string = string> {
|
|
6
|
-
translate: (key: TStringKey, vars?: Record<string, string | number>, lang?: any) => string;
|
|
7
|
-
safeTranslate: (key: TStringKey, vars?: Record<string, string | number>, lang?: any) => string;
|
|
8
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Translation request interface
|
|
3
|
-
*/
|
|
4
|
-
export interface TranslationRequest<
|
|
5
|
-
TStringKeys extends string,
|
|
6
|
-
TLanguages extends string,
|
|
7
|
-
> {
|
|
8
|
-
readonly componentId: string;
|
|
9
|
-
readonly stringKey: TStringKeys;
|
|
10
|
-
readonly language?: TLanguages;
|
|
11
|
-
readonly variables?: Record<string, string | number>;
|
|
12
|
-
}
|