diff --git a/components/calendar/generateCalendar.tsx b/components/calendar/generateCalendar.tsx index c2bba1abaa..1f0834398b 100644 --- a/components/calendar/generateCalendar.tsx +++ b/components/calendar/generateCalendar.tsx @@ -236,7 +236,7 @@ function generateCalendar(generateConfig: GenerateConfig) { [monthFullCellRender, monthCellRender], ); - const contextLocale = useLocale('Calendar', getDefaultLocale); + const [contextLocale] = useLocale('Calendar', getDefaultLocale); return wrapSSR(
(generateConfig: GenerateCo blur: () => innerRef.current?.blur(), })); - const contextLocale = useLocale('Calendar', enUS); + const [contextLocale] = useLocale('Calendar', enUS); const locale = { ...contextLocale, ...props.locale! }; diff --git a/components/date-picker/generatePicker/generateSinglePicker.tsx b/components/date-picker/generatePicker/generateSinglePicker.tsx index 68b5bff9e9..b3a8ffb0d7 100644 --- a/components/date-picker/generatePicker/generateSinglePicker.tsx +++ b/components/date-picker/generatePicker/generateSinglePicker.tsx @@ -122,7 +122,7 @@ export default function generatePicker(generateConfig: GenerateConfig< ); - const contextLocale = useLocale('DatePicker', enUS); + const [contextLocale] = useLocale('DatePicker', enUS); const locale = { ...contextLocale, ...props.locale! }; diff --git a/components/empty/index.tsx b/components/empty/index.tsx index 41d3a376d6..f2a5c1f32f 100644 --- a/components/empty/index.tsx +++ b/components/empty/index.tsx @@ -46,7 +46,7 @@ const Empty: CompoundedComponent = ({ const prefixCls = getPrefixCls('empty', customizePrefixCls); const [wrapSSR, hashId] = useStyle(prefixCls); - const locale = useLocale('Empty'); + const [locale] = useLocale('Empty'); const des = typeof description !== 'undefined' ? description : locale?.description; const alt = typeof des === 'string' ? des : 'empty'; diff --git a/components/form/FormItemLabel.tsx b/components/form/FormItemLabel.tsx index c0d696298d..1ec42e1767 100644 --- a/components/form/FormItemLabel.tsx +++ b/components/form/FormItemLabel.tsx @@ -53,7 +53,7 @@ const FormItemLabel: React.FC { - const formLocale = useLocale('Form'); + const [formLocale] = useLocale('Form'); const { vertical, diff --git a/components/locale/useLocale.ts b/components/locale/useLocale.ts index c3b5b01ddd..69be9bc409 100644 --- a/components/locale/useLocale.ts +++ b/components/locale/useLocale.ts @@ -9,16 +9,28 @@ export type LocaleComponentName = Exclude; const useLocale = ( componentName: C, defaultLocale?: Locale[C] | (() => Locale[C]), -): Locale[C] => { - const antLocale = React.useContext(LocaleContext); - return React.useMemo>(() => { +): readonly [NonNullable, string] => { + const fullLocale = React.useContext(LocaleContext); + + const getLocale = React.useMemo>(() => { const locale = defaultLocale || defaultLocaleData[componentName]; - const localeFromContext = antLocale?.[componentName] ?? {}; + const localeFromContext = fullLocale?.[componentName] ?? {}; return { ...(typeof locale === 'function' ? locale() : locale), ...(localeFromContext || {}), }; - }, [componentName, defaultLocale, antLocale]); + }, [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; diff --git a/components/modal/ConfirmDialog.tsx b/components/modal/ConfirmDialog.tsx index b5580743e4..da21408408 100644 --- a/components/modal/ConfirmDialog.tsx +++ b/components/modal/ConfirmDialog.tsx @@ -81,7 +81,7 @@ export function ConfirmContent( const autoFocusButton = props.autoFocusButton === null ? false : props.autoFocusButton || 'ok'; - const locale = useLocale('Modal'); + const [locale] = useLocale('Modal'); const mergedLocale = staticLocale || locale; diff --git a/components/modal/PurePanel.tsx b/components/modal/PurePanel.tsx index 4115bcd3a2..0ecefa96ae 100644 --- a/components/modal/PurePanel.tsx +++ b/components/modal/PurePanel.tsx @@ -58,7 +58,7 @@ export const Footer: React.FC< footer, } = props; - const locale = useLocale('Modal', getConfirmLocale()); + const [locale] = useLocale('Modal', getConfirmLocale()); return footer === undefined ? ( <> diff --git a/components/modal/useModal/HookModal.tsx b/components/modal/useModal/HookModal.tsx index c1abadd5e6..f2528b58d6 100644 --- a/components/modal/useModal/HookModal.tsx +++ b/components/modal/useModal/HookModal.tsx @@ -46,7 +46,7 @@ const HookModal: React.ForwardRefRenderFunction = const mergedOkCancel = innerConfig.okCancel ?? innerConfig.type === 'confirm'; - const contextLocale = useLocale('Modal', defaultLocale.Modal); + const [contextLocale] = useLocale('Modal', defaultLocale.Modal); return ( = ({ return { prevIcon, nextIcon, jumpPrevIcon, jumpNextIcon }; }; - const contextLocale = useLocale('Pagination', enUS); + const [contextLocale] = useLocale('Pagination', enUS); const locale = { ...contextLocale, ...customLocale }; diff --git a/components/popconfirm/PurePanel.tsx b/components/popconfirm/PurePanel.tsx index 62b40642f1..8b006d65bd 100644 --- a/components/popconfirm/PurePanel.tsx +++ b/components/popconfirm/PurePanel.tsx @@ -56,7 +56,7 @@ export const Overlay: React.FC = (props) => { const { getPrefixCls } = React.useContext(ConfigContext); - const contextLocale = useLocale('Popconfirm', defaultLocale.Popconfirm); + const [contextLocale] = useLocale('Popconfirm', defaultLocale.Popconfirm); return (
diff --git a/components/qrcode/index.tsx b/components/qrcode/index.tsx index 62510ab82f..9970887164 100644 --- a/components/qrcode/index.tsx +++ b/components/qrcode/index.tsx @@ -53,7 +53,7 @@ const QRCode: React.FC = (props) => { }; }, [errorLevel, color, icon, iconSize, size, value]); - const locale = useLocale('QRCode'); + const [locale] = useLocale('QRCode'); if (!value) { if (process.env.NODE_ENV !== 'production') { diff --git a/components/tour/panelRender.tsx b/components/tour/panelRender.tsx index 7443ee2b4e..0176a58c3b 100644 --- a/components/tour/panelRender.tsx +++ b/components/tour/panelRender.tsx @@ -92,7 +92,7 @@ const TourPanel: React.FC = ({ stepProps, current, type, indicat ghost: mergedType === 'primary', }; - const contextLocale = useLocale('Tour', defaultLocale.Tour); + const [contextLocale] = useLocale('Tour', defaultLocale.Tour); return (
(props: ListItemProps = { className, title }; diff --git a/components/transfer/index.tsx b/components/transfer/index.tsx index 13adda3bec..6db4e74f51 100644 --- a/components/transfer/index.tsx +++ b/components/transfer/index.tsx @@ -367,7 +367,7 @@ const Transfer = ( hashId, ); - const contextLocale = useLocale('Transfer', defaultLocale.Transfer); + const [contextLocale] = useLocale('Transfer', defaultLocale.Transfer); const listLocale = getLocale(contextLocale!); diff --git a/components/typography/Base/index.tsx b/components/typography/Base/index.tsx index e2ee39bf14..9c712fc529 100644 --- a/components/typography/Base/index.tsx +++ b/components/typography/Base/index.tsx @@ -135,7 +135,7 @@ const Base = React.forwardRef((props, ref) => { ...restProps } = props; const { getPrefixCls, direction } = React.useContext(ConfigContext); - const textLocale = useLocale('Text'); + const [textLocale] = useLocale('Text'); const typographyRef = React.useRef(null); const editIconRef = React.useRef(null); diff --git a/components/upload/Upload.tsx b/components/upload/Upload.tsx index 6672d0255c..0317c9fdb7 100644 --- a/components/upload/Upload.tsx +++ b/components/upload/Upload.tsx @@ -339,7 +339,7 @@ const InternalUpload: React.ForwardRefRenderFunction = (pr const [wrapSSR, hashId] = useStyle(prefixCls); - const contextLocale = useLocale('Upload', defaultLocale.Upload); + const [contextLocale] = useLocale('Upload', defaultLocale.Upload); const { showRemoveIcon,