Search code examples
javascriptreactjsreact-dates

How to internationalize React-Dates Month and weekday names?


I'm working with react-dates and I'm trying to figure out how to internationalize the month and weekday names. I already figured out how to do this to the input placeholder text but I can't find how to do this.

the current props that I give to react-dates DateRangePicker look like this:

<DateRangePicker
        startDate={this.state.startDate} // momentPropTypes.momentObj or null,
        endDate={this.state.endDate} // momentPropTypes.momentObj or null,
        onDatesChange={({ startDate, endDate }) => {
            this.setState({ startDate, endDate });
            this.props.onDateChange(this.props.name, startDate, endDate);
          }
        } // PropTypes.func.isRequired,
        focusedInput={this.state.focusedInput} // PropTypes.oneOf([START_DATE, END_DATE]) or null,
        onFocusChange={focusedInput => this.setState({ focusedInput })} // PropTypes.func.isRequired,
        numberOfMonths={1}
        firstDayOfWeek={1}
        minimumNights={0}
        isOutsideRange={(x) => moment().add(1, 'days') < x }
        displayFormat="DD-MM-YYYY"
        showClearDates={true}
        startDatePlaceholderText={this.props.intl.formatMessage({id: 'DATE_PICKER_START_DATE'})}
        endDatePlaceholderText={this.props.intl.formatMessage({id: 'DATE_PICKER_END_DATE'})}
        hideKeyboardShortcutsPanel= {true}
      />

I'm using react-intl for the internationalization.

This is what the calendar looks like: image of the calendar

Basically what I want to change is the month name November and the weekday names Mo Tu We Th Fr Sa Su

The desired outcome is to translate them to Finnish -> Marraskuu and Ma Ti Ke To Pe La Su


Solution

  • I managed to solve this by importing moment with finnish and some additional props for the datepicker:

    import React, { Component } from "react";
    import {DateRangePicker} from 'react-dates';
    import { injectIntl, intlShape } from "react-intl";
    import moment from 'moment'
    import 'moment/locale/fi';
    import 'react-dates/initialize';
    import 'react-dates/lib/css/_datepicker.css';
    
    .
    .
    .
    
    <DateRangePicker
        .
        .
        .
        renderMonthText={month => {
          month.locale(this.props.intl.locale)
          return(
            moment(month).format('MMMM YYYY')
          )
        }}
        renderDayContents={day => {
          day.local(this.props.intl.local)
          return(
            moment(day).format('D')
          )
        }}
      />