@jsverse/transloco-keys-manager
Version:
Extract translatable keys from projects that uses Transloco
44 lines • 1.29 kB
JavaScript
import { unflatten } from 'flat';
import fs from 'fs-extra';
import { po } from 'gettext-parser';
import { getConfig } from '../../config.js';
function parseJson(path) {
return fs.readJsonSync(path, { throws: false }) || {};
}
function parsePot(path) {
try {
const file = fs.readFileSync(path, 'utf8');
const parsed = po.parse(file, 'utf8');
if (!Object.keys(parsed.translations).length) {
return {};
}
const value = Object.keys(parsed.translations[''])
.filter((key) => key.length > 0)
.reduce((acc, key) => {
return {
...acc,
[key]: parsed.translations[''][key].msgstr.pop(),
};
}, {});
return getConfig().unflat
? unflatten(value, {
object: true,
})
: value;
}
catch (e) {
if (e.code === 'ENOENT') {
return {};
}
console.warn('Something is wrong with the provided file at "%s":', path, e.message);
return {};
}
}
const parsers = {
json: parseJson,
pot: parsePot,
};
export function getCurrentTranslation({ path, fileFormat, }) {
return parsers[fileFormat](path);
}
//# sourceMappingURL=get-current-translation.js.map