@vendure/ngx-translate-extract
Version:
Extract strings from projects using ngx-translate
52 lines • 2 kB
JavaScript
import { TranslationCollection } from '../utils/translation.collection.js';
import pkg from 'gettext-parser';
const { po } = pkg;
export class PoCompiler {
extension = 'po';
domain = '';
includeSources = true;
constructor(options) {
this.includeSources = options?.poSourceLocation ?? true;
}
compile(collection) {
const data = {
charset: 'utf-8',
headers: {
'mime-version': '1.0',
'content-type': 'text/plain; charset=utf-8',
'content-transfer-encoding': '8bit'
},
translations: {
[this.domain]: Object.keys(collection.values)
.reduce((translations, key) => {
const entry = collection.get(key);
const comments = this.includeSources ? { reference: entry.sourceFiles?.join('\n') } : undefined;
return {
...translations,
[key]: {
msgid: key,
msgstr: entry.value,
comments: comments
}
};
}, {})
}
};
return po.compile(data).toString('utf8');
}
parse(contents) {
const collection = new TranslationCollection();
const parsedPo = po.parse(contents, 'utf8');
if (!parsedPo.translations.hasOwnProperty(this.domain)) {
return collection;
}
const values = Object.keys(parsedPo.translations[this.domain])
.filter((key) => key.length > 0)
.reduce((result, key) => ({
...result,
[key]: { value: parsedPo.translations[this.domain][key].msgstr.pop(), sourceFiles: parsedPo.translations[this.domain][key].comments?.reference?.split('\n') || [] }
}), {});
return new TranslationCollection(values);
}
}
//# sourceMappingURL=po.compiler.js.map