-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.js
146 lines (116 loc) · 6.49 KB
/
translate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const fs = require('fs-extra');
const path = require('path');
const minimist = require('minimist');
async function main() {
const args = minimist(process.argv.slice(2), {
boolean: ['recursive', 'filenameLang', 'move'],
string: ['input', 'exceptions', 'output', 'defaultLang']
});
const inputFolder = args.input;
const outputFolder = args.output;
const recursive = args.recursive || false;
const exceptionsRegex = args.exceptions ? new RegExp(args.exceptions) : null;
const defaultLang = args.defaultLang || 'en';
const filenameLang = args.filenameLang || false;
const moveFiles = args.move || false; // Conservamos la opción 'move' por si se necesita en el futuro, pero ahora solo copiaremos los traducidos.
if (!inputFolder) {
console.error("Error: Debe especificar la carpeta de entrada con --input <carpeta>");
process.exit(1);
}
if (!outputFolder) {
console.error("Error: Debe especificar la carpeta de salida con --output <carpeta>");
process.exit(1);
}
await fs.ensureDir(outputFolder);
async function processFiles(folderPath, outputBasePath) {
try {
const items = await fs.readdir(folderPath);
for (const item of items) {
const itemPath = path.join(folderPath, item);
// La ruta de salida ahora se construye correctamente relativa a la carpeta de entrada *dentro* de la carpeta de salida general
const relativePathFromInput = path.relative(inputFolder, itemPath);
const outputPath = path.join(outputBasePath, relativePathFromInput);
const stat = await fs.stat(itemPath);
if (exceptionsRegex && exceptionsRegex.test(itemPath)) {
console.log(`Excepción aplicada a: ${itemPath}`);
continue;
}
if (stat.isDirectory()) {
if (recursive) {
await fs.ensureDir(outputPath); // Asegura que la carpeta de salida exista
await processFiles(itemPath, outputBasePath); // Recursivamente
}
} else if (stat.isFile() && (item.endsWith('.html') || item.endsWith('.js'))) {
await translateFile(itemPath, outputBasePath);
}
}
} catch (error) {
console.error(`Error al procesar la carpeta ${folderPath}: ${error}`);
}
}
async function translateFile(filePath, outputBasePath) {
try {
const content = await fs.readFile(filePath, 'utf8');
const fileName = path.basename(filePath, path.extname(filePath));
const fileExt = path.extname(filePath);
const translationsFilePath = path.join(path.dirname(filePath), `${fileName}.json`);
if (!await fs.exists(translationsFilePath)) {
console.warn(`Archivo de traducciones no encontrado: ${translationsFilePath}`);
return;
}
const translations = await fs.readJson(translationsFilePath);
const languages = Object.keys(translations);
if (languages.length === 0) {
console.warn(`Archivo de traducciones vacío o sin idiomas definidos: ${translationsFilePath}`);
return;
}
for (const lang of languages) {
const langTranslations = translations[lang];
let translatedContent = content;
for (const key in langTranslations) {
const regex = new RegExp(`\\[\\[${key}\\]\\]`, 'g');
translatedContent = translatedContent.replace(regex, langTranslations[key]);
}
let outputFolderPathLang, outputFilePathLang;
const relativeDirFromInput = path.dirname(path.relative(inputFolder, filePath)); // Ruta relativa del directorio del archivo desde la carpeta de entrada
if (filenameLang) {
// Usar lenguaje en nombre de archivo
outputFolderPathLang = path.join(outputBasePath, relativeDirFromInput); // Usar la ruta relativa dentro de la carpeta de salida
await fs.ensureDir(outputFolderPathLang);
outputFilePathLang = path.join(outputFolderPathLang, `${fileName}.${lang}${fileExt}`); // index.html -> index.es.html
} else {
// Usar carpetas por idioma
outputFolderPathLang = path.join(outputBasePath, relativeDirFromInput, lang); // Añadir carpeta de idioma en la ruta relativa
await fs.ensureDir(outputFolderPathLang);
outputFilePathLang = path.join(outputFolderPathLang, path.basename(filePath));
}
// Copiar el archivo traducido a la carpeta de salida (o mover si moveFiles es true, aunque ahora se prefiere solo copiar)
if (moveFiles) { // En este caso, 'moveFiles' aplicaría solo a los *traducidos*, no a los originales.
await fs.move(outputFilePathLang, outputFilePathLang, { overwrite: true }); // Ejemplo de cómo se *movería* si fuera necesario (aquí, a sí mismo, es una copia efectiva)
console.log(`Archivo traducido (movido) a ${lang}: ${outputFilePathLang}`);
} else {
await fs.writeFile(outputFilePathLang, translatedContent); // Siempre escribimos el contenido traducido
console.log(`Archivo traducido (copiado) a ${lang}: ${outputFilePathLang}`);
}
// if (path.basename(filePath) === 'index.html' && lang === defaultLang) {
// const rootOutputIndex = path.join(outputFolder, 'index.html');
// await fs.copyFile(outputFilePathLang, rootOutputIndex);
// console.log(`Copia de index.html (idioma por defecto ${defaultLang}) creada en: ${rootOutputIndex}`);
// }
}
} catch (error) {
console.error(`Error al traducir el archivo ${filePath}: ${error}`);
}
}
if (!inputFolder) {
console.error("Error: Debe especificar la carpeta de entrada con --input <carpeta>");
process.exit(1);
}
if (!outputFolder) {
console.error("Error: Debe especificar la carpeta de salida con --output <carpeta>");
process.exit(1);
}
await processFiles(inputFolder, outputFolder);
console.log("Proceso de traducción completado.");
}
main();