Skip to content

Commit f87d8af

Browse files
committed
add back needsTranslation which is invoked by yarn trans abs
1 parent 8989ffa commit f87d8af

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

i18n/index.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,24 @@ async function setupCleanupHandlers() {
174174
});
175175
}
176176

177+
function needsTranslation(enFilePath: string, lang: string): boolean {
178+
const cnFilePath = enFilePath.replace(
179+
enFilePath.sep + "en" + enFilePath.sep,
180+
enFilePath.sep + ".." + enFilePath.sep + "i18n" + enFilePath.sep + "translation_output" + enFilePath.sep + lang + enFilePath.sep
181+
);
182+
try {
183+
const cnStats = await fs.promise.stat(cnFilePath);
184+
if (!cnStats.isFile()) {
185+
return true;
186+
}
187+
188+
const enStats = await fs.promise.stat(enFilePath);
189+
return enStats.mtime > cnStats.mtime;
190+
} catch (error) {
191+
return true;
192+
}
193+
}
194+
177195
// Function to recursively find all XML files in a directory
178196
async function findAllXmlFiles(directory: string): Promise<string[]> {
179197
const files = await fs.promises.readdir(directory);
@@ -205,6 +223,7 @@ async function findAllXmlFiles(directory: string): Promise<string[]> {
205223

206224
// Get the absolute path to the xml/en directory using proper path resolution
207225
const enDirPath = path.resolve(__dirname, "../xml/en");
226+
let absent: boolean = false;
208227

209228
if (process.argv[2] === "test") {
210229
if (process.argv.length !== 5) {
@@ -219,7 +238,10 @@ async function findAllXmlFiles(directory: string): Promise<string[]> {
219238
console.error('test error: ', e);
220239
}
221240
return;
222-
} if (process.argv[2] === "all" || process.argv.length <= 2) {
241+
} if (process.argv[2] === "all" || process.argv.length <= 2 || process.argv[2] === "abs") {
242+
if (process.argv[2] === "abs") {
243+
absent = true;
244+
}
223245
// Find all XML files
224246
console.log(`Scanning directory: ${enDirPath}`);
225247
filestoTranslate = await findAllXmlFiles(enDirPath);
@@ -250,6 +272,12 @@ async function findAllXmlFiles(directory: string): Promise<string[]> {
250272
// Process each file in the batch, but handle errors individually
251273
const results = await Promise.allSettled(
252274
batch.map(async file => {
275+
if (absent) {
276+
if (!needsTranslation(file, lang)) {
277+
console.log(`Skipped translation for ${file} to language ${lang} (yarn trans abs)`);
278+
return { file, success: true };
279+
}
280+
}
253281
try {
254282
console.log(`Starting translation for ${file}`);
255283
await translate(lang, file);

0 commit comments

Comments
 (0)