rucken
Version:
Console tools and scripts for nx and not only that I (EndyKaufman) use to automate the workflow and speed up the development process
173 lines • 7.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parse = parse;
exports.parseFileSync = parseFileSync;
const tslib_1 = require("tslib");
const fs_1 = require("fs");
const gettext_parser_1 = tslib_1.__importDefault(require("gettext-parser"));
const gettext_to_messageformat_1 = require("./gettext-to-messageformat");
function parse(buffer, options) {
// Setup options and load in defaults
options = options || {};
const defaults = {
pretty: false,
fuzzy: false,
stringify: false,
format: 'raw',
domain: 'messages',
charset: 'utf8',
fullMF: false,
mfOptions: {},
};
for (const property in defaults) {
const key = property;
options[key] =
'undefined' !== typeof options[key]
? options[key]
: defaults[property];
}
let mfTranslations = {};
let result;
// defer to gettext-to-messageformat for the 'mf' format option
// use all g2m default replacements except for: pattern: /[\\{}#]/g, replacement: '\\$&'
if (options.format === 'mf') {
const poString = buffer.toString();
// if the Plural-Forms header is missing, g2m needs a function or will throw an error
const mfOptions = poString.includes('"Plural-Forms:')
? options.mfOptions
: Object.assign({}, {
pluralFunction: () => 0,
}, options.mfOptions);
result =
Object.keys(mfOptions || {}).length > 0
? (0, gettext_to_messageformat_1.parsePo)(buffer, mfOptions || {})
: (0, gettext_to_messageformat_1.parsePo)(buffer, {});
if (options.fullMF) {
return options.stringify
? // @ts-expect-error - JSON.stringify with undefined spacing
JSON.stringify(result, options.pretty || false ? 3 : undefined)
: result;
}
// simplify the output to only return the translations
if (result) {
const resultData = result;
if (resultData['translations']) {
const translations = resultData['translations'];
if (translations['']) {
mfTranslations = translations[''];
// include the default translations at the top level to keep compatibility as much as possible
Object.keys(translations).forEach(function (context) {
if (context === '') {
Object.keys(translations['']).forEach(function (key) {
mfTranslations[key] =
translations[''][key];
});
}
else {
mfTranslations[context] =
translations[context];
}
});
}
else {
mfTranslations = translations || {};
}
}
}
return options.stringify
? // @ts-expect-error - JSON.stringify with undefined spacing
JSON.stringify(mfTranslations, options.pretty || false ? 3 : undefined)
: mfTranslations;
}
// Parse the PO file
const parsed = gettext_parser_1.default.po.parse(buffer);
// Create gettext/Jed compatible JSON from parsed data
const contexts = parsed.translations;
let resultRaw = {};
Object.keys(contexts).forEach(function (context) {
const translations = parsed.translations[context];
const pluralForms = parsed.headers ? parsed.headers['plural-forms'] : '';
Object.keys(translations).forEach(function (key) {
const t = translations[key];
const translationKey = context.length ? context + '\u0004' + key : key;
const fuzzy = t.comments &&
t.comments.flag &&
t.comments.flag.match(/fuzzy/) !== null;
if (!fuzzy || options?.fuzzy) {
if (options?.format === 'jed') {
resultRaw[translationKey] = [
t.msgid_plural ? t.msgid_plural : null,
...t.msgstr,
];
}
else {
if (pluralForms === 'nplurals=1; plural=0;') {
const msgstr = t.msgid_plural ? [t.msgstr] : t.msgstr;
resultRaw[translationKey] = [
t.msgid_plural ? t.msgid_plural : null,
...msgstr,
];
}
else {
resultRaw[translationKey] = [
t.msgid_plural ? t.msgid_plural : null,
...t.msgstr,
];
}
}
}
// In the case of fuzzy or empty messages, use msgid(/msgid_plural)
if (options?.['fallback-to-msgid'] &&
((fuzzy && !options?.fuzzy) || t.msgstr[0] === '')) {
resultRaw[translationKey] = [
t.msgid_plural ? t.msgid_plural : null,
].concat(t.msgid_plural ? [key, t.msgid_plural] : [key]);
}
});
});
// Attach headers (overwrites any empty translation keys that may have somehow gotten in)
if (parsed.headers) {
resultRaw[''] = parsed.headers;
}
if (options.format === 'mf') {
delete resultRaw[''];
}
// Make JSON fully Jed-compatible
if ((options.format || '').indexOf('jed') === 0) {
const jed = {
domain: options.domain,
locale_data: {},
};
if (options.format === 'jed') {
for (const key in resultRaw) {
// eslint-disable-next-line no-prototype-builtins
if (resultRaw.hasOwnProperty(key) && key !== '') {
const entry = resultRaw[key];
for (let i = 2; i < entry.length; i++) {
if ('' === entry[i]) {
entry[i] = entry[0];
}
}
entry.shift();
}
}
}
const domain = options.domain || 'messages';
jed.locale_data[domain] = resultRaw;
jed.locale_data[domain][''] = {
domain: domain,
plural_forms: (resultRaw[''] || {})['plural-forms'],
lang: (resultRaw[''] || {})['language'],
};
resultRaw = jed;
}
return options.stringify
? // @ts-expect-error - JSON.stringify with undefined spacing
JSON.stringify(resultRaw, options.pretty || false ? 3 : undefined)
: resultRaw;
}
function parseFileSync(fileName, options) {
const data = (0, fs_1.readFileSync)((0, fs_1.realpathSync)(fileName));
return parse(data, options);
}
//# sourceMappingURL=po2json.js.map