@lingui/cli
Version:
Lingui CLI to extract messages, compile catalogs, and manage translation workflows
52 lines (51 loc) • 2.07 kB
JavaScript
export function mergeCatalog(prevCatalog, nextCatalog, forSourceLocale, options) {
prevCatalog = prevCatalog || {};
const nextKeys = Object.keys(nextCatalog);
const prevKeys = Object.keys(prevCatalog);
// this part is optimized for performance, see https://github.com/lingui/js-lingui/pull/2540
const hasKeysInBothCatalogs = prevKeys.length > 0 && nextKeys.length > 0;
let newKeys;
let mergeKeys;
let obsoleteKeys;
if (hasKeysInBothCatalogs) {
const prevKeySet = new Set(prevKeys);
const nextKeySet = new Set(nextKeys);
newKeys = nextKeys.filter((key) => !prevKeySet.has(key));
mergeKeys = nextKeys.filter((key) => prevKeySet.has(key));
obsoleteKeys = prevKeys.filter((key) => !nextKeySet.has(key));
}
else {
newKeys = nextKeys;
mergeKeys = [];
obsoleteKeys = prevKeys;
}
// Initialize new catalog with new keys
const newMessages = Object.fromEntries(newKeys.map((key) => [
key,
{
translation: forSourceLocale ? nextCatalog[key].message || key : "",
...nextCatalog[key],
},
]));
// Merge translations from previous catalog
const mergedMessages = Object.fromEntries(mergeKeys.map((key) => {
const updateFromDefaults = forSourceLocale &&
(prevCatalog[key].translation === prevCatalog[key].message ||
options.overwrite);
const translation = updateFromDefaults
? nextCatalog[key].message || key
: prevCatalog[key].translation;
const { extra } = prevCatalog[key];
return [key, { ...nextCatalog[key], extra, translation }];
}));
// Mark all remaining translations as obsolete
// Only if *options.files* is not provided
const obsoleteMessages = Object.fromEntries(obsoleteKeys.map((key) => [
key,
{
...prevCatalog[key],
...(options.files ? {} : { obsolete: true }),
},
]));
return { ...newMessages, ...mergedMessages, ...obsoleteMessages };
}