Skip to content

Commit 026869d

Browse files
committed
fix(i18n): resolve MyMemory API compatibility issues
- Update Chinese language codes to use API-compatible format (zh-CN, zh-TW) - Replace incorrect email with project contact license@iflabx.com - Enhance rate limiting with exponential backoff for better API reliability - Increase base delay from 100ms to 200ms to reduce API pressure Addresses Copilot review suggestions for better API integration.
1 parent f0bf2f5 commit 026869d

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

scripts/auto-gen-i18n.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ function loadLanguageConfig() {
4040

4141
function getBingLanguageCode(langCode) {
4242
const langMap = {
43-
'zh-CN': 'zh-Hans',
44-
'zh-TW': 'zh-Hant',
43+
'zh-CN': 'zh-CN',
44+
'zh-TW': 'zh-TW',
4545
'ja-JP': 'ja',
4646
'de-DE': 'de',
4747
'fr-FR': 'fr',
@@ -67,7 +67,7 @@ async function translateText(text, targetLanguage) {
6767
const url = new URL('https://api.mymemory.translated.net/get');
6868
url.searchParams.append('q', text);
6969
url.searchParams.append('langpair', `en|${targetLanguage}`);
70-
url.searchParams.append('de', 'claude@agentifui.com');
70+
url.searchParams.append('de', 'license@iflabx.com');
7171

7272
const translationResponse = await fetch(url.toString(), {
7373
method: 'GET',
@@ -122,6 +122,28 @@ function isTranslatableText(text) {
122122
return !skipPatterns.some(pattern => pattern.test(text.trim()));
123123
}
124124

125+
async function translateWithBackoff(text, targetLanguage, retries = 3) {
126+
for (let attempt = 0; attempt < retries; attempt++) {
127+
try {
128+
const result = await translateText(text, targetLanguage);
129+
if (result !== text) {
130+
return result;
131+
}
132+
} catch (error) {
133+
if (attempt === retries - 1) {
134+
throw error;
135+
}
136+
137+
const delay = Math.min(1000 * Math.pow(2, attempt), 5000);
138+
console.warn(
139+
`⚠️ Translation attempt ${attempt + 1} failed, retrying in ${delay}ms...`
140+
);
141+
await new Promise(resolve => setTimeout(resolve, delay));
142+
}
143+
}
144+
return text;
145+
}
146+
125147
async function translateMissingKeys(
126148
sourceObj,
127149
targetObj,
@@ -152,11 +174,11 @@ async function translateMissingKeys(
152174
console.log(
153175
`🔄 Translating missing key "${currentPath}": "${value}"`
154176
);
155-
const translated = await translateText(value, targetLanguage);
177+
const translated = await translateWithBackoff(value, targetLanguage);
156178
targetObj[key] = translated;
157179
stats.translated++;
158180

159-
await new Promise(resolve => setTimeout(resolve, 100));
181+
await new Promise(resolve => setTimeout(resolve, 200));
160182
} catch (error) {
161183
console.error(
162184
`❌ Error translating "${currentPath}":`,

0 commit comments

Comments
 (0)