@digitaldefiance/i18n-lib 1.2.0 → 1.2.2
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 +75 -31
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/translation-engine.d.ts +8 -0
- package/dist/typed-error.d.ts +2 -6
- package/package.json +1 -1
- package/dist/core-language.d.ts +0 -13
- package/dist/core-language.js +0 -17
- package/dist/package.json +0 -52
- package/dist/registry-context.d.ts +0 -12
- package/dist/src/active-context.d.ts +0 -29
- package/dist/src/active-context.js +0 -2
- package/dist/src/context-manager.d.ts +0 -33
- package/dist/src/context-manager.js +0 -61
- package/dist/src/context.d.ts +0 -43
- package/dist/src/context.js +0 -69
- package/dist/src/currency-code.d.ts +0 -19
- package/dist/src/currency-code.js +0 -36
- package/dist/src/currency-format.d.ts +0 -10
- package/dist/src/currency-format.js +0 -2
- package/dist/src/currency.d.ts +0 -11
- package/dist/src/currency.js +0 -48
- package/dist/src/enum-registry.d.ts +0 -35
- package/dist/src/enum-registry.js +0 -67
- package/dist/src/i18n-engine.d.ts +0 -156
- package/dist/src/i18n-engine.js +0 -267
- package/dist/src/index.d.ts +0 -13
- package/dist/src/index.js +0 -32
- package/dist/src/template.d.ts +0 -12
- package/dist/src/template.js +0 -30
- package/dist/src/timezone.d.ts +0 -11
- package/dist/src/timezone.js +0 -22
- package/dist/src/types.d.ts +0 -78
- package/dist/src/types.js +0 -14
- package/dist/src/utils.d.ts +0 -41
- package/dist/src/utils.js +0 -85
- /package/dist/{registry-context.js → translation-engine.js} +0 -0
package/dist/src/template.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createTemplateProcessor = createTemplateProcessor;
|
|
4
|
-
/**
|
|
5
|
-
* Template function that processes {{EnumName.EnumKey}} patterns
|
|
6
|
-
*/
|
|
7
|
-
function createTemplateProcessor(enumObj, translateFn, enumName) {
|
|
8
|
-
return function t(str, language, ...otherVars) {
|
|
9
|
-
let varIndex = 0;
|
|
10
|
-
const pattern = new RegExp(`\\{\\{${enumName}\\.(\\w+)\\}\\}`, 'g');
|
|
11
|
-
// First replace enum patterns
|
|
12
|
-
let result = str.replace(pattern, (match, enumKey) => {
|
|
13
|
-
const enumValue = enumObj[enumKey];
|
|
14
|
-
if (!enumValue) {
|
|
15
|
-
return match; // Return original if enum key not found
|
|
16
|
-
}
|
|
17
|
-
const needsVars = enumValue
|
|
18
|
-
.toLowerCase()
|
|
19
|
-
.endsWith('template');
|
|
20
|
-
const vars = needsVars ? otherVars[varIndex++] ?? {} : {};
|
|
21
|
-
return translateFn(enumValue, vars, language);
|
|
22
|
-
});
|
|
23
|
-
// Then replace any remaining variables from all otherVars
|
|
24
|
-
const allVars = otherVars.reduce((acc, vars) => ({ ...acc, ...vars }), {});
|
|
25
|
-
result = result.replace(/\{(\w+)\}/g, (match, varName) => {
|
|
26
|
-
return allVars[varName] !== undefined ? String(allVars[varName]) : match;
|
|
27
|
-
});
|
|
28
|
-
return result;
|
|
29
|
-
};
|
|
30
|
-
}
|
package/dist/src/timezone.d.ts
DELETED
package/dist/src/timezone.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Timezone = void 0;
|
|
4
|
-
const utils_1 = require("./utils");
|
|
5
|
-
/**
|
|
6
|
-
* Class representing a validated timezone.
|
|
7
|
-
*/
|
|
8
|
-
class Timezone {
|
|
9
|
-
constructor(timezone) {
|
|
10
|
-
if (!(0, utils_1.isValidTimezone)(timezone)) {
|
|
11
|
-
throw new Error(`Invalid timezone: ${timezone}`);
|
|
12
|
-
}
|
|
13
|
-
this._timezone = timezone;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Gets the timezone value.
|
|
17
|
-
*/
|
|
18
|
-
get value() {
|
|
19
|
-
return this._timezone;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
exports.Timezone = Timezone;
|
package/dist/src/types.d.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { CurrencyCode } from './currency-code';
|
|
2
|
-
import { Timezone } from './timezone';
|
|
3
|
-
/**
|
|
4
|
-
* Standard language contexts
|
|
5
|
-
*/
|
|
6
|
-
export type LanguageContext = 'admin' | 'user' | 'system' | 'api';
|
|
7
|
-
/**
|
|
8
|
-
* Default currency code
|
|
9
|
-
*/
|
|
10
|
-
export declare const DefaultCurrencyCode: string;
|
|
11
|
-
/**
|
|
12
|
-
* Currency position type
|
|
13
|
-
*/
|
|
14
|
-
export type CurrencyPosition = 'prefix' | 'postfix' | 'infix';
|
|
15
|
-
/**
|
|
16
|
-
* Custom language context type
|
|
17
|
-
*/
|
|
18
|
-
export type CustomLanguageContext<T extends string = LanguageContext> = T;
|
|
19
|
-
/**
|
|
20
|
-
* Collection of localized strings for a specific language
|
|
21
|
-
*/
|
|
22
|
-
export type StringsCollection<TStringKey extends string> = Partial<Record<TStringKey, string>>;
|
|
23
|
-
/**
|
|
24
|
-
* Mapping of languages to their respective string collections
|
|
25
|
-
*/
|
|
26
|
-
export type MasterStringsCollection<TStringKey extends string, TLanguage extends string> = Partial<Record<TLanguage, StringsCollection<TStringKey>>>;
|
|
27
|
-
/**
|
|
28
|
-
* Mapping of language codes to their respective languages
|
|
29
|
-
*/
|
|
30
|
-
export type LanguageCodeCollection<TLanguage extends string> = Partial<Record<TLanguage, string>>;
|
|
31
|
-
/**
|
|
32
|
-
* Mapping of enumeration values to their translations in multiple languages
|
|
33
|
-
*/
|
|
34
|
-
export type EnumTranslationMap<TEnum extends string | number, TLanguage extends string> = Partial<Record<TLanguage, Partial<Record<TEnum, string>>>>;
|
|
35
|
-
/**
|
|
36
|
-
* I18n configuration interface
|
|
37
|
-
*/
|
|
38
|
-
export interface I18nConfig<TStringKey extends string, TLanguage extends string, TConstants extends Record<string, any> = Record<string, any>, TTranslationContext extends string = LanguageContext> {
|
|
39
|
-
stringNames: TStringKey[];
|
|
40
|
-
strings: MasterStringsCollection<TStringKey, TLanguage>;
|
|
41
|
-
defaultLanguage: TLanguage;
|
|
42
|
-
defaultTranslationContext: TTranslationContext;
|
|
43
|
-
defaultCurrencyCode: CurrencyCode;
|
|
44
|
-
languageCodes: LanguageCodeCollection<TLanguage>;
|
|
45
|
-
languages: TLanguage[];
|
|
46
|
-
constants?: TConstants;
|
|
47
|
-
enumName?: string;
|
|
48
|
-
enumObj?: Record<string, TStringKey>;
|
|
49
|
-
timezone: Timezone;
|
|
50
|
-
adminTimezone: Timezone;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* I18n context interface
|
|
54
|
-
*/
|
|
55
|
-
export interface I18nContext<TLanguage extends string, TTranslationContext extends string = LanguageContext> {
|
|
56
|
-
language: TLanguage;
|
|
57
|
-
adminLanguage: TLanguage;
|
|
58
|
-
currencyCode: CurrencyCode;
|
|
59
|
-
currentContext: TTranslationContext;
|
|
60
|
-
timezone: Timezone;
|
|
61
|
-
adminTimezone: Timezone;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Generic translation type for any enumeration
|
|
65
|
-
*/
|
|
66
|
-
export type EnumTranslation<T extends string | number> = {
|
|
67
|
-
[K in T]: string;
|
|
68
|
-
};
|
|
69
|
-
/**
|
|
70
|
-
* Generic language translation type for any enumeration
|
|
71
|
-
*/
|
|
72
|
-
export type EnumLanguageTranslation<T extends string | number, TLanguage extends string = string> = Partial<{
|
|
73
|
-
[L in TLanguage]: EnumTranslation<T>;
|
|
74
|
-
}>;
|
|
75
|
-
/**
|
|
76
|
-
* Helper function to create typed translations for an enumeration
|
|
77
|
-
*/
|
|
78
|
-
export declare function createTranslations<T extends string | number, TLanguage extends string>(translations: EnumLanguageTranslation<T, TLanguage>): EnumLanguageTranslation<T, TLanguage>;
|
package/dist/src/types.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DefaultCurrencyCode = void 0;
|
|
4
|
-
exports.createTranslations = createTranslations;
|
|
5
|
-
/**
|
|
6
|
-
* Default currency code
|
|
7
|
-
*/
|
|
8
|
-
exports.DefaultCurrencyCode = 'USD';
|
|
9
|
-
/**
|
|
10
|
-
* Helper function to create typed translations for an enumeration
|
|
11
|
-
*/
|
|
12
|
-
function createTranslations(translations) {
|
|
13
|
-
return translations;
|
|
14
|
-
}
|
package/dist/src/utils.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Replaces variables in a string with their corresponding values from vars or constants.
|
|
3
|
-
* @param str - The string containing variables to replace
|
|
4
|
-
* @param vars - An object mapping variable names to their replacement values
|
|
5
|
-
* @param constants - An object containing constant values for replacement
|
|
6
|
-
* @returns The string with variables replaced
|
|
7
|
-
*/
|
|
8
|
-
export declare function replaceVariables(str: string, vars?: Record<string, string | number>, constants?: any): string;
|
|
9
|
-
/**
|
|
10
|
-
* Checks if a given key indicates a template string.
|
|
11
|
-
* @param key - The key to check
|
|
12
|
-
* @returns True if the key indicates a template, false otherwise
|
|
13
|
-
*/
|
|
14
|
-
export declare function isTemplate(key: string): boolean;
|
|
15
|
-
/**
|
|
16
|
-
* Checks if a given timezone string is valid.
|
|
17
|
-
* @param timezone - The timezone string to validate
|
|
18
|
-
* @returns
|
|
19
|
-
*/
|
|
20
|
-
export declare function isValidTimezone(timezone: string): boolean;
|
|
21
|
-
/**
|
|
22
|
-
* Converts parts to a single string key, joining with underscores.
|
|
23
|
-
* @param parts - The parts to join
|
|
24
|
-
* @returns The joined string key
|
|
25
|
-
*/
|
|
26
|
-
export declare function toStringKey<TStringKey extends string>(...parts: (string)[]): TStringKey;
|
|
27
|
-
/**
|
|
28
|
-
* Converts an enum value to a string key by joining parts with underscores and appending the enum value.
|
|
29
|
-
* @param enumObj - The enum object
|
|
30
|
-
* @param value - The enum value
|
|
31
|
-
* @param parts - Additional parts to join
|
|
32
|
-
* @returns The constructed string key
|
|
33
|
-
*/
|
|
34
|
-
export declare function toStringKeyFromEnum<TStringKey extends string, TEnum extends string>(value: TEnum[keyof TEnum], ...parts: (string)[]): TStringKey;
|
|
35
|
-
/**
|
|
36
|
-
* Builds a reason map from an enum object, mapping each enum value to a string key.
|
|
37
|
-
* @param enumObj - The enum object
|
|
38
|
-
* @param prefixes - Prefixes to prepend to each string key
|
|
39
|
-
* @returns The constructed reason map
|
|
40
|
-
*/
|
|
41
|
-
export declare function buildReasonMap<TStringKey extends string, TEnum extends Record<string, string>>(enumObj: TEnum, ...prefixes: string[]): Record<string, TStringKey>;
|
package/dist/src/utils.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.replaceVariables = replaceVariables;
|
|
7
|
-
exports.isTemplate = isTemplate;
|
|
8
|
-
exports.isValidTimezone = isValidTimezone;
|
|
9
|
-
exports.toStringKey = toStringKey;
|
|
10
|
-
exports.toStringKeyFromEnum = toStringKeyFromEnum;
|
|
11
|
-
exports.buildReasonMap = buildReasonMap;
|
|
12
|
-
const moment_timezone_1 = __importDefault(require("moment-timezone"));
|
|
13
|
-
/**
|
|
14
|
-
* Replaces variables in a string with their corresponding values from vars or constants.
|
|
15
|
-
* @param str - The string containing variables to replace
|
|
16
|
-
* @param vars - An object mapping variable names to their replacement values
|
|
17
|
-
* @param constants - An object containing constant values for replacement
|
|
18
|
-
* @returns The string with variables replaced
|
|
19
|
-
*/
|
|
20
|
-
function replaceVariables(str, vars, constants) {
|
|
21
|
-
const variables = str.match(/\{(.+?)\}/g);
|
|
22
|
-
if (!variables)
|
|
23
|
-
return str;
|
|
24
|
-
let result = str;
|
|
25
|
-
for (const variable of variables) {
|
|
26
|
-
const varName = variable.slice(1, -1);
|
|
27
|
-
let replacement = '';
|
|
28
|
-
if (vars && varName in vars) {
|
|
29
|
-
replacement = String(vars[varName]);
|
|
30
|
-
}
|
|
31
|
-
else if (constants && varName in constants) {
|
|
32
|
-
replacement = String(constants[varName]);
|
|
33
|
-
}
|
|
34
|
-
if (replacement) {
|
|
35
|
-
result = result.replace(variable, replacement);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return result;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Checks if a given key indicates a template string.
|
|
42
|
-
* @param key - The key to check
|
|
43
|
-
* @returns True if the key indicates a template, false otherwise
|
|
44
|
-
*/
|
|
45
|
-
function isTemplate(key) {
|
|
46
|
-
return key.trim().toLowerCase().endsWith('template');
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Checks if a given timezone string is valid.
|
|
50
|
-
* @param timezone - The timezone string to validate
|
|
51
|
-
* @returns
|
|
52
|
-
*/
|
|
53
|
-
function isValidTimezone(timezone) {
|
|
54
|
-
return moment_timezone_1.default.tz.zone(timezone) !== null;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Converts parts to a single string key, joining with underscores.
|
|
58
|
-
* @param parts - The parts to join
|
|
59
|
-
* @returns The joined string key
|
|
60
|
-
*/
|
|
61
|
-
function toStringKey(...parts) {
|
|
62
|
-
return parts.join('_');
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Converts an enum value to a string key by joining parts with underscores and appending the enum value.
|
|
66
|
-
* @param enumObj - The enum object
|
|
67
|
-
* @param value - The enum value
|
|
68
|
-
* @param parts - Additional parts to join
|
|
69
|
-
* @returns The constructed string key
|
|
70
|
-
*/
|
|
71
|
-
function toStringKeyFromEnum(value, ...parts) {
|
|
72
|
-
return parts.join('_') + '_' + String(value);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Builds a reason map from an enum object, mapping each enum value to a string key.
|
|
76
|
-
* @param enumObj - The enum object
|
|
77
|
-
* @param prefixes - Prefixes to prepend to each string key
|
|
78
|
-
* @returns The constructed reason map
|
|
79
|
-
*/
|
|
80
|
-
function buildReasonMap(enumObj, ...prefixes) {
|
|
81
|
-
return Object.values(enumObj).reduce((map, value) => {
|
|
82
|
-
map[value] = toStringKeyFromEnum(value, ...prefixes);
|
|
83
|
-
return map;
|
|
84
|
-
}, {});
|
|
85
|
-
}
|
|
File without changes
|