-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunOnFolder.ts
57 lines (45 loc) · 2 KB
/
runOnFolder.ts
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
import { main as translateFile } from './openai-translate';
import path from 'path';
import fs from 'fs';
import { Settings } from './settings';
function findI18nFolders(dir: string): string[] {
let i18nFolders: string[] = [];
// Function to recursively search for i18n folders
function searchFolders(currentDir) {
const files = fs.readdirSync(currentDir);
files.forEach(file => {
const fullPath = path.join(currentDir, file);
const i18nPath = path.join(fullPath, Settings.lookInDirectoriesNamed);
const defaultJsonPath = path.join(i18nPath, Settings.sourceLanguageFileName);
const targetLanguageJsonPath = path.join(i18nPath, Settings.targetLanguageFileName);
if (fs.statSync(fullPath).isDirectory()) {
// Check for i18n folder in the current directory
if (fs.existsSync(i18nPath) && fs.existsSync(defaultJsonPath) && !fs.existsSync(targetLanguageJsonPath)) {
i18nFolders.push(i18nPath);
}
// Continue searching in the subdirectory
searchFolders(fullPath);
}
});
}
searchFolders(dir);
return i18nFolders;
}
const inputDir = path.resolve(Settings.folderLocationToSearchIn);
const i18nFolders = findI18nFolders(inputDir);
console.log(`Found ${i18nFolders.length} folders`, JSON.stringify(i18nFolders, null, 2));
async function main() {
console.log(`Translating ${i18nFolders.length} folders...`);
await new Promise(resolve => setTimeout(resolve, 3000));
for (let i = 0; i < i18nFolders.length; i++) {
const i18nPath = i18nFolders[i];
if (i18nPath === undefined) {
continue;
}
const defaultJsonPath = path.join(i18nPath, Settings.sourceLanguageFileName);
const targetLanguageJsonPath = path.join(i18nPath, Settings.targetLanguageFileName);
console.log(`processing ${defaultJsonPath} to ${targetLanguageJsonPath}`);
await translateFile(defaultJsonPath, targetLanguageJsonPath);
}
}
main();