next-translate-scanner
Version:
Scan next-translate code for translations and update json files.
70 lines (69 loc) • 3.38 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const key_without_plural_1 = __importDefault(require("./key-without-plural"));
const is_plural_key_1 = __importDefault(require("./is-plural-key"));
const order_object_by_key_1 = __importDefault(require("./order-object-by-key"));
const get_plural_keys_1 = __importDefault(require("./get-plural-keys"));
const updateExistingTranslations = (newTranslations, existingTranslations, config) => {
const mergedTranslations = {};
let namespaces = [...new Set([...Object.keys(newTranslations)])];
for (const locale of config.locales) {
namespaces = [...namespaces, ...Object.keys(existingTranslations[locale])];
}
namespaces = [...new Set(namespaces)];
namespaces.sort();
for (const locale of config.locales) {
mergedTranslations[locale] = {};
for (const namespace of namespaces) {
const existingTransForNamespace = existingTranslations[locale]?.[namespace] || {};
let temp = { ...newTranslations[namespace] };
if (existingTranslations[locale]?.[namespace]) {
for (const [existingKey, existingTranslation] of Object.entries(existingTransForNamespace)) {
if (!!existingTranslation) {
let newTrans = existingTranslation;
if (config.replaceDefaults) {
newTrans = temp[existingKey] || existingTranslation;
if ((0, is_plural_key_1.default)(existingKey)) {
const noPluralKey = (0, key_without_plural_1.default)(existingKey);
if (temp[`${noPluralKey}_other`] && (!existingKey.endsWith('_one') && !existingKey.endsWith('_other'))) {
continue;
}
}
}
temp[existingKey] = newTrans;
}
}
}
if (config.sort) {
temp = (0, order_object_by_key_1.default)(temp);
}
if (!config.keepRemoved) {
let filtered = {};
for (const [key] of Object.entries(newTranslations[namespace] || {})) {
if ((0, is_plural_key_1.default)(key)) {
if (key.endsWith('_other')) {
const noPluralKey = (0, key_without_plural_1.default)(key);
for (const pluralKey of (0, get_plural_keys_1.default)(noPluralKey)) {
if (temp[pluralKey] !== undefined) {
filtered[pluralKey] = temp[pluralKey];
}
}
}
}
else {
filtered[key] = temp[key];
}
}
temp = filtered;
}
if (Object.keys(temp).length) {
mergedTranslations[locale][namespace] = temp;
}
}
}
return mergedTranslations;
};
exports.default = updateExistingTranslations;