react-intlayer
Version:
Easily internationalize i18n your React applications with type-safe multilingual content management.
1 lines • 4.07 kB
Source Map (JSON)
{"version":3,"sources":["../../../src/client/IntlayerProvider.tsx"],"sourcesContent":["'use client';\n\nimport configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/config/client';\n\nimport { localeResolver } from '@intlayer/core';\nimport { MessageKey, useCrossFrameState } from '@intlayer/editor-react';\nimport {\n type FC,\n type PropsWithChildren,\n createContext,\n useContext,\n} from 'react';\nimport { IntlayerEditorProvider } from '../editor/IntlayerEditorProvider';\nimport { localeCookie, setLocaleCookie } from './useLocaleCookie';\n\ntype IntlayerValue = {\n locale: LocalesValues;\n setLocale: (newLocale: LocalesValues) => void;\n disableEditor?: boolean;\n};\n\n/**\n * Context that store the current locale on the client side\n */\nexport const IntlayerClientContext = createContext<IntlayerValue>({\n locale: localeCookie ?? configuration?.internationalization?.defaultLocale,\n setLocale: () => null,\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}>;\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}) => {\n const { internationalization } = configuration ?? {};\n const { defaultLocale: defaultLocaleConfig, locales: availableLocales } =\n internationalization ?? {};\n\n const defaultLocale =\n localeProp ?? localeCookie ?? 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 setLocaleCookie(newLocale); // 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkFI;AAhFJ,mBAA0B;AAG1B,kBAA+B;AAC/B,0BAA+C;AAC/C,mBAKO;AACP,oCAAuC;AACvC,6BAA8C;AAWvC,MAAM,4BAAwB,4BAA6B;AAAA,EAChE,QAAQ,uCAAgB,aAAAA,SAAe,sBAAsB;AAAA,EAC7D,WAAW,MAAM;AAAA,EACjB,eAAe;AACjB,CAAC;AAKM,MAAM,qBAAqB,UAAM,yBAAW,qBAAqB;AAYjE,MAAM,0BAAqD,CAAC;AAAA,EACjE,QAAQ;AAAA,EACR,eAAe;AAAA,EACf;AAAA,EACA,WAAW;AAAA,EACX;AACF,MAAM;AACJ,QAAM,EAAE,qBAAqB,IAAI,aAAAA,WAAiB,CAAC;AACnD,QAAM,EAAE,eAAe,qBAAqB,SAAS,iBAAiB,IACpE,wBAAwB,CAAC;AAE3B,QAAM,gBACJ,cAAc,uCAAgB,qBAAqB;AAErD,QAAM,CAAC,eAAe,gBAAgB,QAAI;AAAA,IACxC,+BAAW;AAAA,IACX;AAAA,EACF;AAEA,QAAM,gBAAgB,CAAC,cAA6B;AAClD,QAAI,cAAc,SAAS,MAAM,UAAU,SAAS,EAAG;AAEvD,QAAI,CAAC,kBAAkB,IAAI,MAAM,EA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