mirror of
https://github.com/kmvan/x-prober.git
synced 2026-08-13 04:31:40 +08:00
38 lines
920 B
JavaScript
38 lines
920 B
JavaScript
/**
|
|
* @version 1.0.2
|
|
*/
|
|
|
|
import { existsSync, readFileSync } from "node:fs";
|
|
import gettextParser from "gettext-parser";
|
|
|
|
export class PoParser {
|
|
poPath = "";
|
|
items = {};
|
|
constructor({ poPath }) {
|
|
this.poPath = poPath;
|
|
if (!existsSync(this.poPath)) {
|
|
throw new Error(`${this.poPath} not exists`);
|
|
}
|
|
}
|
|
parse() {
|
|
const input = readFileSync(this.poPath);
|
|
const po = gettextParser.po.parse(input);
|
|
for (const group of Object.values(po.translations)) {
|
|
for (const item of Object.values(group)) {
|
|
const id = item.msgid;
|
|
const [str] = item.msgstr;
|
|
const ctxt = item?.msgctxt || "";
|
|
const key = ctxt === "" ? id : `${ctxt}|${id}`;
|
|
this.items[key] = str;
|
|
}
|
|
}
|
|
this.items = Object.keys(this.items)
|
|
.sort()
|
|
.reduce((r, k) => {
|
|
r[k] = this.items[k];
|
|
return r;
|
|
}, {});
|
|
return this.items;
|
|
}
|
|
}
|