import * as React from 'react'; import type { Locale } from '.'; import type { LocaleContextProps } from './context'; import LocaleContext from './context'; import defaultLocaleData from './en_US'; export type LocaleComponentName = Exclude; const useLocale = ( componentName: C, defaultLocale?: Locale[C] | (() => Locale[C]), ): readonly [NonNullable, string] => { const fullLocale = React.useContext(LocaleContext); const getLocale = React.useMemo>(() => { const locale = defaultLocale || defaultLocaleData[componentName]; const localeFromContext = fullLocale?.[componentName] ?? {}; return { ...(typeof locale === 'function' ? locale() : locale), ...(localeFromContext || {}), }; }, [componentName, defaultLocale, fullLocale]); const getLocaleCode = React.useMemo(() => { const localeCode = fullLocale?.locale; // Had use LocaleProvide but didn't set locale if (fullLocale?.exist && !localeCode) { return defaultLocaleData.locale; } return localeCode!; }, [fullLocale]); return [getLocale, getLocaleCode] as const; }; export default useLocale;