-
Notifications
You must be signed in to change notification settings - Fork 2
/
extract-translations.ts
92 lines (84 loc) · 3.01 KB
/
extract-translations.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
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
import { readdir, readFile, stat, writeFile } from 'fs/promises';
import { extname, join } from 'path';
const translateRegex = /\{\{\s*([^{|]+?)(?:\s*\|\s*translate\s*\}\})/gs;
const ternaryRegex = /([\?:]\s?('.+')){1,2}/gs;
const appPath = join(__dirname, 'src', 'app');
const i18nPath = join(__dirname, 'src', 'assets', 'i18n');
const languages = ['en', 'mn'];
const removeApostrophe = (str: string) => {
if (str[0] === "'" && str[str.length - 1] === "'") {
return str.substring(1, str.length - 1);
}
return str;
};
const extractTranslationsFromFile = async (file: string): Promise<string[]> => {
const translations = new Array<string>();
const content = await readFile(file, 'utf8');
let match;
while ((match = translateRegex.exec(content))) {
const capture = match[1]
.split(/[\n\r]/)
.map(line => line.trim())
.join(' ');
const ternaryMatch = ternaryRegex.exec(capture);
if (ternaryMatch) {
const ternaryCaptures = ternaryMatch[2].split(':').map(line => line.trim());
translations.push(...ternaryCaptures);
} else {
translations.push(capture);
}
}
return translations.map(removeApostrophe);
};
const aggregateFilePaths = async (directory: string): Promise<string[]> => {
const files = await readdir(directory);
const filePaths = new Array<string>();
for (const file of files) {
const path = join(directory, file);
const pathStat = await stat(path);
if (pathStat.isDirectory()) {
filePaths.push(...(await aggregateFilePaths(join(directory, file))));
} else if (extname(file) === '.html') {
filePaths.push(path);
}
}
return filePaths;
};
const fileExists = async (path: string) => {
try {
await stat(path);
return true;
} catch (e) {
return false;
}
};
const combineTranslations = async (translations: string[]) => {
const translateObj: Record<string, string> = {};
for (const translation of translations) {
translateObj[translation] = translation;
}
for (const language of languages) {
const path = join(i18nPath, `${language}.json`);
if (!(await fileExists(path))) {
await writeFile(path, JSON.stringify(translateObj, null, 2));
continue;
}
const content = await readFile(path, 'utf8');
const parsedContent = JSON.parse(content);
for (const key of Object.keys(translateObj)) {
if (!parsedContent[key]) {
parsedContent[key] = translateObj[key];
}
}
await writeFile(path, JSON.stringify(parsedContent, null, 2));
}
};
(async () => {
const paths = await aggregateFilePaths(appPath);
const translations = await Promise.all(paths.flatMap(extractTranslationsFromFile));
const uniqueTranslations = Array.from(new Set<string>(translations.flat()));
const sortedTranslations = uniqueTranslations.sort((a, b) => a.localeCompare(b));
console.log(`Found ${sortedTranslations.length} translation strings`);
await combineTranslations(sortedTranslations);
console.log(`Wrote translations for ${languages.length} languages: ${languages.join(', ')}`);
})();