mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-24 11:10:01 +08:00
502dac12aa
* docs: fix code * feat: lint * feat: prettier * feat: test * feat: review * feat: format html * feat: format html
38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
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<keyof Locale, 'locale'>;
|
|
|
|
const useLocale = <C extends LocaleComponentName = LocaleComponentName>(
|
|
componentName: C,
|
|
defaultLocale?: Locale[C] | (() => Locale[C]),
|
|
): readonly [NonNullable<Locale[C]>, string] => {
|
|
const fullLocale = React.useContext<LocaleContextProps | undefined>(LocaleContext);
|
|
|
|
const getLocale = React.useMemo<NonNullable<Locale[C]>>(() => {
|
|
const locale = defaultLocale || defaultLocaleData[componentName];
|
|
const localeFromContext = fullLocale?.[componentName] ?? {};
|
|
return {
|
|
...(typeof locale === 'function' ? locale() : locale),
|
|
...(localeFromContext || {}),
|
|
};
|
|
}, [componentName, defaultLocale, fullLocale]);
|
|
|
|
const getLocaleCode = React.useMemo<string>(() => {
|
|
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;
|