UNPKG

attranslate

Version:

Semi-automated Text Translator for Websites and Apps

65 lines (64 loc) 2.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PoFile = exports.potCache = void 0; const gettext_parser_1 = require("gettext-parser"); const format_cache_1 = require("../common/format-cache"); const managed_utf8_1 = require("../common/managed-utf8"); const po_ops_1 = require("./po-ops"); exports.potCache = new format_cache_1.FormatCache(); class PoFile { readTFile(args) { const rawFile = (0, managed_utf8_1.readManagedUtf8)(args.path); if (!rawFile) { return Promise.resolve(new Map()); } const potFile = (0, po_ops_1.parsePotFile)(args, rawFile); exports.potCache.insertFileCache({ path: args.path, entries: new Map(), auxData: { potFile, rawFile }, }); const tSet = (0, po_ops_1.extractPotTranslations)(args, potFile); return Promise.resolve(tSet); } writeTFile(args) { var _a; const sourcePot = (_a = exports.potCache.getOldestAuxdata()) === null || _a === void 0 ? void 0 : _a.potFile; let output; if (!sourcePot) { output = createUncachedPot(args); } else { output = createCachedPot(args, sourcePot); } (0, managed_utf8_1.writeManagedUtf8)({ path: args.path, utf8: output }); exports.potCache.purge(); } } exports.PoFile = PoFile; const compileOptions = { foldLength: 100, sort: false, }; function createCachedPot(args, sourcePot) { (0, po_ops_1.updatePotTranslations)(args, sourcePot); const buffer = gettext_parser_1.po.compile(sourcePot, compileOptions); return buffer.toString("utf-8"); } function createUncachedPot(args) { const potFile = { charset: "utf-8", headers: {}, translations: {}, }; args.tSet.forEach((value, key) => { potFile.translations[key] = { key: { msgid: key, msgstr: [value !== null && value !== void 0 ? value : ""], }, }; }); const buffer = gettext_parser_1.po.compile(potFile, compileOptions); return buffer.toString("utf-8") + "\n"; }