upd
This commit is contained in:
@@ -115,6 +115,73 @@ Here is the English translation file to translate:
|
||||
{{englishFileContent}}
|
||||
`;
|
||||
|
||||
// Function to check if source file is newer than target file
|
||||
function isSourceNewer(sourcePath, targetPath) {
|
||||
try {
|
||||
// If target doesn't exist, source is considered newer
|
||||
if (!fs.existsSync(targetPath)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const sourceStats = fs.statSync(sourcePath);
|
||||
const targetStats = fs.statSync(targetPath);
|
||||
|
||||
return sourceStats.mtime > targetStats.mtime;
|
||||
} catch (error) {
|
||||
console.error(`Error checking file timestamps for ${sourcePath} -> ${targetPath}:`, error.message);
|
||||
return true; // Default to translating if we can't check
|
||||
}
|
||||
}
|
||||
|
||||
// Function to get files that need translation from German to English
|
||||
function getFilesNeedingEnglishTranslation() {
|
||||
const filesToTranslate = [];
|
||||
|
||||
for (const fileName of TRANSLATION_FILES) {
|
||||
const germanFile = path.join(GERMAN_DIR, fileName);
|
||||
const englishFile = path.join(ENGLISH_DIR, fileName);
|
||||
|
||||
if (!fs.existsSync(germanFile)) {
|
||||
console.log(`⚠️ German file not found: ${fileName}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isSourceNewer(germanFile, englishFile)) {
|
||||
filesToTranslate.push(fileName);
|
||||
console.log(`📝 ${fileName} needs German → English translation`);
|
||||
} else {
|
||||
console.log(`⏭️ ${fileName} is up to date (German → English)`);
|
||||
}
|
||||
}
|
||||
|
||||
return filesToTranslate;
|
||||
}
|
||||
|
||||
// Function to get files that need translation from English to target language
|
||||
function getFilesNeedingTargetTranslation(langCode) {
|
||||
const filesToTranslate = [];
|
||||
const targetDir = path.join(LOCALES_DIR, langCode);
|
||||
|
||||
for (const fileName of TRANSLATION_FILES) {
|
||||
const englishFile = path.join(ENGLISH_DIR, fileName);
|
||||
const targetFile = path.join(targetDir, fileName);
|
||||
|
||||
if (!fs.existsSync(englishFile)) {
|
||||
console.log(`⚠️ English file not found: ${fileName}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isSourceNewer(englishFile, targetFile)) {
|
||||
filesToTranslate.push(fileName);
|
||||
console.log(`📝 ${fileName} needs English → ${langCode} translation`);
|
||||
} else {
|
||||
console.log(`⏭️ ${fileName} is up to date (English → ${langCode})`);
|
||||
}
|
||||
}
|
||||
|
||||
return filesToTranslate;
|
||||
}
|
||||
|
||||
// Function to read and parse JavaScript export file
|
||||
function readTranslationFile(filePath) {
|
||||
try {
|
||||
@@ -224,11 +291,20 @@ function extractJSObjectString(response) {
|
||||
|
||||
// Main translation function for multiple files
|
||||
async function translateToEnglish() {
|
||||
console.log(`🔄 Step 1: Translating German to English using ${GERMAN_TO_ENGLISH_MODEL}...`);
|
||||
console.log(`🔄 Step 1: Checking which files need German → English translation...`);
|
||||
|
||||
const filesToTranslate = getFilesNeedingEnglishTranslation();
|
||||
|
||||
if (filesToTranslate.length === 0) {
|
||||
console.log('✅ All German → English translations are up to date');
|
||||
return TRANSLATION_FILES.filter(fileName => fs.existsSync(path.join(ENGLISH_DIR, fileName)));
|
||||
}
|
||||
|
||||
console.log(`🔄 Translating ${filesToTranslate.length} files from German to English using ${GERMAN_TO_ENGLISH_MODEL}...`);
|
||||
|
||||
const translatedFiles = [];
|
||||
|
||||
for (const fileName of TRANSLATION_FILES) {
|
||||
for (const fileName of filesToTranslate) {
|
||||
const germanFile = path.join(GERMAN_DIR, fileName);
|
||||
const englishFile = path.join(ENGLISH_DIR, fileName);
|
||||
|
||||
@@ -257,15 +333,17 @@ async function translateToEnglish() {
|
||||
}
|
||||
|
||||
console.log(`✅ German to English translation completed for ${translatedFiles.length} files`);
|
||||
return translatedFiles;
|
||||
|
||||
// Return all English files that exist (both newly translated and existing)
|
||||
return TRANSLATION_FILES.filter(fileName => fs.existsSync(path.join(ENGLISH_DIR, fileName)));
|
||||
}
|
||||
|
||||
// Function to translate English to other languages (multiple files)
|
||||
async function translateToOtherLanguages(translatedFiles) {
|
||||
async function translateToOtherLanguages(availableEnglishFiles) {
|
||||
console.log(`🔄 Step 2: Translating English to other languages using ${ENGLISH_TO_OTHER_MODEL}...`);
|
||||
|
||||
for (const [langCode, langName] of Object.entries(TARGET_LANGUAGES)) {
|
||||
console.log(`🔄 Translating to ${langName} (${langCode})...`);
|
||||
console.log(`🔄 Checking ${langName} (${langCode}) translations...`);
|
||||
|
||||
// Create target language directory if it doesn't exist
|
||||
const targetDir = path.join(LOCALES_DIR, langCode);
|
||||
@@ -273,7 +351,16 @@ async function translateToOtherLanguages(translatedFiles) {
|
||||
fs.mkdirSync(targetDir, { recursive: true });
|
||||
}
|
||||
|
||||
for (const fileName of TRANSLATION_FILES) {
|
||||
const filesToTranslate = getFilesNeedingTargetTranslation(langCode);
|
||||
|
||||
if (filesToTranslate.length === 0) {
|
||||
console.log(`✅ All English → ${langName} translations are up to date`);
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log(`🔄 Translating ${filesToTranslate.length} files to ${langName}...`);
|
||||
|
||||
for (const fileName of filesToTranslate) {
|
||||
try {
|
||||
const englishFile = path.join(ENGLISH_DIR, fileName);
|
||||
const targetFile = path.join(targetDir, fileName);
|
||||
|
||||
Reference in New Issue
Block a user