@jsverse/transloco-keys-manager
Version:
Extract translatable keys from projects that uses Transloco
52 lines • 1.7 kB
JavaScript
import { flatten, unflatten } from 'flat';
import { po } from 'gettext-parser';
import { getConfig } from '../../config.js';
import { mergeDeep, stringify } from '../../utils/object.utils.js';
import { removeExtraKeys } from './remove-extra-keys.js';
function resolveTranslation({ currentTranslation, translation, replace, removeExtraKeys: removeExtraKeysParam, }) {
if (replace) {
return mergeDeep({}, translation);
}
if (removeExtraKeysParam) {
currentTranslation = removeExtraKeys(currentTranslation, translation);
}
return mergeDeep({}, translation, currentTranslation);
}
function createJson(config) {
const { translation } = config;
return stringify(resolveTranslation({
...config,
translation: getConfig().unflat
? unflatten(translation, { object: true })
: translation,
}));
}
function createPot(config) {
const resolved = getConfig().unflat
? flatten(resolveTranslation(config))
: resolveTranslation(config);
return po
.compile({
charset: 'utf-8',
headers: {
'mime-version': '1.0',
'content-type': 'text/plain; charset=utf-8',
'content-transfer-encoding': '8bit',
},
translations: {
'': Object.entries(resolved).reduce((acc, [msgid, msgstr]) => ({
...acc,
[msgid]: { msgid, msgstr },
}), {}),
},
})
.toString('utf8');
}
const compilers = {
json: createJson,
pot: createPot,
};
export function createTranslation(config) {
return compilers[config.fileFormat](config);
}
//# sourceMappingURL=create-translation.js.map