Files
v10/dist/dev/i18n/get-provider-root-props.js

40 lines
1.9 KiB
JavaScript

//#region src/i18n/get-provider-root-props.ts
function isRecord(value) {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
function mergeTranslations(parent, child) {
return {
...parent,
...child,
...parent && child ? Object.fromEntries(Object.keys(parent).filter((key) => isRecord(parent[key]) && isRecord(child[key])).map((key) => {
const parentValue = parent[key];
const childValue = child[key];
return [key, isRecord(parentValue) && isRecord(childValue) ? {
...parentValue,
...childValue
} : childValue];
})) : {}
};
}
function getProviderRootProps(props, parent, parentAddLocaleRoot) {
const hasOverrides = props.locale !== void 0 || props.translations !== void 0 || props.onActiveLocaleChange !== void 0;
const langRootOnly = props.langRootRef !== void 0 && !hasOverrides;
if (parent && !hasOverrides && (!langRootOnly || parent.localeFromProp)) return;
const inheritedLocale = props.locale ?? (props.langRootRef === void 0 ? parent?.locale : void 0);
const parentLocale = props.langRootRef !== void 0 ? parent?.locale : void 0;
const inheritedTranslations = props.translations !== void 0 && parent?.translations !== void 0 ? mergeTranslations(parent.translations, props.translations) : props.translations ?? (langRootOnly ? parent?.translations : void 0);
const onActiveLocaleChange = props.onActiveLocaleChange ?? parent?.onActiveLocaleChange;
return {
...props,
...inheritedLocale !== void 0 ? { locale: inheritedLocale } : {},
localeFromProp: props.locale !== void 0,
...parentLocale !== void 0 ? { parentLocale } : {},
...inheritedTranslations !== void 0 ? { translations: inheritedTranslations } : {},
...onActiveLocaleChange !== void 0 ? { onActiveLocaleChange } : {},
...parentAddLocaleRoot !== void 0 ? { parentAddLocaleRoot } : {}
};
}
//#endregion
export { getProviderRootProps };
//# sourceMappingURL=get-provider-root-props.js.map