trivabble/l10n/makejs.js

121 lines
3.0 KiB
JavaScript
Raw Permalink Normal View History

2016-02-28 20:23:41 +01:00
#!/usr/bin/env node
2020-05-16 10:02:18 +02:00
/*eslint strict: [2, "global"]*/
/*eslint no-sync: ["error", { allowAtRootLevel: true }]*/
2016-02-28 20:23:41 +01:00
2020-05-16 10:02:18 +02:00
"use strict";
2020-05-17 09:03:05 +02:00
const ROOT = "../public/l10n/js";
2016-02-28 20:23:41 +01:00
2020-05-16 10:02:18 +02:00
/* Builds translation files. */
2016-02-28 20:23:41 +01:00
2020-05-16 10:02:18 +02:00
const fs = require("fs");
2020-05-17 09:03:05 +02:00
const path = require("path");
2020-05-16 10:02:18 +02:00
let po;
let i;
let len;
2016-02-28 20:23:41 +01:00
function skipLine() {
2020-05-16 10:02:18 +02:00
while (i < len && po[i] !== "\n") {
++i;
}
++i;
2016-02-28 20:23:41 +01:00
}
function skipSpaces() {
2020-05-16 10:02:18 +02:00
while (i < len && !po[i].trim()) {
++i;
}
2016-02-28 20:23:41 +01:00
}
function parseString() {
2020-05-16 10:02:18 +02:00
skipSpaces();
if (po[i] !== '"') {
return "";
}
++i;
const deb = i;
while (i < len) {
if (po[i] === "\\") {
++i;
} else if (po[i] === '"') {
const str1 = po.substring(deb, i++);
const end = i;
skipSpaces();
const ndeb = i;
const str2 = parseString();
2016-02-28 20:23:41 +01:00
2020-05-16 10:02:18 +02:00
if (i === ndeb) { // we did not parse anything
i = end;
return str1;
}
2016-02-28 20:23:41 +01:00
2020-05-16 10:02:18 +02:00
return str1 + str2;
}
++i;
}
throw new Error("not ended string at character " + deb);
}
2016-02-28 20:23:41 +01:00
2020-05-16 10:02:18 +02:00
let msgid;
let msgstr;
2016-02-28 20:23:41 +01:00
2020-05-17 09:03:05 +02:00
fs.mkdirSync(ROOT, {recursive: true});
2020-05-16 10:02:18 +02:00
for (const lang of fs.readdirSync("po")) {
2020-05-17 09:03:05 +02:00
const jsFile = fs.openSync(path.join(ROOT, lang + ".js"), "w");
2020-05-16 10:02:18 +02:00
fs.writeSync(jsFile, "(function(){var ");
let translationFunction = "translationFunction";
2016-02-28 20:23:41 +01:00
2020-05-16 10:02:18 +02:00
for (const poFile of fs.readdirSync("po/" + lang)) {
translationFunction = fs.readFileSync("pot/" + poFile + "t", {encoding: "utf-8"})
.match(/#TranslationFunction[\s]+(?<functionName>[\S]+)/u).groups.functionName;
2016-02-28 20:23:41 +01:00
2020-05-16 10:02:18 +02:00
fs.writeSync(jsFile, "_=" + translationFunction + ".l10n;");
2016-02-28 20:23:41 +01:00
2020-05-16 10:02:18 +02:00
po = fs.readFileSync("po/" + lang + "/" + poFile, {encoding: "utf-8"});
i = 0;
len = po.length;
while (i < len) {
2016-02-28 20:23:41 +01:00
skipSpaces();
2020-05-16 10:02:18 +02:00
if (po.substr(i, 5) === "msgid") {
if (po[i + 5].trim() && po[i + 5] !== '"') {
skipLine(); // don't understand this line
} else {
i += 5;
skipSpaces();
msgid = parseString();
}
} else if (po.substr(i, 6) === "msgstr") {
if (po[i + 6].trim() && po[i + 6] !== '"') {
skipLine(); // don't understand this line
} else {
i += 6;
msgstr = parseString();
fs.writeSync(
jsFile,
'_("' + lang + '","' + msgid.replace(/\n/gu, "") + '","' + msgstr.replace(/\n/gu, "") + '");'
);
}
2016-02-28 20:23:41 +01:00
}
2020-05-16 10:02:18 +02:00
// if po[i] === "#", ignore
skipLine();
}
}
fs.writeSync(
jsFile,
"if(" + translationFunction + ".applyL10n){" + translationFunction + ".applyL10n();}})();"
);
fs.close(jsFile, function (e) {
if (e) {
console.error(e);
}
});
2016-02-28 20:23:41 +01:00
}