React-datepicker: Locale prop not used

Created on 15 Mar 2018  路  7Comments  路  Source: Hacker0x01/react-datepicker

Expected behavior

I would like to use locale prop in my DatePicker, just like in the example:

        <DatePicker
          selected={this.state.startDate}
          onChange={this.handleChange}
          locale="fr"
          todayButton={"Aujourd'hui"}
        />

But, it seems to not work because it is still in English.
My dependencies are:

    "moment": "2.21.0",
    "react": "16.2.0",
    "react-dom": "16.2.0",
    "react-datepicker": "1.2.2"

Steps to reproduce

I created a simple example in this CodeSandBox: https://codesandbox.io/s/5383o29jpx.

Most helpful comment

Fixed by importing moment locale

import moment from 'moment'
import 'moment/locale/ru'
moment.locale('ru')

All 7 comments

Same with ru locale. Using as property or moment global - there is no difference

Fixed by importing moment locale

import moment from 'moment'
import 'moment/locale/ru'
moment.locale('ru')

Yep that's the way to do it 馃憤

How can you do this dynamically? I need something like import moment/src/locale/${Locale2} so I can add the appropriate language depending on the user, but javascript doesn't allow you to do this, our app has more than 20 different languages.

Doing this locale=Locale2 on the component doesn't work either.

@Rolando-Barbella I think you should import all 20 different languages yourself.

Now wait...
I simply set moment locale
moment.locale('fr')
and in the datepicker

<Datepicker
    locale={'fr'} 
        ... 
/>

and it just works. No need to import the locale resources.

It was a big problem for me. Please, add to Docs some words about localization with moment.

For me works next:

  1. Import all necessary langs (or just one, if it static):
import 'moment/locale/ru';
import 'moment/locale/fr';
import 'moment/locale/en-gb';
...

Note: be careful, because last imported locale stay as default, if you don't set locale for DatePicker or if you set locale that has not been imported.

  1. Add locale prop for DatePicker. It can be value from state or props or other variable, which you use to manage locale (or just string, if it static).
this.state = {
     locale: "ru"
};

<DatePicker locale={this.state.locale} ... />

Was this page helpful?
0 / 5 - 0 ratings

Related issues

evolve2k picture evolve2k  路  3Comments

formigone picture formigone  路  3Comments

arturictus picture arturictus  路  3Comments

ericreis picture ericreis  路  3Comments

dhruvparmar372 picture dhruvparmar372  路  3Comments