DatePicker 组件来自 antd,显示的语言错误。

huangapple go评论67阅读模式
英文:

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 &#39;react&#39;;
import ReactDOM from &#39;react-dom&#39;;
import { ConfigProvider } from &#39;antd&#39;;
import ptBR from &#39;antd/es/locale/pt_BR&#39;
ReactDOM.render(
    &lt;ConfigProvider locale={ptBR} &gt;
        &lt;MYApp/&gt;
    &lt;/ConfigProvider&gt;
)

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 &#39;moment/locale/pt-br&#39;

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 &#39;antd&#39;
import ptBR from &#39;antd/es/locale/pt_BR&#39;
import moment from &#39;moment&#39;

// don&#39;t forget to import moment locale
import &#39;moment/locale/pt-br&#39;

// set moment locale too
moment.locale(&#39;pt-br&#39;)

ReactDOM.render(
  &lt;ConfigProvider locale={ptBR}&gt;
    &lt;MYApp/&gt;
  &lt;/ConfigProvider&gt;
)

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.

huangapple
  • 本文由 发表于 2023年6月15日 20:06:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定