@digitaldefiance/i18n-lib 1.1.4 → 1.1.6
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
|
@@ -564,7 +564,7 @@ I18nEngine.removeInstance('main');
|
|
|
564
564
|
|
|
565
565
|
- `new PluginI18nEngine<TLanguages>(languages, config?)` - Create new plugin engine
|
|
566
566
|
- `PluginI18nEngine.createInstance<TLanguages>(key, languages, config?)` - Create named instance
|
|
567
|
-
- `PluginI18nEngine.getInstance<TLanguages>(key?)` - Get existing instance
|
|
567
|
+
- `PluginI18nEngine.getInstance<TLanguages>(key?)` - Get existing instance (throws error if not found)
|
|
568
568
|
|
|
569
569
|
**Component Management**
|
|
570
570
|
|
|
@@ -762,8 +762,9 @@ describe('My tests', () => {
|
|
|
762
762
|
#### Available Cleanup Methods
|
|
763
763
|
|
|
764
764
|
- `PluginI18nEngine.clearAllInstances()` - Remove all engine instances
|
|
765
|
-
- `PluginI18nEngine.removeInstance(key?)` - Remove specific instance by key
|
|
766
|
-
- `PluginI18nEngine.hasInstance(key?)` - Check if instance exists
|
|
765
|
+
- `PluginI18nEngine.removeInstance(key?)` - Remove specific instance by key (returns boolean)
|
|
766
|
+
- `PluginI18nEngine.hasInstance(key?)` - Check if instance exists (returns boolean)
|
|
767
|
+
- `PluginI18nEngine.getInstance(key?)` - Get existing instance (throws RegistryError if not found)
|
|
767
768
|
- `PluginI18nEngine.resetAll()` - Clear instances and component registrations
|
|
768
769
|
- `resetAllI18nEngines()` - Convenience function that calls `resetAll()`
|
|
769
770
|
|
|
@@ -936,6 +937,19 @@ Part of the DigitalBurnbag project - a secure file sharing and automated protoco
|
|
|
936
937
|
|
|
937
938
|
## ChangeLog
|
|
938
939
|
|
|
940
|
+
### Version 1.1.6
|
|
941
|
+
|
|
942
|
+
- Tue Oct 14 2025 17:04:00 GMT-0700 (Pacific Daylight Time)
|
|
943
|
+
- Added missing T function to i18n plugin engine
|
|
944
|
+
|
|
945
|
+
### Version 1.1.5
|
|
946
|
+
|
|
947
|
+
- Tue Oct 14 2025 14:48:00 GMT-0700 (Pacific Daylight Time)
|
|
948
|
+
- [Current] HotFix for GlobalActiveContext
|
|
949
|
+
- Fixed getInstance method to throw RegistryError when instance not found instead of auto-creating instances
|
|
950
|
+
- Improved test reliability and proper error handling for non-existent instances
|
|
951
|
+
- Updated API documentation to reflect error-throwing behavior
|
|
952
|
+
|
|
939
953
|
### Version 1.1.4
|
|
940
954
|
|
|
941
955
|
- Tue Oct 14 2025 14:21:00 GMT-0700 (Pacific Daylight Time)
|
|
@@ -106,9 +106,9 @@ class ComponentRegistry {
|
|
|
106
106
|
if (!translation) {
|
|
107
107
|
throw registry_error_1.RegistryError.createSimple(registry_error_type_1.RegistryErrorType.StringKeyNotFound, `String key '${stringKey}' not found for component '${componentId}' in language '${actualLanguage}'`, { componentId, stringKey, language: actualLanguage });
|
|
108
108
|
}
|
|
109
|
-
// Process variables if the string
|
|
109
|
+
// Process variables if the string key indicates it's a template
|
|
110
110
|
let processedTranslation = translation;
|
|
111
|
-
if (variables && (0, utils_1.isTemplate)(
|
|
111
|
+
if (variables && (0, utils_1.isTemplate)(stringKey)) {
|
|
112
112
|
processedTranslation = (0, utils_1.replaceVariables)(translation, variables);
|
|
113
113
|
}
|
|
114
114
|
return {
|
package/dist/context-error.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ContextErrorType } from './context-error-type';
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
export declare class ContextError extends Error {
|
|
3
|
+
readonly type: ContextErrorType;
|
|
4
|
+
readonly contextKey?: string;
|
|
4
5
|
constructor(type: ContextErrorType, contextKey?: string);
|
|
5
6
|
}
|
package/dist/context-error.js
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ContextError = void 0;
|
|
4
|
-
|
|
5
|
-
const core_string_key_1 = require("./core-string-key");
|
|
6
|
-
const plugin_i18n_engine_1 = require("./plugin-i18n-engine");
|
|
7
|
-
const typed_error_1 = require("./typed-error");
|
|
8
|
-
class ContextError extends typed_error_1.CoreTypedError {
|
|
4
|
+
class ContextError extends Error {
|
|
9
5
|
constructor(type, contextKey) {
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
const message = contextKey
|
|
7
|
+
? `Invalid context: ${contextKey}`
|
|
8
|
+
: 'Invalid context';
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'ContextError';
|
|
11
|
+
this.type = type;
|
|
12
|
+
this.contextKey = contextKey;
|
|
14
13
|
}
|
|
15
14
|
}
|
|
16
15
|
exports.ContextError = ContextError;
|
|
@@ -20,6 +20,10 @@ export declare class PluginI18nEngine<TLanguages extends string> {
|
|
|
20
20
|
private readonly enumRegistry;
|
|
21
21
|
private readonly config;
|
|
22
22
|
private contextKey;
|
|
23
|
+
/**
|
|
24
|
+
* Default template processor instance
|
|
25
|
+
*/
|
|
26
|
+
readonly t: (str: string, language?: TLanguages, ...otherVars: Record<string, string | number>[]) => string;
|
|
23
27
|
/**
|
|
24
28
|
* Static instances for semi-singleton pattern
|
|
25
29
|
*/
|
|
@@ -51,6 +51,27 @@ class PluginI18nEngine {
|
|
|
51
51
|
globalContext.setCurrencyCode(this.config.defaultCurrencyCode, this.contextKey);
|
|
52
52
|
globalContext.setUserTimezone(this.config.timezone, this.contextKey);
|
|
53
53
|
globalContext.setAdminTimezone(this.config.adminTimezone, this.contextKey);
|
|
54
|
+
// Initialize the default template processor for component-based patterns
|
|
55
|
+
this.t = (str, language, ...otherVars) => {
|
|
56
|
+
// Step 1: Replace component-based patterns like {{componentId.stringKey}}
|
|
57
|
+
let result = str.replace(/\{\{([^}]+)\}\}/g, (match, pattern) => {
|
|
58
|
+
const parts = pattern.split('.');
|
|
59
|
+
if (parts.length === 2) {
|
|
60
|
+
const [componentId, stringKey] = parts;
|
|
61
|
+
// For template strings, use the first variable object if available
|
|
62
|
+
const isTemplate = stringKey.toLowerCase().endsWith('template');
|
|
63
|
+
const vars = isTemplate && otherVars.length > 0 ? otherVars[0] : {};
|
|
64
|
+
return this.safeTranslate(componentId.trim(), stringKey.trim(), vars, language);
|
|
65
|
+
}
|
|
66
|
+
return match; // Return original if pattern doesn't match expected format
|
|
67
|
+
});
|
|
68
|
+
// Step 2: Replace remaining variable patterns like {varName} with merged variables
|
|
69
|
+
const allVars = otherVars.reduce((acc, vars) => ({ ...acc, ...vars }), {});
|
|
70
|
+
result = result.replace(/\{(\w+)\}/g, (match, varName) => {
|
|
71
|
+
return allVars[varName] !== undefined ? String(allVars[varName]) : match;
|
|
72
|
+
});
|
|
73
|
+
return result;
|
|
74
|
+
};
|
|
54
75
|
// Auto-register as default instance if none exists
|
|
55
76
|
if (!PluginI18nEngine._defaultKey) {
|
|
56
77
|
PluginI18nEngine._instances.set(PluginI18nEngine.DefaultInstanceKey, this);
|