react-intlayer
Version:
Easily internationalize i18n your React applications with type-safe multilingual content management.
1 lines • 2.34 kB
Source Map (JSON)
{"version":3,"file":"useLocale.mjs","names":["locale"],"sources":["../../../src/client/useLocale.ts"],"sourcesContent":["'use client';\n\nimport configuration from '@intlayer/config/built';\nimport type { DeclaredLocales, LocalesValues } from '@intlayer/types';\nimport { useCallback, useContext } from 'react';\nimport { IntlayerClientContext } from './IntlayerProvider';\nimport { setLocaleInStorage } from './useLocaleStorage';\n\ntype UseLocaleProps = {\n isCookieEnabled?: boolean;\n onLocaleChange?: (locale: LocalesValues) => void;\n};\n\ntype UseLocaleResult = {\n locale: DeclaredLocales;\n defaultLocale: DeclaredLocales;\n availableLocales: DeclaredLocales[];\n setLocale: (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 = ({\n isCookieEnabled,\n onLocaleChange,\n}: UseLocaleProps = {}): UseLocaleResult => {\n const { defaultLocale, locales: availableLocales } =\n configuration?.internationalization ?? {};\n\n const {\n locale,\n setLocale: setLocaleState,\n isCookieEnabled: isCookieEnabledContext,\n } = useContext(IntlayerClientContext);\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 setLocaleInStorage(\n locale,\n isCookieEnabled ?? isCookieEnabledContext ?? true\n );\n onLocaleChange?.(locale);\n },\n [availableLocales, onLocaleChange, 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 } as UseLocaleResult;\n};\n"],"mappings":";;;;;;;;;;;AAuBA,MAAa,aAAa,EACxB,iBACA,mBACkB,EAAE,KAAsB;CAC1C,MAAM,EAAE,eAAe,SAAS,qBAC9B,eAAe,wBAAwB,EAAE;CAE3C,MAAM,EACJ,QACA,WAAW,gBACX,iBAAiB,2BACf,WAAW,sBAAsB;AAmBrC,QAAO;EACL;EACA;EACA;EACA,WArBgB,aACf,aAA0B;AACzB,OAAI,CAAC,kBAAkB,IAAI,OAAO,CAAC,SAASA,SAAO,EAAE;AACnD,YAAQ,MAAM,UAAUA,SAAO,mBAAmB;AAClD;;AAGF,kBAAeA,SAAO;AACtB,sBACEA,UACA,mBAAmB,0BAA0B,KAC9C;AACD,oBAAiBA,SAAO;KAE1B;GAAC;GAAkB;GAAgB;GAAe,CACnD;EAOA"}