mirror of
https://github.com/zadam/trilium.git
synced 2026-05-07 02:25:57 +02:00
feat(scripts): import weblate CSVs into translations
This commit is contained in:
38
scripts/import-translations-from-weblate-csv.ts
Normal file
38
scripts/import-translations-from-weblate-csv.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { readFileSync, writeFileSync } from "fs";
|
||||
import { parseString } from '@fast-csv/parse';
|
||||
|
||||
const csvPath = process.argv[2];
|
||||
const translationPath = process.argv[3];
|
||||
|
||||
if (!csvPath || !translationPath) {
|
||||
console.log("Usage: input.csv translation.json")
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const csvFile = readFileSync(csvPath, "utf-8");
|
||||
const translationFile = readFileSync(translationPath, "utf-8");
|
||||
const translation = JSON.parse(translationFile);
|
||||
|
||||
parseString(csvFile, { headers: true })
|
||||
.on("error", error => {
|
||||
console.error(error);
|
||||
process.exit(2);
|
||||
})
|
||||
.on("data", data => {
|
||||
replaceTranslation(data.context, data.target);
|
||||
})
|
||||
.on("end", () => {
|
||||
writeFileSync(translationPath, JSON.stringify(translation, null, 2));
|
||||
});
|
||||
|
||||
function replaceTranslation(path: string, value: string) {
|
||||
let cursor = translation;
|
||||
const segments = path.split(".");
|
||||
const lastSegment = segments.pop();
|
||||
for (const current of segments) {
|
||||
if (!cursor[current]) cursor[current] = {};
|
||||
cursor = cursor[current];
|
||||
}
|
||||
|
||||
cursor[lastSegment] = value;
|
||||
}
|
||||
Reference in New Issue
Block a user