Change locale in react-datepicker

孤者浪人 提交于 2020-11-30 10:29:10

问题


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

registerLocale 

and

setDefaultLocale

from 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.


回答1:


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.

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

so (after installing the correct versions)

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

and use it

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

Working demo at https://codesandbox.io/s/7j8z7kvy06




回答2:


import React, { Component } from 'react';
import DatePicker, { registerLocale } from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import ja from "date-fns/locale/ja";

registerLocale("ja", ja);

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      date: new Date()
    }
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(date) {
    this.setState({
      date
    });
  }

  render() {
    return (
      <div className="App">
        <body>
          <DatePicker
            dateFormat="yyyy/MM/dd"
            selected={this.state.date}
            onChange={this.handleChange}
            locale='ja'
          />
        </body>
      </div>
    );
  }
}

export default App;

I could get the result you wanted. And I tried to make it with moment library but it didn't work on my code. sorry




回答3:


For those who don't want to depend on date-fns module you can define your own localization.

const months = ['Ocak', 'Şubat', 'Mart', 'Nisan', 'Mayıs', 'Haziran', 'Temmuz', 'Ağustos', 'Eylül', 'Ekim', 'Kasım', 'Aralık']
const days = ['Pt', 'Sa', 'Ça', 'Pe', 'Cu', 'Ct', 'Pz']

const locale = {
  localize: {
    month: n => months[n],
    day: n => days[n]
  },
  formatLong: {}
}

<DatePicker locale={locale} />



回答4:


You don't even need to use registerLocale, just use import variable name ja without quotes :

<DatePicker
  dateFormat="yyyy/MM/dd"
  selected={this.state.date}
  onChange={this.handleChange}
  locale=ja
/>

You can also set the default locale for all date picker fields with setDefaultLocale :

constructor (props) {
    registerLocale("ja", ja);
    setDefaultLocale("ja");
}

Hope this helps.




回答5:


In case you want to use a locale, that is not supported by date-fns (and those are quite few), you can do a shim.

const monthsBG = ['Януари', 'Февруари', 'Март', 'Април', 'Май', 'Юни', 'Юли', 'Август', 'Септември', 'Октомври', 'Ноември', 'Декември'];
const daysBG = ['нед', 'пон', 'вт', 'ср', 'четв', 'пет', 'съб'];

registerLocale('bg', {
  localize: {
    month: n => monthsBG[n],
    day: n => daysBG[n]
  }, 
  formatLong:{} 
});

and then you can use this locale as any other

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


来源:https://stackoverflow.com/questions/54399084/change-locale-in-react-datepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!