@digitaldefiance/i18n-lib 1.3.9 → 1.3.11
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 +8 -0
- package/dist/global-active-context.d.ts +4 -0
- package/dist/global-active-context.js +7 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/plugin-i18n-engine.js +3 -2
- package/dist/unified-translator.d.ts +30 -0
- package/dist/unified-translator.js +68 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2274,6 +2274,14 @@ For issues, questions, or contributions:
|
|
|
2274
2274
|
|
|
2275
2275
|
## ChangeLog
|
|
2276
2276
|
|
|
2277
|
+
### Version 1.3.11
|
|
2278
|
+
|
|
2279
|
+
- Export i18nconfig
|
|
2280
|
+
|
|
2281
|
+
### Version 1.3.10
|
|
2282
|
+
|
|
2283
|
+
- Add UnifiedTranslator
|
|
2284
|
+
|
|
2277
2285
|
### Version 1.3.9
|
|
2278
2286
|
|
|
2279
2287
|
- Add more handleable/typed classes
|
|
@@ -43,4 +43,8 @@ export declare class GlobalActiveContext<TLanguage extends string, TActiveContex
|
|
|
43
43
|
setAdminTimezone(tz: Timezone, key?: string): void;
|
|
44
44
|
get adminTimezone(): Timezone;
|
|
45
45
|
set adminTimezone(tz: Timezone);
|
|
46
|
+
/**
|
|
47
|
+
* Clear all contexts (useful for testing)
|
|
48
|
+
*/
|
|
49
|
+
static clearAll(): void;
|
|
46
50
|
}
|
|
@@ -163,6 +163,13 @@ class GlobalActiveContext {
|
|
|
163
163
|
set adminTimezone(tz) {
|
|
164
164
|
this.context.adminTimezone = tz;
|
|
165
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Clear all contexts (useful for testing)
|
|
168
|
+
*/
|
|
169
|
+
static clearAll() {
|
|
170
|
+
GlobalActiveContext._contextMap.clear();
|
|
171
|
+
GlobalActiveContext._instance = undefined;
|
|
172
|
+
}
|
|
166
173
|
}
|
|
167
174
|
exports.GlobalActiveContext = GlobalActiveContext;
|
|
168
175
|
GlobalActiveContext._contextMap = new Map();
|
package/dist/index.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ export * from './handleable';
|
|
|
16
16
|
export * from './i-handleable';
|
|
17
17
|
export * from './i-handleable-error-options';
|
|
18
18
|
export * from './i-global-active-context';
|
|
19
|
+
export * from './i18n-config';
|
|
19
20
|
export * from './i18n-context';
|
|
20
21
|
export * from './i18n-engine';
|
|
21
22
|
export * from './language-definition';
|
|
@@ -44,6 +45,7 @@ export * from './translation-request';
|
|
|
44
45
|
export * from './translation-response';
|
|
45
46
|
export * from './translatable';
|
|
46
47
|
export * from './create-translation-adapter';
|
|
48
|
+
export * from './unified-translator';
|
|
47
49
|
export { createCoreI18nEngine as createCoreI18n } from './core-i18n';
|
|
48
50
|
export { I18nEngine as I18n } from './i18n-engine';
|
|
49
51
|
export { PluginI18nEngine as PluginI18n } from './plugin-i18n-engine';
|
package/dist/index.js
CHANGED
|
@@ -35,6 +35,7 @@ __exportStar(require("./handleable"), exports);
|
|
|
35
35
|
__exportStar(require("./i-handleable"), exports);
|
|
36
36
|
__exportStar(require("./i-handleable-error-options"), exports);
|
|
37
37
|
__exportStar(require("./i-global-active-context"), exports);
|
|
38
|
+
__exportStar(require("./i18n-config"), exports);
|
|
38
39
|
__exportStar(require("./i18n-context"), exports);
|
|
39
40
|
__exportStar(require("./i18n-engine"), exports);
|
|
40
41
|
__exportStar(require("./language-definition"), exports);
|
|
@@ -63,6 +64,7 @@ __exportStar(require("./translation-request"), exports);
|
|
|
63
64
|
__exportStar(require("./translation-response"), exports);
|
|
64
65
|
__exportStar(require("./translatable"), exports);
|
|
65
66
|
__exportStar(require("./create-translation-adapter"), exports);
|
|
67
|
+
__exportStar(require("./unified-translator"), exports);
|
|
66
68
|
// Re-export for convenience
|
|
67
69
|
var core_i18n_1 = require("./core-i18n");
|
|
68
70
|
Object.defineProperty(exports, "createCoreI18n", { enumerable: true, get: function () { return core_i18n_1.createCoreI18nEngine; } });
|
|
@@ -341,13 +341,14 @@ class PluginI18nEngine {
|
|
|
341
341
|
* Useful for test cleanup
|
|
342
342
|
*/
|
|
343
343
|
static resetAll() {
|
|
344
|
-
for
|
|
345
|
-
|
|
344
|
+
// Clear component registrations for each engine BEFORE clearing instances
|
|
345
|
+
for (const engine of PluginI18nEngine._instances.values()) {
|
|
346
346
|
engine.clearAllComponents();
|
|
347
347
|
}
|
|
348
348
|
PluginI18nEngine._instances.clear();
|
|
349
349
|
PluginI18nEngine._defaultKey = null;
|
|
350
350
|
language_registry_1.LanguageRegistry.clear();
|
|
351
|
+
global_active_context_1.GlobalActiveContext.clearAll();
|
|
351
352
|
}
|
|
352
353
|
}
|
|
353
354
|
exports.PluginI18nEngine = PluginI18nEngine;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { PluginI18nEngine } from './plugin-i18n-engine';
|
|
2
|
+
import { I18nEngine } from './i18n-engine';
|
|
3
|
+
/**
|
|
4
|
+
* Unified translator with explicit component/engine specification
|
|
5
|
+
*/
|
|
6
|
+
export declare class UnifiedTranslator<TLanguage extends string = string> {
|
|
7
|
+
private sources;
|
|
8
|
+
private defaultLanguage;
|
|
9
|
+
private defaultSource?;
|
|
10
|
+
constructor(defaultLanguage: TLanguage);
|
|
11
|
+
/**
|
|
12
|
+
* Register a plugin component
|
|
13
|
+
*/
|
|
14
|
+
registerPlugin(id: string, engine: PluginI18nEngine<any>, componentId: string): void;
|
|
15
|
+
/**
|
|
16
|
+
* Register a legacy engine
|
|
17
|
+
*/
|
|
18
|
+
registerLegacy(id: string, engine: I18nEngine<any, any, any, any>): void;
|
|
19
|
+
/**
|
|
20
|
+
* Set default source for unqualified keys
|
|
21
|
+
*/
|
|
22
|
+
setDefaultSource(id: string): void;
|
|
23
|
+
/**
|
|
24
|
+
* Translate with explicit source: 'source:key' or just 'key' (uses default)
|
|
25
|
+
*/
|
|
26
|
+
translate(key: string, vars?: Record<string, string | number>, language?: TLanguage): string;
|
|
27
|
+
setLanguage(language: TLanguage): void;
|
|
28
|
+
getLanguage(): TLanguage;
|
|
29
|
+
clearSources(): void;
|
|
30
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UnifiedTranslator = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Unified translator with explicit component/engine specification
|
|
6
|
+
*/
|
|
7
|
+
class UnifiedTranslator {
|
|
8
|
+
constructor(defaultLanguage) {
|
|
9
|
+
this.sources = new Map();
|
|
10
|
+
this.defaultLanguage = defaultLanguage;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Register a plugin component
|
|
14
|
+
*/
|
|
15
|
+
registerPlugin(id, engine, componentId) {
|
|
16
|
+
this.sources.set(id, { type: 'plugin', engine, componentId });
|
|
17
|
+
if (!this.defaultSource)
|
|
18
|
+
this.defaultSource = id;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Register a legacy engine
|
|
22
|
+
*/
|
|
23
|
+
registerLegacy(id, engine) {
|
|
24
|
+
this.sources.set(id, { type: 'legacy', engine });
|
|
25
|
+
if (!this.defaultSource)
|
|
26
|
+
this.defaultSource = id;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Set default source for unqualified keys
|
|
30
|
+
*/
|
|
31
|
+
setDefaultSource(id) {
|
|
32
|
+
if (!this.sources.has(id)) {
|
|
33
|
+
throw new Error(`Source '${id}' not registered`);
|
|
34
|
+
}
|
|
35
|
+
this.defaultSource = id;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Translate with explicit source: 'source:key' or just 'key' (uses default)
|
|
39
|
+
*/
|
|
40
|
+
translate(key, vars, language) {
|
|
41
|
+
const lang = language || this.defaultLanguage;
|
|
42
|
+
const [sourceName, actualKey] = key.includes(':')
|
|
43
|
+
? key.split(':', 2)
|
|
44
|
+
: [this.defaultSource, key];
|
|
45
|
+
if (!sourceName)
|
|
46
|
+
return `[${key}]`;
|
|
47
|
+
const source = this.sources.get(sourceName);
|
|
48
|
+
if (!source)
|
|
49
|
+
return `[${key}]`;
|
|
50
|
+
if (source.type === 'plugin') {
|
|
51
|
+
return source.engine.safeTranslate(source.componentId, actualKey, vars, lang);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
return source.engine.safeTranslate(actualKey, vars, lang);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
setLanguage(language) {
|
|
58
|
+
this.defaultLanguage = language;
|
|
59
|
+
}
|
|
60
|
+
getLanguage() {
|
|
61
|
+
return this.defaultLanguage;
|
|
62
|
+
}
|
|
63
|
+
clearSources() {
|
|
64
|
+
this.sources.clear();
|
|
65
|
+
this.defaultSource = undefined;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.UnifiedTranslator = UnifiedTranslator;
|