Enhance translation functionality and localization support: Updated translate-i18n.js to include new command line options for skipping and only translating English. Modified package.json to add new translation scripts. Improved localization files for multiple languages with better comments for clarity and accuracy, ensuring comprehensive support for internationalization.
This commit is contained in:
@@ -294,31 +294,69 @@ function getLocaleCode(langCode) {
|
||||
|
||||
// Main execution
|
||||
async function main() {
|
||||
// Parse command line arguments
|
||||
const args = process.argv.slice(2);
|
||||
const skipEnglish = args.includes('--skip-english') || args.includes('-s');
|
||||
const onlyEnglish = args.includes('--only-english') || args.includes('-e');
|
||||
|
||||
if (skipEnglish && onlyEnglish) {
|
||||
console.error('❌ Cannot use both --skip-english and --only-english flags');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log('🚀 Starting translation process...');
|
||||
|
||||
// Check if OpenAI API key is set
|
||||
if (!OPENAI_API_KEY) {
|
||||
if (skipEnglish) {
|
||||
console.log('⏭️ Skipping German → English translation (using existing English file)');
|
||||
} else if (onlyEnglish) {
|
||||
console.log('🎯 Only translating German → English (skipping other languages)');
|
||||
}
|
||||
|
||||
// Check if OpenAI API key is set (only if we're doing actual translation)
|
||||
if (!skipEnglish && !OPENAI_API_KEY) {
|
||||
console.error('❌ OPENAI_API_KEY environment variable is not set');
|
||||
console.log('Please set your OpenAI API key: export OPENAI_API_KEY="your-api-key-here"');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Check if German file exists
|
||||
if (!fs.existsSync(GERMAN_FILE)) {
|
||||
// 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}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
// Step 1: Translate German to English
|
||||
const englishObjectString = await translateToEnglish();
|
||||
let englishObjectString;
|
||||
|
||||
if (englishObjectString) {
|
||||
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');
|
||||
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');
|
||||
} else {
|
||||
// Step 1: Translate German to English
|
||||
englishObjectString = await translateToEnglish();
|
||||
|
||||
if (!englishObjectString) {
|
||||
console.error('❌ Failed to create English translation, stopping process');
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (onlyEnglish) {
|
||||
console.log('🎉 English translation completed! Skipping other languages.');
|
||||
} else {
|
||||
// Step 2: Translate English to other languages
|
||||
await translateToOtherLanguages(englishObjectString);
|
||||
console.log('🎉 All translations completed successfully!');
|
||||
} else {
|
||||
console.error('❌ Failed to create English translation, stopping process');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user