在 react-datepicker 中更改语言环境 [英] Change locale in react-datepicker

查看:32
本文介绍了在 react-datepicker 中更改语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 react-datepicker NPM 包,
我试图按照文档进行操作,但无法导入

I am using react-datepicker NPM package,
I tried to follow documentation but I was unable to import

registerLocale 

setDefaultLocale

来自 react-datepicker

你知道我哪里出错了吗?

Do you see where I make mistake?

 import DatePicker from 'react-datepicker';



...
    <DatePicker
            { ...this.props }
            dateFormat={ this.dateFormat }
            ref={ (node) => { this.ref = node; } }
            onClickOutside={ this.clickOutside }
          />
...

这是我要导入语言环境的代码.

this is code where I want to import locale.

推荐答案

首先确保您使用的是最新版本的插件 (2.0.0).然后您还需要安装 date-fns 模块,但目前 react-datepicker 正在使用 2.0.0-alpha.23 版本.

First of all make sure you are using the latest version of the plugin (2.0.0). Then you need to also install the date-fns module, but for the moment the react-datepicker is working with the 2.0.0-alpha.23 version.

然后你需要导入并注册你想要的语言环境,最后将locale属性添加到DatePicker

Then you need to import and register the locale you want and finally add the locale property to the DatePicker

so(安装正确版本后)

import DatePicker, { registerLocale } from "react-datepicker";
import el from "date-fns/locale/el"; // the locale you want
registerLocale("el", el); // register it with the name you want

并使用它

<DatePicker 
    locale="el"
    ...
/>

https://codesandbox.io/s/7j8z7kvy06

这篇关于在 react-datepicker 中更改语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆