@digitaldefiance/i18n-lib 1.0.21 → 1.0.23

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 CHANGED
@@ -22,7 +22,7 @@ npm install @digitaldefiance/i18n-lib
22
22
  ## Quick Start
23
23
 
24
24
  ```typescript
25
- import { I18nEngine, I18nConfig, createContext } from '@digitaldefiance/i18n-lib';
25
+ import { I18nEngine, I18nConfig, createContext, CurrencyCode, Timezone } from '@digitaldefiance/i18n-lib';
26
26
 
27
27
  // Define your enums
28
28
  enum MyStrings {
@@ -49,12 +49,15 @@ const config: I18nConfig<MyStrings, MyLanguages> = {
49
49
  }
50
50
  },
51
51
  defaultLanguage: MyLanguages.English,
52
- defaultContext: 'user',
52
+ defaultTranslationContext: 'user',
53
+ defaultCurrencyCode: new CurrencyCode('USD'),
53
54
  languageCodes: {
54
55
  [MyLanguages.English]: 'en',
55
56
  [MyLanguages.Spanish]: 'es'
56
57
  },
57
- languages: Object.values(MyLanguages)
58
+ languages: Object.values(MyLanguages),
59
+ timezone: new Timezone('UTC'),
60
+ adminTimezone: new Timezone('UTC')
58
61
  };
59
62
 
60
63
  // Create engine
@@ -216,7 +219,7 @@ I18nEngine.removeInstance('main');
216
219
  #### Translation Methods
217
220
  - `translate(key, vars?, language?, fallbackLanguage?)` - Translate string with optional variables
218
221
  - `translateEnum(enumObj, value, language)` - Translate enum value
219
- - `t(templateString, language?, ...vars)` - Process template with enum patterns
222
+ - `t(templateString, language?, ...vars)` - Process template string with `{{EnumName.EnumKey}}` patterns
220
223
 
221
224
  #### Registration
222
225
  - `registerEnum(enumObj, translations, enumName)` - Register enum translations
@@ -266,16 +269,19 @@ type EnumLanguageTranslation<T extends string | number, TLanguage extends string
266
269
  [L in TLanguage]: EnumTranslation<T>;
267
270
  }>;
268
271
 
269
- interface I18nConfig<TStringKey, TLanguage, TConstants?, TContext?> {
272
+ interface I18nConfig<TStringKey, TLanguage, TConstants?, TTranslationContext?> {
270
273
  stringNames: TStringKey[];
271
274
  strings: MasterStringsCollection<TStringKey, TLanguage>;
272
275
  defaultLanguage: TLanguage;
273
- defaultContext: TContext;
276
+ defaultTranslationContext: TTranslationContext;
277
+ defaultCurrencyCode: CurrencyCode;
274
278
  languageCodes: LanguageCodeCollection<TLanguage>;
275
279
  languages: TLanguage[];
276
280
  constants?: TConstants;
277
281
  enumName?: string;
278
282
  enumObj?: Record<string, TStringKey>;
283
+ timezone: Timezone;
284
+ adminTimezone: Timezone;
279
285
  }
280
286
  ```
281
287
 
package/dist/context.d.ts CHANGED
@@ -10,7 +10,7 @@ import { I18nContext, LanguageContext } from './types';
10
10
  * @param defaultAdminTimezone - The default admin timezone (defaults to UTC)
11
11
  * @returns A new I18nContext instance
12
12
  */
13
- export declare function createContext<TLanguage extends string, TContext extends string = LanguageContext>(defaultLanguage: TLanguage, defaultContext: TContext, defaultCurrencyCode?: CurrencyCode, defaultTimezone?: Timezone, defaultAdminTimezone?: Timezone): I18nContext<TLanguage, TContext>;
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
14
  /**
15
15
  * Sets the language for the given I18n context.
16
16
  * @param context - The I18n context to modify
@@ -3,7 +3,7 @@ import { EnumLanguageTranslation, I18nConfig, I18nContext } from './types';
3
3
  /**
4
4
  * Internationalization engine class
5
5
  */
6
- export declare class I18nEngine<TStringKey extends string, TLanguage extends string, TConstants extends Record<string, any> = Record<string, any>, TContext extends string = string> {
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
7
  /**
8
8
  * Registry for enum translations
9
9
  */
@@ -11,7 +11,7 @@ export declare class I18nEngine<TStringKey extends string, TLanguage extends str
11
11
  /**
12
12
  * Configuration for the i18n engine
13
13
  */
14
- readonly config: I18nConfig<TStringKey, TLanguage, TConstants, TContext>;
14
+ readonly config: I18nConfig<TStringKey, TLanguage, TConstants, TTranslationContext>;
15
15
  /**
16
16
  * Static instances for semi-singleton pattern
17
17
  */
@@ -31,14 +31,14 @@ export declare class I18nEngine<TStringKey extends string, TLanguage extends str
31
31
  /**
32
32
  * Default template processor instance
33
33
  */
34
- readonly t: (key: TStringKey, vars?: Record<string, string | number>, language?: TLanguage) => string;
34
+ readonly t: (str: string, language?: TLanguage, ...otherVars: Record<string, string | number>[]) => string;
35
35
  /**
36
36
  * Creates a new I18nEngine instance
37
37
  * @param config The i18n configuration
38
38
  * @param key Optional instance key for the semi-singleton pattern
39
39
  * @throws Error if an instance with the same key already exists
40
40
  */
41
- constructor(config: I18nConfig<TStringKey, TLanguage, TConstants, TContext>, key?: string);
41
+ constructor(config: I18nConfig<TStringKey, TLanguage, TConstants, TTranslationContext>, key?: string, newContext?: () => TContext);
42
42
  /**
43
43
  * Gets an instance of the I18nEngine by key. If no key is provided, the default instance is returned.
44
44
  * @param key The key of the instance to retrieve
@@ -68,12 +68,12 @@ export declare class I18nEngine<TStringKey extends string, TLanguage extends str
68
68
  * Gets the global context for translations
69
69
  * @returns The global context object
70
70
  */
71
- get context(): I18nContext<TLanguage, TContext>;
71
+ get context(): TContext;
72
72
  /**
73
73
  * Sets the global context for translations (used if no context is provided) for this instance
74
74
  * @param context The context to set
75
75
  */
76
- set context(context: Partial<I18nContext<TLanguage, TContext>>);
76
+ set context(context: Partial<TContext>);
77
77
  /**
78
78
  * Translates a string key into the specified language, replacing any variables as needed.
79
79
  * @param key The string key to translate
@@ -15,10 +15,10 @@ class I18nEngine {
15
15
  * @param key Optional instance key for the semi-singleton pattern
16
16
  * @throws Error if an instance with the same key already exists
17
17
  */
18
- constructor(config, key) {
18
+ constructor(config, key, newContext = () => (0, context_1.createContext)(config.defaultLanguage, config.defaultTranslationContext, config.defaultCurrencyCode, config.timezone, config.adminTimezone)) {
19
19
  this.config = config;
20
20
  this.enumRegistry = new enum_registry_1.EnumTranslationRegistry();
21
- this._context = (0, context_1.createContext)(config.defaultLanguage, config.defaultTranslationContext, config.defaultCurrencyCode, config.timezone, config.adminTimezone);
21
+ this._context = newContext();
22
22
  const instanceKey = key || I18nEngine.DefaultInstanceKey;
23
23
  if (I18nEngine._instances.has(instanceKey)) {
24
24
  const existing = I18nEngine._instances.get(instanceKey);
@@ -247,7 +247,8 @@ class I18nEngine {
247
247
  I18nEngine._instances.delete(instanceKey);
248
248
  if (I18nEngine._defaultKey === instanceKey) {
249
249
  const nextKey = I18nEngine._instances.keys().next().value;
250
- I18nEngine._defaultKey = I18nEngine._instances.size > 0 && nextKey ? nextKey : null;
250
+ I18nEngine._defaultKey =
251
+ I18nEngine._instances.size > 0 && nextKey ? nextKey : null;
251
252
  }
252
253
  }
253
254
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@digitaldefiance/i18n-lib",
3
- "version": "1.0.21",
3
+ "version": "1.0.23",
4
4
  "description": "Generic i18n library with enum translation support",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "build": "yarn tsc",
9
- "test": "yarn jest",
9
+ "test": "yarn jest --detectOpenHandles",
10
10
  "lint": "eslint src/**/*.ts tests/**/*.ts",
11
11
  "lint:fix": "eslint src/**/*.ts tests/**/*.ts --fix",
12
12
  "prettier:check": "prettier --check 'src/**/*.{ts,tsx}' 'tests/**/*.{ts,tsx}'",