react-intlayer
Version:
Easily internationalize i18n your React applications with type-safe multilingual content management.
1 lines • 2.22 kB
Source Map (JSON)
{"version":3,"sources":["../../../src/client/useLocale.ts"],"sourcesContent":["'use client';\n\nimport configuration from '@intlayer/config/built';\nimport type { LocalesValues } from '@intlayer/config/client';\nimport { useCallback, useContext } from 'react';\nimport { IntlayerClientContext } from './IntlayerProvider';\nimport { useLocaleCookie } from './useLocaleCookie';\n\ntype useLocaleProps = {\n onLocaleChange?: (locale: LocalesValues) => void;\n};\n\n/**\n * On the client side, hook to get the current locale and all related fields\n */\nexport const useLocale = ({ onLocaleChange }: useLocaleProps = {}) => {\n const { defaultLocale, locales: availableLocales } =\n configuration?.internationalization ?? {};\n\n const { locale, setLocale: setLocaleState } = useContext(\n IntlayerClientContext\n );\n const { setLocaleCookie } = useLocaleCookie();\n\n const setLocale = useCallback(\n (locale: LocalesValues) => {\n if (!availableLocales?.map(String).includes(locale)) {\n console.error(`Locale ${locale} is not available`);\n return;\n }\n\n setLocaleState(locale);\n setLocaleCookie(locale);\n onLocaleChange?.(locale);\n },\n [availableLocales, onLocaleChange, setLocaleCookie, setLocaleState]\n );\n\n return {\n locale, // Current locale\n defaultLocale, // Principal locale defined in config\n availableLocales, // List of the available locales defined in config\n setLocale, // Function to set the locale\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAA0B;AAE1B,mBAAwC;AACxC,8BAAsC;AACtC,6BAAgC;AASzB,MAAM,YAAY,CAAC,EAAE,eAAe,IAAoB,CAAC,MAAM;AACpE,QAAM,EAAE,eAAe,SAAS,iBAAiB,IAC/C,aAAAA,SAAe,wBAAwB,CAAC;AAE1C,QAAM,EAAE,QAAQ,WAAW,eAAe,QAAI;AAAA,IAC5C;AAAA,EACF;AACA,QAAM,EAAE,gBAAgB,QAAI,wCAAgB;AAE5C,QAAM,gBAAY;AAAA,IAChB,CAACC,YAA0B;AACzB,UAAI,CAAC,kBAAkB,IAAI,MAAM,EAAE,SAASA,OAAM,GAAG;AACnD,gBAAQ,MAAM,UAAUA,OAAM,mBAAmB;AACjD;AAAA,MACF;AAEA,qBAAeA,OAAM;AACrB,sBAAgBA,OAAM;AACtB,uBAAiBA,OAAM;AAAA,IACzB;AAAA,IACA,CAAC,kBAAkB,gBAAgB,iBAAiB,cAAc;AAAA,EACpE;AAEA,SAAO;AAAA,IACL;AAAA;AAAA,IACA;AAAA;AAAA,IACA;AAAA;AAAA,IACA;AAAA;AAAA,EACF;AACF;","names":["configuration","locale"]}