forked from longjiang/zerotohero-nuxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-translation-files.js
104 lines (90 loc) · 3.15 KB
/
generate-translation-files.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
// Usage: node generate-translation-files.js
// Description: Converts CSV files in static/translations-csv to JSON files in static/locales
const fs = require("fs");
const csv = require("csv-parser");
const mkdirp = require("mkdirp");
async function parseCSV(filePath) {
return new Promise((resolve, reject) => {
const results = [];
fs.createReadStream(filePath)
.pipe(csv())
.on("data", (data) => results.push(data))
.on("end", () => {
resolve(results);
})
.on("error", (err) => {
reject(err);
});
});
}
async function codeByIso639_3(iso639_3, langData) {
const languageRow = langData.find((row) => row["iso639-3"] === iso639_3);
if (languageRow && languageRow["iso639-1"] && languageRow["iso639-1"].trim() !== "") {
return languageRow["iso639-1"].trim();
} else if (languageRow && languageRow["iso639-3"] && languageRow["iso639-3"].trim() !== "") {
return languageRow["iso639-3"].trim();
} else if (languageRow && languageRow["glottologId"] && languageRow["glottologId"].trim() !== "") {
return languageRow["glottologId"].trim();
} else {
return "";
}
}
async function readAndMergeTranslations(filename, translations, langData) {
return new Promise(async (resolve, reject) => {
const results = [];
fs.createReadStream(filename)
.pipe(csv())
.on("data", (data) => results.push(data))
.on("end", async () => {
const languagesLong = Object.keys(results[0]);
for (const lang of languagesLong) {
const langShort = await codeByIso639_3(lang, langData);
if (!translations[langShort]) {
translations[langShort] = {};
}
results.forEach((row) => {
const key = row[languagesLong[0]];
const translation = row[lang] || key;
translations[langShort][key] = translation;
});
}
resolve(translations);
})
.on("error", reject);
});
}
async function startConversion() {
const langData = await parseCSV("static/data/languages/languages.csv.txt");
const csvFiles = [
"static/translations-csv/translations-general.csv",
"static/translations-csv/translations-collocations.csv",
"static/translations-csv/translations-countries.csv",
"static/translations-csv/translations-languages.csv",
"static/translations-csv/translations-topics.csv",
];
let translations = {};
for (const csvFile of csvFiles) {
translations = await readAndMergeTranslations(csvFile, translations, langData);
}
const outputDir = "static/locales";
mkdirp.sync(outputDir);
for (const [lang, transDict] of Object.entries(translations)) {
if (!lang) {
continue;
}
// Create an array from the object, sort it by keys, and then reconstruct the object
const sortedTransDict = Object.entries(transDict)
.sort((a, b) => a[0].localeCompare(b[0]))
.reduce((obj, [key, value]) => {
obj[key] = value;
return obj;
}, {});
fs.writeFileSync(
`${outputDir}/${lang}.json`,
JSON.stringify(sortedTransDict, null, 2),
"utf-8"
);
}
console.log("Conversion complete!");
}
startConversion();