UNPKG

react-intlayer

Version:

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

1 lines 4.03 kB
{"version":3,"file":"IntlayerProvider.mjs","names":["IntlayerProviderContent: FC<IntlayerProviderProps>","IntlayerProvider: FC<IntlayerProviderProps>"],"sources":["../../../src/client/IntlayerProvider.tsx"],"sourcesContent":["'use client';\n\nimport configuration from '@intlayer/config/built';\nimport { localeResolver } from '@intlayer/core';\nimport { MessageKey, useCrossFrameState } from '@intlayer/editor-react';\nimport type { LocalesValues } from '@intlayer/types';\nimport {\n createContext,\n type FC,\n type PropsWithChildren,\n useContext,\n} from 'react';\nimport { IntlayerEditorProvider } from '../editor/IntlayerEditorProvider';\nimport { localeInStorage, setLocaleInStorage } from './useLocaleStorage';\n\ntype IntlayerValue = {\n locale: LocalesValues;\n setLocale: (newLocale: LocalesValues) => void;\n disableEditor?: boolean;\n isCookieEnabled?: boolean;\n};\n\n/**\n * Context that store the current locale on the client side\n */\nexport const IntlayerClientContext = createContext<IntlayerValue>({\n locale: localeInStorage ?? configuration?.internationalization?.defaultLocale,\n setLocale: () => null,\n isCookieEnabled: true,\n disableEditor: false,\n});\n\n/**\n * Hook that provides the current locale\n */\nexport const useIntlayerContext = () => useContext(IntlayerClientContext);\n\nexport type IntlayerProviderProps = PropsWithChildren<{\n locale?: LocalesValues;\n defaultLocale?: LocalesValues;\n setLocale?: (locale: LocalesValues) => void;\n disableEditor?: boolean;\n isCookieEnabled?: boolean;\n}>;\n\n/**\n * Provider that store the current locale on the client side\n */\nexport const IntlayerProviderContent: FC<IntlayerProviderProps> = ({\n locale: localeProp,\n defaultLocale: defaultLocaleProp,\n children,\n setLocale: setLocaleProp,\n disableEditor,\n isCookieEnabled,\n}) => {\n const { internationalization } = configuration ?? {};\n const { defaultLocale: defaultLocaleConfig, locales: availableLocales } =\n internationalization ?? {};\n\n const defaultLocale =\n localeProp ?? localeInStorage ?? defaultLocaleProp ?? defaultLocaleConfig;\n\n const [currentLocale, setCurrentLocale] = useCrossFrameState(\n MessageKey.INTLAYER_CURRENT_LOCALE,\n defaultLocale\n );\n\n const setLocaleBase = (newLocale: LocalesValues) => {\n if (currentLocale.toString() === newLocale.toString()) return;\n\n if (!availableLocales?.map(String).includes(newLocale)) {\n console.error(`Locale ${newLocale} is not available`);\n return;\n }\n\n setCurrentLocale(newLocale); // Update state\n setLocaleInStorage(newLocale, isCookieEnabled); // Optionally set cookie for persistence\n };\n\n const setLocale = setLocaleProp ?? setLocaleBase;\n\n const resolvedLocale = localeResolver(localeProp ?? currentLocale);\n\n return (\n <IntlayerClientContext.Provider\n value={{\n locale: resolvedLocale,\n setLocale,\n disableEditor,\n }}\n >\n {children}\n </IntlayerClientContext.Provider>\n );\n};\n\nexport const IntlayerProvider: FC<IntlayerProviderProps> = (props) => (\n <IntlayerEditorProvider>\n <IntlayerProviderContent {...props} />\n </IntlayerEditorProvider>\n);\n"],"mappings":";;;;;;;;;;;;;;AAyBA,MAAa,wBAAwB,cAA6B;CAChE,QAAQ,mBAAmB,eAAe,sBAAsB;CAChE,iBAAiB;CACjB,iBAAiB;CACjB,eAAe;CAChB,CAAC;;;;AAKF,MAAa,2BAA2B,WAAW,sBAAsB;;;;AAazE,MAAaA,2BAAsD,EACjE,QAAQ,YACR,eAAe,mBACf,UACA,WAAW,eACX,eACA,sBACI;CACJ,MAAM,EAAE,yBAAyB,iBAAiB,EAAE;CACpD,MAAM,EAAE,eAAe,qBAAqB,SAAS,qBACnD,wBAAwB,EAAE;CAE5B,MAAM,gBACJ,cAAc,mBAAmB,qBAAqB;CAExD,MAAM,CAAC,eAAe,oBAAoB,mBACxC,WAAW,yBACX,cACD;CAED,MAAM,iBAAiB,cAA6B;AAClD,MAAI,cAAc,UAAU,KAAK,UAAU,UAAU,CAAE;AAEvD,MAAI,CAAC,kBAAkB,IAAI,OAAO,CAAC,SAAS,UAAU,EAAE;AACtD,WAAQ,MAAM,UAAU,UAAU,mBAAmB;AACrD;;AAGF,mBAAiB,UAAU;AAC3B,uBAAmB,WAAW,gBAAgB;;CAGhD,MAAM,YAAY,iBAAiB;CAEnC,MAAM,iBAAiB,eAAe,cAAc,cAAc;AAElE,QACE,oBAAC,sBAAsB;EACrB,OAAO;GACL,QAAQ;GACR;GACA;GACD;EAEA;GAC8B;;AAIrC,MAAaC,oBAA+C,UAC1D,oBAAC,oCACC,oBAAC,2BAAwB,GAAI,QAAS,GACf"}