Skip to content

Commit

Permalink
Merge multiple .po before creating golang file
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Aug 20, 2023
1 parent 3c0dcb6 commit 986a737
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
},
"homepage": "https://github.com/hebcal/hebcal-locales#readme",
"dependencies": {
"@hebcal/core": "^4.3.0"
"@hebcal/core": "^4.3.1"
},
"files": [
"dist"
],
"scripts": {
"build:rollup": "rollup -c",
"po2json": "node ./po2json.js po/*.po",
"po2golang": "node ./po2golang.js ../hebcal-es6/po/ashkenazi.po ../hebcal-es6/po/he.po po/*.po",
"po2golang": "node ./po2golang.js ../hebcal-es6/po/ashkenazi.po ../hebcal-es6/po/he.po ../hebcal-learning/po/*.po po/*.po",
"build": "npm run po2json && npm run build:rollup",
"prepublish": "npm run build",
"test": "ava"
Expand Down
52 changes: 35 additions & 17 deletions po2golang.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,26 @@ const parser = require('ttag-cli/dist/src/lib/parser');
const utils = require('ttag-cli/dist/src/lib/utils');

const langs = new Set();
const parsedPoData = new Map();
for (const arg of process.argv.slice(2)) {
const langName = path.basename(arg).replace(/\.po$/, '').replace(/\./g, '_');
const contents = fs.readFileSync(arg);
const poData = parser.parse(
contents.toString().normalize(),
);
const langName = getLangFromBase(arg);
const arr = parsedPoData.get(langName);
if (arr) {
arr.push(poData);
} else {
parsedPoData.set(langName, [poData]);
}
langs.add(langName);
}

for (const langName of langs.values()) {
const outpath = `./go/strings_${langName}.go`;
console.log(`${arg} => ${outpath}`);
writePoFile(arg, outpath, langName);
console.log(`${langName} => ${outpath}`);
writePoFile(parsedPoData.get(langName), outpath, langName);
langs.add(langName);
}

Expand Down Expand Up @@ -43,23 +58,26 @@ for (const langName of langs.values()) {
outstream.write(`\t}\n\treturn key, false\n}\n`);
outstream.end();

function writePoFile(inpath, outpath, langName) {
const poData = parser.parse(
fs.readFileSync(inpath).toString().normalize(),
);
function getLangFromBase(arg) {
return path.basename(arg).replace(/\.po$/, '').replace(/\./g, '_');
}

function writePoFile(poDatas, outpath, langName) {
const outstream = fs.createWriteStream(outpath, {flags: 'w'});
outstream.write('package locales\n\n');
outstream.write(`var dict_${langName} = map[string]string{\n`);
const messages = utils.iterateTranslations(poData.translations);
for (const msg of messages) {
const msgid = msg.msgid;
const msgstr = msg.msgstr;
if (typeof msgid === 'string' && msgid.length &&
Array.isArray(msgstr) && typeof msgstr[0] === 'string' && msgstr[0].length) {
const src = msgid.replace(/\"/g, '\\"');
const dest = msgstr[0].replace(/\"/g, '\\"');
if (dest !== "") {
outstream.write(`\t"${src}": "${dest}",\n`);
for (const poData of poDatas) {
const messages = utils.iterateTranslations(poData.translations);
for (const msg of messages) {
const msgid = msg.msgid;
const msgstr = msg.msgstr;
if (typeof msgid === 'string' && msgid.length &&
Array.isArray(msgstr) && typeof msgstr[0] === 'string' && msgstr[0].length) {
const src = msgid.replace(/\"/g, '\\"');
const dest = msgstr[0].replace(/\"/g, '\\"');
if (dest !== '') {
outstream.write(`\t"${src}": "${dest}",\n`);
}
}
}
}
Expand Down

0 comments on commit 986a737

Please sign in to comment.