@digitaldefiance/i18n-lib 1.0.22 → 1.0.24

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.
@@ -0,0 +1,43 @@
1
+ import { CurrencyCode } from './currency-code';
2
+ import { Timezone } from './timezone';
3
+ import { I18nContext, LanguageContext } from './types';
4
+ /**
5
+ * Creates a new I18n context with default values.
6
+ * @param defaultLanguage - The default language for the context
7
+ * @param defaultContext - The default language context
8
+ * @param defaultCurrencyCode - The default currency code (defaults to USD)
9
+ * @param defaultTimezone - The default timezone (defaults to UTC)
10
+ * @param defaultAdminTimezone - The default admin timezone (defaults to UTC)
11
+ * @returns A new I18nContext instance
12
+ */
13
+ export declare function createContext<TLanguage extends string, TTranslationContext extends string = LanguageContext, TContext extends I18nContext<TLanguage, TTranslationContext> = I18nContext<TLanguage, TTranslationContext>>(defaultLanguage: TLanguage, defaultContext: TTranslationContext, defaultCurrencyCode?: CurrencyCode, defaultTimezone?: Timezone, defaultAdminTimezone?: Timezone): TContext;
14
+ /**
15
+ * Sets the language for the given I18n context.
16
+ * @param context - The I18n context to modify
17
+ * @param language - The language to set
18
+ */
19
+ export declare function setLanguage<TLanguage extends string, TContext extends string = LanguageContext>(context: I18nContext<TLanguage, TContext>, language: TLanguage): void;
20
+ /**
21
+ * Sets the admin language for the given I18n context.
22
+ * @param context - The I18n context to modify
23
+ * @param language - The admin language to set
24
+ */
25
+ export declare function setAdminLanguage<TLanguage extends string, TContext extends string = LanguageContext>(context: I18nContext<TLanguage, TContext>, language: TLanguage): void;
26
+ /**
27
+ * Sets the current context for the given I18n context.
28
+ * @param context - The I18n context to modify
29
+ * @param languageContext - The language context to set
30
+ */
31
+ export declare function setContext<TLanguage extends string, TContext extends string = LanguageContext>(context: I18nContext<TLanguage, TContext>, languageContext: TContext): void;
32
+ /**
33
+ * Sets the timezone for the given I18n context.
34
+ * @param context - The I18n context to modify
35
+ * @param timezone - The timezone to set
36
+ */
37
+ export declare function setTimezone<TLanguage extends string, TContext extends string = LanguageContext>(context: I18nContext<TLanguage, TContext>, timezone: Timezone): void;
38
+ /**
39
+ * Sets the admin timezone for the given I18n context.
40
+ * @param context - The I18n context to modify
41
+ * @param timezone - The admin timezone to set
42
+ */
43
+ export declare function setAdminTimezone<TLanguage extends string, TContext extends string = LanguageContext>(context: I18nContext<TLanguage, TContext>, timezone: Timezone): void;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createContext = createContext;
4
+ exports.setLanguage = setLanguage;
5
+ exports.setAdminLanguage = setAdminLanguage;
6
+ exports.setContext = setContext;
7
+ exports.setTimezone = setTimezone;
8
+ exports.setAdminTimezone = setAdminTimezone;
9
+ const currency_code_1 = require("./currency-code");
10
+ const timezone_1 = require("./timezone");
11
+ /**
12
+ * Creates a new I18n context with default values.
13
+ * @param defaultLanguage - The default language for the context
14
+ * @param defaultContext - The default language context
15
+ * @param defaultCurrencyCode - The default currency code (defaults to USD)
16
+ * @param defaultTimezone - The default timezone (defaults to UTC)
17
+ * @param defaultAdminTimezone - The default admin timezone (defaults to UTC)
18
+ * @returns A new I18nContext instance
19
+ */
20
+ function createContext(defaultLanguage, defaultContext, defaultCurrencyCode = new currency_code_1.CurrencyCode('USD'), defaultTimezone = new timezone_1.Timezone('UTC'), defaultAdminTimezone = new timezone_1.Timezone('UTC')) {
21
+ return {
22
+ language: defaultLanguage,
23
+ adminLanguage: defaultLanguage,
24
+ currencyCode: defaultCurrencyCode,
25
+ currentContext: defaultContext,
26
+ timezone: defaultTimezone,
27
+ adminTimezone: defaultAdminTimezone,
28
+ };
29
+ }
30
+ /**
31
+ * Sets the language for the given I18n context.
32
+ * @param context - The I18n context to modify
33
+ * @param language - The language to set
34
+ */
35
+ function setLanguage(context, language) {
36
+ context.language = language;
37
+ }
38
+ /**
39
+ * Sets the admin language for the given I18n context.
40
+ * @param context - The I18n context to modify
41
+ * @param language - The admin language to set
42
+ */
43
+ function setAdminLanguage(context, language) {
44
+ context.adminLanguage = language;
45
+ }
46
+ /**
47
+ * Sets the current context for the given I18n context.
48
+ * @param context - The I18n context to modify
49
+ * @param languageContext - The language context to set
50
+ */
51
+ function setContext(context, languageContext) {
52
+ context.currentContext = languageContext;
53
+ }
54
+ /**
55
+ * Sets the timezone for the given I18n context.
56
+ * @param context - The I18n context to modify
57
+ * @param timezone - The timezone to set
58
+ */
59
+ function setTimezone(context, timezone) {
60
+ context.timezone = timezone;
61
+ }
62
+ /**
63
+ * Sets the admin timezone for the given I18n context.
64
+ * @param context - The I18n context to modify
65
+ * @param timezone - The admin timezone to set
66
+ */
67
+ function setAdminTimezone(context, timezone) {
68
+ context.adminTimezone = timezone;
69
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Class representing a validated currency code.
3
+ */
4
+ export declare class CurrencyCode {
5
+ private _value;
6
+ /**
7
+ * Gets the currency code value.
8
+ */
9
+ get value(): string;
10
+ /**
11
+ * Sets the currency code value after validating it.
12
+ */
13
+ set value(value: string);
14
+ /**
15
+ * Gets the list of all valid currency codes.
16
+ */
17
+ static get values(): string[];
18
+ constructor(value?: string);
19
+ }
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CurrencyCode = void 0;
4
+ const currency_codes_1 = require("currency-codes");
5
+ const types_1 = require("./types");
6
+ /**
7
+ * Class representing a validated currency code.
8
+ */
9
+ class CurrencyCode {
10
+ /**
11
+ * Gets the currency code value.
12
+ */
13
+ get value() {
14
+ return this._value;
15
+ }
16
+ /**
17
+ * Sets the currency code value after validating it.
18
+ */
19
+ set value(value) {
20
+ if (!CurrencyCode.values.includes(value)) {
21
+ throw new Error('Invalid currency code');
22
+ }
23
+ this._value = value;
24
+ }
25
+ /**
26
+ * Gets the list of all valid currency codes.
27
+ */
28
+ static get values() {
29
+ return (0, currency_codes_1.codes)();
30
+ }
31
+ constructor(value = types_1.DefaultCurrencyCode) {
32
+ this._value = types_1.DefaultCurrencyCode;
33
+ this.value = value;
34
+ }
35
+ }
36
+ exports.CurrencyCode = CurrencyCode;
@@ -0,0 +1,10 @@
1
+ import { CurrencyPosition } from './types';
2
+ /**
3
+ * Represents the format details for a specific currency in a given locale.
4
+ */
5
+ export interface CurrencyFormat {
6
+ symbol: string;
7
+ position: CurrencyPosition;
8
+ groupSeparator: string;
9
+ decimalSeparator: string;
10
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Currency formatting utilities
3
+ */
4
+ import { CurrencyFormat } from './currency-format';
5
+ /**
6
+ * Get currency format details for a given locale and currency code.
7
+ * @param locale The locale string (e.g., 'en-US')
8
+ * @param currencyCode The ISO 4217 currency code (e.g., 'USD')
9
+ * @returns The currency format details
10
+ */
11
+ export declare function getCurrencyFormat(locale: string, currencyCode: string): CurrencyFormat;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ /**
3
+ * Currency formatting utilities
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getCurrencyFormat = getCurrencyFormat;
7
+ /**
8
+ * Get currency format details for a given locale and currency code.
9
+ * @param locale The locale string (e.g., 'en-US')
10
+ * @param currencyCode The ISO 4217 currency code (e.g., 'USD')
11
+ * @returns The currency format details
12
+ */
13
+ function getCurrencyFormat(locale, currencyCode) {
14
+ const formatter = new Intl.NumberFormat(locale, {
15
+ style: 'currency',
16
+ currency: currencyCode,
17
+ });
18
+ const parts = formatter.formatToParts(1234567.89);
19
+ const symbol = parts.find((part) => part.type === 'currency')?.value || '';
20
+ const symbolIndex = parts.findIndex((part) => part.type === 'currency');
21
+ const decimalIndex = parts.findIndex((part) => part.type === 'decimal');
22
+ const integerIndex = parts.findIndex((part) => part.type === 'integer');
23
+ let position;
24
+ if (decimalIndex === -1) {
25
+ // No decimal separator
26
+ if (symbolIndex < integerIndex) {
27
+ position = 'prefix';
28
+ }
29
+ else {
30
+ position = 'postfix';
31
+ }
32
+ }
33
+ else if (symbolIndex < decimalIndex && symbolIndex < integerIndex) {
34
+ position = 'prefix';
35
+ }
36
+ else if (symbolIndex > decimalIndex) {
37
+ position = 'postfix';
38
+ }
39
+ else {
40
+ position = 'infix';
41
+ }
42
+ return {
43
+ symbol,
44
+ position,
45
+ groupSeparator: parts.find((part) => part.type === 'group')?.value || ',',
46
+ decimalSeparator: parts.find((part) => part.type === 'decimal')?.value || '.',
47
+ };
48
+ }
@@ -0,0 +1,35 @@
1
+ import { EnumLanguageTranslation } from './types';
2
+ /**
3
+ * Registry for managing enum translations across multiple languages.
4
+ */
5
+ export declare class EnumTranslationRegistry<TLanguage extends string> {
6
+ protected translations: Map<any, Partial<{ [L in TLanguage]: import("./types").EnumTranslation<any>; }>>;
7
+ protected enumNames: WeakMap<any, string>;
8
+ /**
9
+ * Registers an enumeration with its translations and a name.
10
+ * @param enumObj The enumeration object
11
+ * @param translations The translations for the enumeration
12
+ * @param enumName The name of the enumeration
13
+ */
14
+ register<TEnum extends string | number>(enumObj: Record<string, TEnum>, translations: EnumLanguageTranslation<TEnum, TLanguage>, enumName: string): void;
15
+ /**
16
+ * Translates a value from the given enumeration to the specified language.
17
+ * @param enumObj The enumeration object
18
+ * @param value The value to translate
19
+ * @param language The target language for translation
20
+ * @returns The translated string
21
+ */
22
+ translate<TEnum extends string | number>(enumObj: Record<string, TEnum>, value: TEnum, language: TLanguage): string;
23
+ /**
24
+ * Gets the name of the enumeration.
25
+ * @param enumObj The enumeration object
26
+ * @returns The name of the enumeration
27
+ */
28
+ private getEnumName;
29
+ /**
30
+ * Checks if the registry has translations for the given enumeration.
31
+ * @param enumObj The enumeration object
32
+ * @returns True if translations exist, false otherwise
33
+ */
34
+ has(enumObj: any): boolean;
35
+ }
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EnumTranslationRegistry = void 0;
4
+ /**
5
+ * Registry for managing enum translations across multiple languages.
6
+ */
7
+ class EnumTranslationRegistry {
8
+ constructor() {
9
+ this.translations = new Map();
10
+ this.enumNames = new WeakMap();
11
+ }
12
+ /**
13
+ * Registers an enumeration with its translations and a name.
14
+ * @param enumObj The enumeration object
15
+ * @param translations The translations for the enumeration
16
+ * @param enumName The name of the enumeration
17
+ */
18
+ register(enumObj, translations, enumName) {
19
+ this.translations.set(enumObj, translations);
20
+ this.enumNames.set(enumObj, enumName);
21
+ }
22
+ /**
23
+ * Translates a value from the given enumeration to the specified language.
24
+ * @param enumObj The enumeration object
25
+ * @param value The value to translate
26
+ * @param language The target language for translation
27
+ * @returns The translated string
28
+ */
29
+ translate(enumObj, value, language) {
30
+ const translations = this.translations.get(enumObj);
31
+ if (!translations) {
32
+ throw new Error(`No translations found for enum: ${this.getEnumName(enumObj)}`);
33
+ }
34
+ const langTranslations = translations[language];
35
+ if (!langTranslations) {
36
+ throw new Error(`No translations found for language: ${language}`);
37
+ }
38
+ let result = langTranslations[value];
39
+ if (!result && typeof value === 'number') {
40
+ const stringKey = Object.keys(enumObj).find((key) => enumObj[key] === value);
41
+ if (stringKey) {
42
+ result = langTranslations[stringKey];
43
+ }
44
+ }
45
+ if (!result) {
46
+ throw new Error(`No translation found for value: ${String(value)}`);
47
+ }
48
+ return result;
49
+ }
50
+ /**
51
+ * Gets the name of the enumeration.
52
+ * @param enumObj The enumeration object
53
+ * @returns The name of the enumeration
54
+ */
55
+ getEnumName(enumObj) {
56
+ return this.enumNames.get(enumObj) || 'UnknownEnum';
57
+ }
58
+ /**
59
+ * Checks if the registry has translations for the given enumeration.
60
+ * @param enumObj The enumeration object
61
+ * @returns True if translations exist, false otherwise
62
+ */
63
+ has(enumObj) {
64
+ return this.translations.has(enumObj);
65
+ }
66
+ }
67
+ exports.EnumTranslationRegistry = EnumTranslationRegistry;
@@ -0,0 +1,156 @@
1
+ import { EnumTranslationRegistry } from './enum-registry';
2
+ import { EnumLanguageTranslation, I18nConfig, I18nContext } from './types';
3
+ /**
4
+ * Internationalization engine class
5
+ */
6
+ export declare class I18nEngine<TStringKey extends string, TLanguage extends string, TConstants extends Record<string, any> = Record<string, any>, TTranslationContext extends string = string, TContext extends I18nContext<TLanguage, TTranslationContext> = I18nContext<TLanguage, TTranslationContext>> {
7
+ /**
8
+ * Registry for enum translations
9
+ */
10
+ protected enumRegistry: EnumTranslationRegistry<TLanguage>;
11
+ /**
12
+ * Configuration for the i18n engine
13
+ */
14
+ readonly config: I18nConfig<TStringKey, TLanguage, TConstants, TTranslationContext>;
15
+ /**
16
+ * Static instances for semi-singleton pattern
17
+ */
18
+ private static _instances;
19
+ /**
20
+ * Default instance key (first created instance)
21
+ */
22
+ private static _defaultKey;
23
+ /**
24
+ * Default instance key if none is provided
25
+ */
26
+ protected static readonly DefaultInstanceKey = "default";
27
+ /**
28
+ * Global context for translations (used if no context is provided) for this instance
29
+ */
30
+ private _context;
31
+ /**
32
+ * Default template processor instance
33
+ */
34
+ readonly t: (str: string, language?: TLanguage, ...otherVars: Record<string, string | number>[]) => string;
35
+ /**
36
+ * Creates a new I18nEngine instance
37
+ * @param config The i18n configuration
38
+ * @param key Optional instance key for the semi-singleton pattern
39
+ * @throws Error if an instance with the same key already exists
40
+ */
41
+ constructor(config: I18nConfig<TStringKey, TLanguage, TConstants, TTranslationContext>, key?: string, newContext?: () => TContext);
42
+ /**
43
+ * Gets an instance of the I18nEngine by key. If no key is provided, the default instance is returned.
44
+ * @param key The key of the instance to retrieve
45
+ * @returns The I18nEngine instance
46
+ * @throws Error if the instance with the provided key does not exist
47
+ */
48
+ static getInstance<T extends I18nEngine<any, any, any, any>>(key?: string): T;
49
+ /**
50
+ * Gets a translation for the provided error key using the specified instance (or default instance if none is provided).
51
+ * @param errorKey The error key to translate
52
+ * @param vars Variables to replace in the translation string
53
+ * @param instanceKey The key of the I18nEngine instance to use
54
+ * @param language The language to translate to
55
+ * @param fallbackLanguage The fallback language if the translation is not found
56
+ * @returns The translated error message
57
+ */
58
+ static getErrorMessage(errorKey: string, vars?: Record<string, string | number>, instanceKey?: string, language?: string, fallbackLanguage?: string): string;
59
+ /**
60
+ * Throws an error with a translated message using the specified instance (or default instance if none is provided).
61
+ * @param errorKey The error key to translate
62
+ * @param vars Variables to replace in the translation string
63
+ * @param instanceKey The key of the I18nEngine instance to use
64
+ * @throws Error with translated message
65
+ */
66
+ static throwError(errorKey: string, vars?: Record<string, string | number>, instanceKey?: string): never;
67
+ /**
68
+ * Gets the global context for translations
69
+ * @returns The global context object
70
+ */
71
+ get context(): TContext;
72
+ /**
73
+ * Sets the global context for translations (used if no context is provided) for this instance
74
+ * @param context The context to set
75
+ */
76
+ set context(context: Partial<TContext>);
77
+ /**
78
+ * Translates a string key into the specified language, replacing any variables as needed.
79
+ * @param key The string key to translate
80
+ * @param vars Variables to replace in the translation string
81
+ * @param language The language to translate to
82
+ * @param fallbackLanguage The fallback language if the translation is not found
83
+ * @returns The translated string
84
+ */
85
+ translate(key: TStringKey, vars?: Record<string, string | number>, language?: TLanguage, fallbackLanguage?: TLanguage): string;
86
+ /**
87
+ * Translates an enumeration value into the specified language.
88
+ * @param enumObj The enumeration object
89
+ * @param value The enumeration value to translate
90
+ * @param language The language to translate to
91
+ * @returns The translated enumeration value
92
+ */
93
+ translateEnum<TEnum extends string | number>(enumObj: Record<string, TEnum>, value: TEnum, language: TLanguage): string;
94
+ /**
95
+ * Registers an enumeration and its translations with the engine.
96
+ * @param enumObj The enumeration object
97
+ * @param translations The translations for the enumeration
98
+ * @param enumName The name of the enumeration (for error messages)
99
+ */
100
+ registerEnum<TEnum extends string | number>(enumObj: Record<string, TEnum> | Record<string | number, string | number>, translations: EnumLanguageTranslation<TEnum, TLanguage>, enumName: string): void;
101
+ /**
102
+ * Safe translation that prevents infinite recursion for error messages
103
+ * @param key The string key to translate
104
+ * @param vars Variables to replace in the translation string
105
+ * @param language The language to translate to
106
+ * @returns The translated string or the key if translation fails
107
+ */
108
+ private safeTranslate;
109
+ /**
110
+ * Retrieves the string for the given language and key, throwing an error if not found.
111
+ * @param language The language to get the string for
112
+ * @param key The string key to retrieve
113
+ * @returns The string value
114
+ * @throws Error if the language or string key is not found
115
+ */
116
+ private getString;
117
+ /**
118
+ * Gets the language code for the specified language.
119
+ * @param language The language to get the code for
120
+ * @returns The language code
121
+ */
122
+ getLanguageCode(language: TLanguage): string;
123
+ /**
124
+ * Gets the language for the specified language code.
125
+ * @param code The language code to look up
126
+ * @returns The language, or undefined if not found
127
+ */
128
+ getLanguageFromCode(code: string): TLanguage | undefined;
129
+ /**
130
+ * Gets all language codes.
131
+ * @returns A record of all language codes
132
+ */
133
+ getAllLanguageCodes(): Record<TLanguage, string>;
134
+ /**
135
+ * Gets all available languages.
136
+ * @returns An array of all available languages
137
+ */
138
+ getAvailableLanguages(): TLanguage[];
139
+ /**
140
+ * Checks if a language is available.
141
+ * @param language The language to check
142
+ * @returns True if the language is available, false otherwise
143
+ */
144
+ isLanguageAvailable(language: string): language is TLanguage;
145
+ /**
146
+ * Clears all instances (for testing purposes)
147
+ * @internal
148
+ */
149
+ static clearInstances(): void;
150
+ /**
151
+ * Removes a specific instance by key
152
+ * @param key The key of the instance to remove
153
+ * @internal
154
+ */
155
+ static removeInstance(key?: string): void;
156
+ }