translate
This commit is contained in:
@@ -7,8 +7,32 @@ import OpenAI from 'openai';
|
||||
// Configuration
|
||||
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
|
||||
const LOCALES_DIR = './src/i18n/locales';
|
||||
const GERMAN_FILE = path.join(LOCALES_DIR, 'de/translation.js');
|
||||
const ENGLISH_FILE = path.join(LOCALES_DIR, 'en/translation.js');
|
||||
const GERMAN_DIR = path.join(LOCALES_DIR, 'de');
|
||||
const ENGLISH_DIR = path.join(LOCALES_DIR, 'en');
|
||||
|
||||
// Translation file groups
|
||||
const TRANSLATION_FILES = [
|
||||
'locale.js',
|
||||
'navigation.js',
|
||||
'auth.js',
|
||||
'cart.js',
|
||||
'product.js',
|
||||
'search.js',
|
||||
'sorting.js',
|
||||
'chat.js',
|
||||
'delivery.js',
|
||||
'checkout.js',
|
||||
'payment.js',
|
||||
'filters.js',
|
||||
'tax.js',
|
||||
'footer.js',
|
||||
'titles.js',
|
||||
'sections.js',
|
||||
'pages.js',
|
||||
'orders.js',
|
||||
'settings.js',
|
||||
'common.js'
|
||||
];
|
||||
|
||||
// Model configuration
|
||||
const GERMAN_TO_ENGLISH_MODEL = 'gpt-4.1'; // High-quality model for German -> English (critical step)
|
||||
@@ -78,14 +102,13 @@ Translate the English strings in the following file to {{targetLanguage}}, prese
|
||||
|
||||
Rules:
|
||||
1. Translate only the English strings to {{targetLanguage}}
|
||||
2. Keep all German comments unchanged
|
||||
2. Drop the comments in output
|
||||
3. Maintain the exact JavaScript object structure and formatting
|
||||
4. Keep all interpolation variables like {{count}}, {{vat}}, etc. unchanged
|
||||
5. Update locale code appropriately for the target language
|
||||
6. Do not translate technical terms, API keys, or code-related strings
|
||||
7. Preserve any special formatting or HTML entities
|
||||
8. Keep existing comments from the English version
|
||||
9. Return a valid JavaScript object (not JSON) that can be exported
|
||||
8. Return a valid JavaScript object (not JSON) that can be exported
|
||||
|
||||
Here is the English translation file to translate:
|
||||
|
||||
@@ -199,68 +222,101 @@ function extractJSObjectString(response) {
|
||||
}
|
||||
}
|
||||
|
||||
// Main translation function
|
||||
// Main translation function for multiple files
|
||||
async function translateToEnglish() {
|
||||
console.log(`🔄 Step 1: Translating German to English using ${GERMAN_TO_ENGLISH_MODEL}...`);
|
||||
|
||||
// Read German translation file
|
||||
const germanContent = fs.readFileSync(GERMAN_FILE, 'utf8');
|
||||
const translatedFiles = [];
|
||||
|
||||
try {
|
||||
const translatedContent = await translateContent(germanContent, GERMAN_TO_ENGLISH_SYSTEM_PROMPT, null, GERMAN_TO_ENGLISH_MODEL);
|
||||
const englishObjectString = extractJSObjectString(translatedContent);
|
||||
for (const fileName of TRANSLATION_FILES) {
|
||||
const germanFile = path.join(GERMAN_DIR, fileName);
|
||||
const englishFile = path.join(ENGLISH_DIR, fileName);
|
||||
|
||||
if (englishObjectString) {
|
||||
writeTranslationFile(ENGLISH_FILE, englishObjectString);
|
||||
console.log('✅ German to English translation completed');
|
||||
return englishObjectString;
|
||||
} else {
|
||||
throw new Error('Failed to parse English translation');
|
||||
console.log(`🔄 Translating ${fileName}...`);
|
||||
|
||||
try {
|
||||
// Read German translation file
|
||||
const germanContent = fs.readFileSync(germanFile, 'utf8');
|
||||
|
||||
const translatedContent = await translateContent(germanContent, GERMAN_TO_ENGLISH_SYSTEM_PROMPT, null, GERMAN_TO_ENGLISH_MODEL);
|
||||
const englishObjectString = extractJSObjectString(translatedContent);
|
||||
|
||||
if (englishObjectString) {
|
||||
writeTranslationFile(englishFile, englishObjectString);
|
||||
translatedFiles.push(fileName);
|
||||
console.log(`✅ ${fileName} translated successfully`);
|
||||
} else {
|
||||
throw new Error(`Failed to parse English translation for ${fileName}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`❌ Error translating ${fileName}:`, error.message);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('❌ Error translating to English:', error.message);
|
||||
return null;
|
||||
|
||||
// Add delay to avoid rate limiting
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
}
|
||||
|
||||
console.log(`✅ German to English translation completed for ${translatedFiles.length} files`);
|
||||
return translatedFiles;
|
||||
}
|
||||
|
||||
// Function to translate English to other languages
|
||||
async function translateToOtherLanguages(englishObjectString) {
|
||||
// Function to translate English to other languages (multiple files)
|
||||
async function translateToOtherLanguages(translatedFiles) {
|
||||
console.log(`🔄 Step 2: Translating English to other languages using ${ENGLISH_TO_OTHER_MODEL}...`);
|
||||
|
||||
const englishContent = `export default ${englishObjectString};`;
|
||||
|
||||
for (const [langCode, langName] of Object.entries(TARGET_LANGUAGES)) {
|
||||
try {
|
||||
console.log(`🔄 Translating to ${langName} (${langCode})...`);
|
||||
|
||||
const translatedContent = await translateToTargetLanguage(
|
||||
englishContent,
|
||||
langName,
|
||||
ENGLISH_TO_OTHER_MODEL
|
||||
);
|
||||
|
||||
const translatedObjectString = extractJSObjectString(translatedContent);
|
||||
|
||||
if (translatedObjectString) {
|
||||
// Update locale code in the string
|
||||
const updatedString = translatedObjectString.replace(
|
||||
/"code":\s*"[^"]*"/,
|
||||
`"code": "${getLocaleCode(langCode)}"`
|
||||
console.log(`🔄 Translating to ${langName} (${langCode})...`);
|
||||
|
||||
// Create target language directory if it doesn't exist
|
||||
const targetDir = path.join(LOCALES_DIR, langCode);
|
||||
if (!fs.existsSync(targetDir)) {
|
||||
fs.mkdirSync(targetDir, { recursive: true });
|
||||
}
|
||||
|
||||
for (const fileName of TRANSLATION_FILES) {
|
||||
try {
|
||||
const englishFile = path.join(ENGLISH_DIR, fileName);
|
||||
const targetFile = path.join(targetDir, fileName);
|
||||
|
||||
console.log(`🔄 Translating ${fileName} to ${langName}...`);
|
||||
|
||||
// Read English file
|
||||
const englishContent = fs.readFileSync(englishFile, 'utf8');
|
||||
|
||||
const translatedContent = await translateToTargetLanguage(
|
||||
englishContent,
|
||||
langName,
|
||||
ENGLISH_TO_OTHER_MODEL
|
||||
);
|
||||
|
||||
const targetFile = path.join(LOCALES_DIR, `${langCode}/translation.js`);
|
||||
writeTranslationFile(targetFile, updatedString);
|
||||
console.log(`✅ ${langName} translation completed`);
|
||||
} else {
|
||||
console.error(`❌ Failed to parse ${langName} translation`);
|
||||
const translatedObjectString = extractJSObjectString(translatedContent);
|
||||
|
||||
if (translatedObjectString) {
|
||||
// Special handling for locale.js file
|
||||
let updatedString = translatedObjectString;
|
||||
if (fileName === 'locale.js') {
|
||||
updatedString = translatedObjectString.replace(
|
||||
/"code":\s*"[^"]*"/,
|
||||
`"code": "${getLocaleCode(langCode)}"`
|
||||
);
|
||||
}
|
||||
|
||||
writeTranslationFile(targetFile, updatedString);
|
||||
console.log(`✅ ${fileName} translated to ${langName}`);
|
||||
} else {
|
||||
console.error(`❌ Failed to parse ${fileName} translation for ${langName}`);
|
||||
}
|
||||
|
||||
// Add delay to avoid rate limiting
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
|
||||
} catch (error) {
|
||||
console.error(`❌ Error translating ${fileName} to ${langName}:`, error.message);
|
||||
}
|
||||
|
||||
// Add delay to avoid rate limiting
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
|
||||
} catch (error) {
|
||||
console.error(`❌ Error translating to ${langName}:`, error.message);
|
||||
}
|
||||
|
||||
// Add longer delay between languages
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,34 +375,35 @@ async function main() {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Check if German file exists (only if we're translating from German)
|
||||
if (!skipEnglish && !fs.existsSync(GERMAN_FILE)) {
|
||||
console.error(`❌ German translation file not found: ${GERMAN_FILE}`);
|
||||
// Check if German directory exists (only if we're translating from German)
|
||||
if (!skipEnglish && !fs.existsSync(GERMAN_DIR)) {
|
||||
console.error(`❌ German translation directory not found: ${GERMAN_DIR}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
let englishObjectString;
|
||||
let translatedFiles;
|
||||
|
||||
if (skipEnglish) {
|
||||
// Skip German → English, read existing English file
|
||||
if (!fs.existsSync(ENGLISH_FILE)) {
|
||||
console.error(`❌ English translation file not found: ${ENGLISH_FILE}`);
|
||||
console.log('💡 Run without --skip-english first to generate the English file');
|
||||
// Skip German → English, read existing English files
|
||||
if (!fs.existsSync(ENGLISH_DIR)) {
|
||||
console.error(`❌ English translation directory not found: ${ENGLISH_DIR}`);
|
||||
console.log('💡 Run without --skip-english first to generate the English files');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log('📖 Reading existing English translation file...');
|
||||
const englishContent = fs.readFileSync(ENGLISH_FILE, 'utf8');
|
||||
// Extract the object part (remove export default and semicolon)
|
||||
englishObjectString = englishContent.replace(/^export default\s*/, '').replace(/;\s*$/, '');
|
||||
console.log('✅ English file loaded successfully');
|
||||
console.log('📖 Reading existing English translation files...');
|
||||
translatedFiles = TRANSLATION_FILES.filter(fileName => {
|
||||
const englishFile = path.join(ENGLISH_DIR, fileName);
|
||||
return fs.existsSync(englishFile);
|
||||
});
|
||||
console.log(`✅ Found ${translatedFiles.length} English files`);
|
||||
} else {
|
||||
// Step 1: Translate German to English
|
||||
englishObjectString = await translateToEnglish();
|
||||
translatedFiles = await translateToEnglish();
|
||||
|
||||
if (!englishObjectString) {
|
||||
console.error('❌ Failed to create English translation, stopping process');
|
||||
if (!translatedFiles || translatedFiles.length === 0) {
|
||||
console.error('❌ Failed to create English translations, stopping process');
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@@ -355,7 +412,7 @@ async function main() {
|
||||
console.log('🎉 English translation completed! Skipping other languages.');
|
||||
} else {
|
||||
// Step 2: Translate English to other languages
|
||||
await translateToOtherLanguages(englishObjectString);
|
||||
await translateToOtherLanguages(translatedFiles);
|
||||
console.log('🎉 All translations completed successfully!');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user