@jsverse/transloco-keys-manager
Version:
Extract translatable keys from projects that uses Transloco
45 lines • 1.58 kB
JavaScript
import { unflatten } from 'flat';
import { sync as globSync } from 'glob';
import { basename } from 'node:path';
import { readFile, writeFile } from '../utils/file.utils.js';
import { mergeDeep } from '../utils/object.utils.js';
function filterLangs(config) {
return function (path) {
return config.langs.find((lang) => lang === basename(path).replace(`.${config.fileFormat}`, ''));
};
}
/**
* In use in the Webpack Plugin
*/
export function generateKeys({ translationPath, scopeToKeys, config }) {
const scopePaths = config.scopePathMap || {};
let result = [];
for (const [scope, path] of Object.entries(scopePaths)) {
const keys = scopeToKeys[scope];
if (keys) {
result.push({
keys,
files: globSync(`${path}/*.${config.fileFormat}`).filter(filterLangs(config)),
});
}
}
for (const [scope, keys] of Object.entries(scopeToKeys)) {
if (keys) {
const isGlobal = scope === '__global';
result.push({
keys,
files: globSync(`${translationPath}/${isGlobal ? '' : scope}*.${config.fileFormat}`).filter(filterLangs(config)),
});
}
}
for (let { files, keys } of result) {
if (config.unflat) {
keys = unflatten(keys);
}
for (const filePath of files) {
const translation = readFile(filePath, { parse: true });
writeFile(filePath, mergeDeep({}, keys, translation));
}
}
}
//# sourceMappingURL=generate-keys.js.map