-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathgettext2json.js
30 lines (26 loc) · 975 Bytes
/
gettext2json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import Gettext from '@postalsys/gettext';
import { po } from 'gettext-parser';
import { js2i18next } from 'gettext-converter';
import fromCallback from 'p-from-callback';
export default function gettextToI18next(locale, body, options = {}) {
return addTextLocale(locale, body, options)
.then((translations) => (translations ? js2i18next({ translations }, { ...options, locale }) : {}))
.then((json) => JSON.stringify(json, null, 4));
}
/*
* gettext --> barebone json
*/
function addTextLocale(locale, body, options = {}) {
const gt = new Gettext();
const domain = 'messages';
const {
filter,
gettextDefaultCharset = 'UTF-8', // eslint-disable-line unicorn/text-encoding-identifier-case
} = options;
if (body.length > 0) {
gt.addTranslations(locale, domain, po.parse(body, gettextDefaultCharset));
}
return filter
? fromCallback((cb) => filter(gt, locale, cb))
: Promise.resolve(gt.catalogs[locale]?.[domain].translations);
}