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"
I created a simple example in this CodeSandBox: https://codesandbox.io/s/5383o29jpx.
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.
For me works next:
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.
this.state = {
locale: "ru"
};
<DatePicker
locale={this.state.locale}
...
/>
Most helpful comment
Fixed by importing moment locale