英文:
DatePicker component from antd displaying wrong language
问题
我正在使用 antd 版本 3.26.20,并且目前正在在我的入口文件中使用 ConfigProvider 组件来设置默认的区域设置,但是并没有应用到所有的组件上。
这是我的做法:
import React from 'react';
import ReactDOM from 'react-dom';
import { ConfigProvider } from 'antd';
import ptBR from 'antd/es/locale/pt_BR';
ReactDOM.render(
<ConfigProvider locale={ptBR}>
<MYApp/>
</ConfigProvider>
)
根据 antd 文档 (https://3x.ant.design/components/date-picker/),组件的默认语言应该被 ConfigProvider 中定义的区域设置覆盖,对于我的应用中的某些 DatePicker 组件,确实出现了这种行为,但对于其他一些组件,我需要在开头导入以下包:
import 'moment/locale/pt-br'
对于那些正常工作的 DatePicker 组件,我并不需要从 moment 库导入任何包。
有人知道为什么会出现这种情况吗?
谢谢!
英文:
I'm using antd at 3.26.20 and currently setting the default locale in my entry file using the ConfigProvider component, but the locale is not being applied to all components.
Here is what I'm doing:
import React from 'react';
import ReactDOM from 'react-dom';
import { ConfigProvider } from 'antd';
import ptBR from 'antd/es/locale/pt_BR'
ReactDOM.render(
<ConfigProvider locale={ptBR} >
<MYApp/>
</ConfigProvider>
)
According to the antd documentation (https://3x.ant.design/components/date-picker/), the component's default language should be overridden by the locale defined in ConfigProvider and for some DatePicker components in my app this behavior is being seen but for others, I need to import the following package in the beginning:
import 'moment/locale/pt-br'
For those DatePicker components that are working properly, I didn't have to import any package from moment lib.
Does anyone know why this is happening?
Thank you!
答案1
得分: 1
请查看ConfigProvider文档:您需要在根组件中设置moment
的区域设置,并检查是否有两个不同版本的它。
在您的应用根文件中执行以下操作:
// 入口文件
import { ConfigProvider } from 'antd';
import ptBR from 'antd/es/locale/pt_BR';
import moment from 'moment';
// 不要忘记导入moment的区域设置
import 'moment/locale/pt-br';
// 也要设置moment的区域设置
moment.locale('pt-br');
ReactDOM.render(
<ConfigProvider locale={ptBR}>
<MYApp/>
</ConfigProvider>
);
所有区域设置/语言配置都必须在您的根组件中完成。如果您的应用支持动态语言切换,您可以查看此示例。
英文:
Take a look into ConfigProvider docs: You need to set moment
locale in your root component and also check if you don't have two different versions of it.
In your app root file do the following:
// entry file file
import {ConfigProvider} from 'antd'
import ptBR from 'antd/es/locale/pt_BR'
import moment from 'moment'
// don't forget to import moment locale
import 'moment/locale/pt-br'
// set moment locale too
moment.locale('pt-br')
ReactDOM.render(
<ConfigProvider locale={ptBR}>
<MYApp/>
</ConfigProvider>
)
All locale/language configs must be done in your root component. If your app have dynamic language switch you can have a look into this example.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论