@rimori/client 2.5.9 → 2.5.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.
|
@@ -13,7 +13,6 @@ export declare class Translator {
|
|
|
13
13
|
private translationUrl;
|
|
14
14
|
private ai;
|
|
15
15
|
private aiTranslationCache;
|
|
16
|
-
private aiTranslationInFlight;
|
|
17
16
|
constructor(initialLanguage: string, translationUrl: string, ai: AIModule);
|
|
18
17
|
/**
|
|
19
18
|
* Initialize translator with user's language
|
|
@@ -22,6 +21,7 @@ export declare class Translator {
|
|
|
22
21
|
initialize(): Promise<void>;
|
|
23
22
|
private getTranslationUrl;
|
|
24
23
|
usePlugin(plugin: ThirdPartyModule): void;
|
|
24
|
+
onLanguageChanged(callback: () => void): void;
|
|
25
25
|
/**
|
|
26
26
|
* Fetch translations manually from the current domain
|
|
27
27
|
* @param language - Language code to fetch
|
|
@@ -44,6 +44,5 @@ export declare class Translator {
|
|
|
44
44
|
*/
|
|
45
45
|
isReady(): boolean;
|
|
46
46
|
private isTranslationKey;
|
|
47
|
-
|
|
48
|
-
private fetchAiTranslation;
|
|
47
|
+
fetchTranslation(text: string, additionalInstructions?: string): Promise<string>;
|
|
49
48
|
}
|
|
@@ -14,7 +14,6 @@ import { createInstance } from 'i18next';
|
|
|
14
14
|
export class Translator {
|
|
15
15
|
constructor(initialLanguage, translationUrl, ai) {
|
|
16
16
|
this.aiTranslationCache = new Map();
|
|
17
|
-
this.aiTranslationInFlight = new Set();
|
|
18
17
|
this.currentLanguage = initialLanguage;
|
|
19
18
|
this.initializationState = 'not-inited';
|
|
20
19
|
this.initializationPromise = null;
|
|
@@ -48,7 +47,19 @@ export class Translator {
|
|
|
48
47
|
translation: translations,
|
|
49
48
|
},
|
|
50
49
|
},
|
|
51
|
-
debug:
|
|
50
|
+
debug: false,
|
|
51
|
+
parseMissingKeyHandler: (key, defaultValue) => {
|
|
52
|
+
if (this.isTranslationKey(key)) {
|
|
53
|
+
console.warn(`Translation key not found: ${key}`);
|
|
54
|
+
return defaultValue !== null && defaultValue !== void 0 ? defaultValue : '';
|
|
55
|
+
}
|
|
56
|
+
void this.fetchTranslation(key).then((translation) => {
|
|
57
|
+
var _a, _b;
|
|
58
|
+
(_a = this.i18n) === null || _a === void 0 ? void 0 : _a.addResource(this.currentLanguage, 'translation', key, translation);
|
|
59
|
+
(_b = this.i18n) === null || _b === void 0 ? void 0 : _b.emit('languageChanged'); // triggers re-render
|
|
60
|
+
});
|
|
61
|
+
return key;
|
|
62
|
+
},
|
|
52
63
|
});
|
|
53
64
|
yield instance.init();
|
|
54
65
|
this.i18n = instance;
|
|
@@ -78,6 +89,12 @@ export class Translator {
|
|
|
78
89
|
}
|
|
79
90
|
this.i18n.use(plugin);
|
|
80
91
|
}
|
|
92
|
+
onLanguageChanged(callback) {
|
|
93
|
+
if (!this.i18n) {
|
|
94
|
+
throw new Error('Translator is not initialized');
|
|
95
|
+
}
|
|
96
|
+
this.i18n.on('languageChanged', callback);
|
|
97
|
+
}
|
|
81
98
|
/**
|
|
82
99
|
* Fetch translations manually from the current domain
|
|
83
100
|
* @param language - Language code to fetch
|
|
@@ -111,9 +128,6 @@ export class Translator {
|
|
|
111
128
|
* @returns Translated string
|
|
112
129
|
*/
|
|
113
130
|
t(key, options) {
|
|
114
|
-
if (!this.isTranslationKey(key)) {
|
|
115
|
-
return this.translateFreeformText(key);
|
|
116
|
-
}
|
|
117
131
|
if (!this.i18n) {
|
|
118
132
|
throw new Error('Translator is not initialized');
|
|
119
133
|
}
|
|
@@ -134,26 +148,17 @@ export class Translator {
|
|
|
134
148
|
isTranslationKey(key) {
|
|
135
149
|
return /^[^\s.]+(\.[^\s.]+)+$/.test(key);
|
|
136
150
|
}
|
|
137
|
-
|
|
138
|
-
const cached = this.aiTranslationCache.get(text);
|
|
139
|
-
if (cached)
|
|
140
|
-
return cached;
|
|
141
|
-
if (!this.ai || this.aiTranslationInFlight.has(text)) {
|
|
142
|
-
return text;
|
|
143
|
-
}
|
|
144
|
-
this.aiTranslationInFlight.add(text);
|
|
145
|
-
void this.fetchAiTranslation(text).finally(() => {
|
|
146
|
-
this.aiTranslationInFlight.delete(text);
|
|
147
|
-
});
|
|
148
|
-
return text;
|
|
149
|
-
}
|
|
150
|
-
fetchAiTranslation(text) {
|
|
151
|
+
fetchTranslation(text, additionalInstructions) {
|
|
151
152
|
return __awaiter(this, void 0, void 0, function* () {
|
|
153
|
+
const cached = this.aiTranslationCache.get(text);
|
|
154
|
+
if (cached)
|
|
155
|
+
return cached;
|
|
152
156
|
try {
|
|
153
|
-
|
|
154
|
-
|
|
157
|
+
// If the current language is English, don't translate
|
|
158
|
+
if (!this.ai || this.currentLanguage === 'en')
|
|
159
|
+
return text;
|
|
155
160
|
const response = yield this.ai.getObject({
|
|
156
|
-
behaviour: 'You are a translation engine. Return only the translated text.',
|
|
161
|
+
behaviour: 'You are a translation engine. Return only the translated text.' + additionalInstructions,
|
|
157
162
|
instructions: `Translate the following text into ${this.currentLanguage}: ${text}`,
|
|
158
163
|
tool: {
|
|
159
164
|
translation: {
|
|
@@ -165,11 +170,13 @@ export class Translator {
|
|
|
165
170
|
const translation = response === null || response === void 0 ? void 0 : response.translation;
|
|
166
171
|
if (translation) {
|
|
167
172
|
this.aiTranslationCache.set(text, translation);
|
|
173
|
+
return translation;
|
|
168
174
|
}
|
|
169
175
|
}
|
|
170
176
|
catch (error) {
|
|
171
177
|
console.warn('Failed to translate freeform text:', { text, error });
|
|
172
178
|
}
|
|
179
|
+
return text;
|
|
173
180
|
});
|
|
174
181
|
}
|
|
175
182
|
}
|