react-intlayer
Version:
Easily internationalize i18n your React applications with type-safe multilingual content management.
62 lines • 2.16 kB
JavaScript
"use client";
import { jsx } from "react/jsx-runtime";
import configuration from "@intlayer/config/built";
import { localeResolver } from "@intlayer/core";
import { MessageKey, useCrossFrameState } from "@intlayer/editor-react";
import {
createContext,
useContext
} from "react";
import { IntlayerEditorProvider } from "../editor/IntlayerEditorProvider.mjs";
import { localeCookie, setLocaleCookie } from "./useLocaleCookie.mjs";
const IntlayerClientContext = createContext({
locale: localeCookie ?? configuration?.internationalization?.defaultLocale,
setLocale: () => null,
disableEditor: false
});
const useIntlayerContext = () => useContext(IntlayerClientContext);
const IntlayerProviderContent = ({
locale: localeProp,
defaultLocale: defaultLocaleProp,
children,
setLocale: setLocaleProp,
disableEditor
}) => {
const { internationalization } = configuration ?? {};
const { defaultLocale: defaultLocaleConfig, locales: availableLocales } = internationalization ?? {};
const defaultLocale = localeProp ?? localeCookie ?? defaultLocaleProp ?? defaultLocaleConfig;
const [currentLocale, setCurrentLocale] = useCrossFrameState(
MessageKey.INTLAYER_CURRENT_LOCALE,
defaultLocale
);
const setLocaleBase = (newLocale) => {
if (currentLocale.toString() === newLocale.toString()) return;
if (!availableLocales?.map(String).includes(newLocale)) {
console.error(`Locale ${newLocale} is not available`);
return;
}
setCurrentLocale(newLocale);
setLocaleCookie(newLocale);
};
const setLocale = setLocaleProp ?? setLocaleBase;
const resolvedLocale = localeResolver(localeProp ?? currentLocale);
return /* @__PURE__ */ jsx(
IntlayerClientContext.Provider,
{
value: {
locale: resolvedLocale,
setLocale,
disableEditor
},
children
}
);
};
const IntlayerProvider = (props) => /* @__PURE__ */ jsx(IntlayerEditorProvider, { children: /* @__PURE__ */ jsx(IntlayerProviderContent, { ...props }) });
export {
IntlayerClientContext,
IntlayerProvider,
IntlayerProviderContent,
useIntlayerContext
};
//# sourceMappingURL=IntlayerProvider.mjs.map