@digitaldefiance/i18n-lib 1.3.9 → 1.3.10
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 +4 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/unified-translator.d.ts +29 -0
- package/dist/unified-translator.js +64 -0
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -44,6 +44,7 @@ export * from './translation-request';
|
|
|
44
44
|
export * from './translation-response';
|
|
45
45
|
export * from './translatable';
|
|
46
46
|
export * from './create-translation-adapter';
|
|
47
|
+
export * from './unified-translator';
|
|
47
48
|
export { createCoreI18nEngine as createCoreI18n } from './core-i18n';
|
|
48
49
|
export { I18nEngine as I18n } from './i18n-engine';
|
|
49
50
|
export { PluginI18nEngine as PluginI18n } from './plugin-i18n-engine';
|
package/dist/index.js
CHANGED
|
@@ -63,6 +63,7 @@ __exportStar(require("./translation-request"), exports);
|
|
|
63
63
|
__exportStar(require("./translation-response"), exports);
|
|
64
64
|
__exportStar(require("./translatable"), exports);
|
|
65
65
|
__exportStar(require("./create-translation-adapter"), exports);
|
|
66
|
+
__exportStar(require("./unified-translator"), exports);
|
|
66
67
|
// Re-export for convenience
|
|
67
68
|
var core_i18n_1 = require("./core-i18n");
|
|
68
69
|
Object.defineProperty(exports, "createCoreI18n", { enumerable: true, get: function () { return core_i18n_1.createCoreI18nEngine; } });
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
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
|
+
}
|
|
64
|
+
exports.UnifiedTranslator = UnifiedTranslator;
|