UNPKG

react-intlayer

Version:

Easily internationalize i18n your React applications with type-safe multilingual content management.

38 lines (35 loc) 1.19 kB
'use client'; import { setLocaleInStorage } from "./useLocaleStorage.mjs"; import { IntlayerClientContext } from "./IntlayerProvider.mjs"; import { useCallback, useContext } from "react"; import configuration from "@intlayer/config/built"; //#region src/client/useLocale.ts /** * On the client side, hook to get the current locale and all related fields */ const useLocale = ({ isCookieEnabled, onLocaleChange } = {}) => { const { defaultLocale, locales: availableLocales } = configuration?.internationalization ?? {}; const { locale, setLocale: setLocaleState, isCookieEnabled: isCookieEnabledContext } = useContext(IntlayerClientContext); return { locale, defaultLocale, availableLocales, setLocale: useCallback((locale$1) => { if (!availableLocales?.map(String).includes(locale$1)) { console.error(`Locale ${locale$1} is not available`); return; } setLocaleState(locale$1); setLocaleInStorage(locale$1, isCookieEnabled ?? isCookieEnabledContext ?? true); onLocaleChange?.(locale$1); }, [ availableLocales, onLocaleChange, setLocaleState, isCookieEnabled ]) }; }; //#endregion export { useLocale }; //# sourceMappingURL=useLocale.mjs.map