@rimori/client 2.5.31-next.0 → 2.5.31-next.1
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.
|
@@ -11,6 +11,7 @@ export declare class Translator {
|
|
|
11
11
|
private translationUrl;
|
|
12
12
|
private ai;
|
|
13
13
|
private aiTranslationCache;
|
|
14
|
+
private aiTranslationPending;
|
|
14
15
|
constructor(initialLanguage: string, translationUrl: string, ai: AIModule);
|
|
15
16
|
/**
|
|
16
17
|
* Initialize translator with user's language
|
|
@@ -14,6 +14,7 @@ import { createInstance } from 'i18next';
|
|
|
14
14
|
export class Translator {
|
|
15
15
|
constructor(initialLanguage, translationUrl, ai) {
|
|
16
16
|
this.aiTranslationCache = new Map();
|
|
17
|
+
this.aiTranslationPending = new Map();
|
|
17
18
|
this.currentLanguage = initialLanguage;
|
|
18
19
|
this.initializationState = 'not-inited';
|
|
19
20
|
this.initializationPromise = null;
|
|
@@ -158,29 +159,38 @@ export class Translator {
|
|
|
158
159
|
const cached = this.aiTranslationCache.get(text);
|
|
159
160
|
if (cached)
|
|
160
161
|
return cached;
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
162
|
+
const pending = this.aiTranslationPending.get(text);
|
|
163
|
+
if (pending)
|
|
164
|
+
return pending;
|
|
165
|
+
if (!this.ai || this.currentLanguage === 'en')
|
|
166
|
+
return text;
|
|
167
|
+
const promise = (() => __awaiter(this, void 0, void 0, function* () {
|
|
168
|
+
try {
|
|
169
|
+
const response = yield this.ai.getObject({
|
|
170
|
+
prompt: 'global.translator.translate',
|
|
171
|
+
variables: {
|
|
172
|
+
additionalInstructions: additionalInstructions !== null && additionalInstructions !== void 0 ? additionalInstructions : '',
|
|
173
|
+
language: this.currentLanguage,
|
|
174
|
+
text,
|
|
175
|
+
},
|
|
176
|
+
cache: true,
|
|
177
|
+
});
|
|
178
|
+
const translation = response === null || response === void 0 ? void 0 : response.translation;
|
|
179
|
+
if (translation) {
|
|
180
|
+
this.aiTranslationCache.set(text, translation);
|
|
181
|
+
return translation;
|
|
182
|
+
}
|
|
178
183
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
+
catch (error) {
|
|
185
|
+
console.warn('Failed to translate freeform text:', { text, error });
|
|
186
|
+
}
|
|
187
|
+
finally {
|
|
188
|
+
this.aiTranslationPending.delete(text);
|
|
189
|
+
}
|
|
190
|
+
return text;
|
|
191
|
+
}))();
|
|
192
|
+
this.aiTranslationPending.set(text, promise);
|
|
193
|
+
return promise;
|
|
184
194
|
});
|
|
185
195
|
}
|
|
186
196
|
}
|