@revenuecat/purchases-ui-js
Version:
Web components for Paywalls. Powered by RevenueCat
27 lines (26 loc) • 824 B
JavaScript
import { getContext, setContext } from "svelte";
const key = Symbol("localization");
export function setLocalizationContext(props) {
const getLocalizedString = (id) => {
if (!id) {
return "";
}
const { selectedLocale, defaultLocale, localizations } = props();
const locale = selectedLocale ?? defaultLocale;
const selected = localizations[locale]?.[id];
const fallback = localizations[defaultLocale]?.[id];
return selected ?? fallback;
};
const context = {
getLocalizedString,
};
setContext(key, context);
return context;
}
export function getLocalizationContext() {
const context = getContext(key);
if (context === undefined) {
throw new Error("Localization context not found");
}
return context;
}