79 lines
2.5 KiB
JavaScript
79 lines
2.5 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const FR_DIR = path.join(__dirname, 'scripts');
|
|
const OPPORTUNITIES_FILE = path.join(__dirname, 'translation-opportunities.json');
|
|
|
|
// Charger les opportunités de traduction
|
|
const data = JSON.parse(fs.readFileSync(OPPORTUNITIES_FILE, 'utf8'));
|
|
const { reliableTranslations, opportunities } = data;
|
|
|
|
// Fonction pour échapper les caractères spéciaux pour regex
|
|
function escapeRegExp(string) {
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
}
|
|
|
|
// Statistiques
|
|
let stats = {
|
|
filesProcessed: 0,
|
|
filesModified: 0,
|
|
replacementsMade: 0,
|
|
errors: 0
|
|
};
|
|
|
|
console.log('Application automatique des traductions...\n');
|
|
console.log('='.repeat(60));
|
|
|
|
// Traiter chaque fichier avec des opportunités
|
|
opportunities.forEach(({ file, translations }) => {
|
|
const filePath = path.join(FR_DIR, file);
|
|
stats.filesProcessed++;
|
|
|
|
try {
|
|
let content = fs.readFileSync(filePath, 'utf8');
|
|
let modified = false;
|
|
let replacementsInFile = 0;
|
|
|
|
// Appliquer chaque traduction
|
|
translations.forEach(({ en, fr }) => {
|
|
// Vérifier que la chaîne anglaise existe toujours dans le fichier
|
|
if (content.includes(en)) {
|
|
// Remplacement simple (pas de regex pour éviter les problèmes avec les caractères spéciaux)
|
|
const newContent = content.split(en).join(fr);
|
|
|
|
if (newContent !== content) {
|
|
const count = (content.split(en).length - 1);
|
|
content = newContent;
|
|
modified = true;
|
|
replacementsInFile += count;
|
|
stats.replacementsMade += count;
|
|
}
|
|
}
|
|
});
|
|
|
|
// Sauvegarder si modifié
|
|
if (modified) {
|
|
fs.writeFileSync(filePath, content, 'utf8');
|
|
stats.filesModified++;
|
|
console.log(`✓ ${file} : ${replacementsInFile} remplacement(s)`);
|
|
}
|
|
|
|
} catch (error) {
|
|
stats.errors++;
|
|
console.error(`✗ ${file} : ${error.message}`);
|
|
}
|
|
});
|
|
|
|
console.log('\n' + '='.repeat(60));
|
|
console.log('Application terminée !');
|
|
console.log('='.repeat(60));
|
|
console.log(`Fichiers traités : ${stats.filesProcessed}`);
|
|
console.log(`Fichiers modifiés : ${stats.filesModified}`);
|
|
console.log(`Remplacements effectués : ${stats.replacementsMade}`);
|
|
console.log(`Erreurs : ${stats.errors}`);
|
|
console.log('='.repeat(60));
|