How to change language in react-day-picker DayPickerInput component ?
In this article, we will learn about How to change language in the react-day-picker DayPickerInput Component. Initially, we will learn about react-day-picker.
The react-day-picker is designed to cover the most common needs for a date picker in web applications. The React DayPickerInput component is used to render an input field opening the DayPicker in an overlay.
import DayPickerInput from 'react-day-picker/DayPickerInput';
Now let’s build a react application to learn further.
Step 1: Create a react application using the following command.
npx create-react-app foldername
Step 2: Once it is done, change your directory to the newly created application using the following command.
Step 3: Install the module using the following command:
npm install react-day-picker
Project Structure: It will look like this.
Step 3: Run the server using the following command:
Implementation: In order to change the language of DatePickerInput, we need to use the dayPickerProp i.e locale. This is known as localization.
Localization: The react-day-picker can be localized into any language (English being the default). This can be done by:
- Using dayPickerProp (locale)
- Using moment.js
- Using Locale
Locale is used to set up language in DateInputPicker. It can be any language. We need to define some other props also based upon the language like weekdaysLong, weekdaysShort, months.
<DayPicker locale="*" weekdaysLong="*" weekdaysShort="*" months="*" />
Example 1: In this example, we will preDefine Months, weekdays for the language. Then we will render them accordingly.
Module Installation: Install this module using the following command:
npm install moment
Example 2: In this example, we will again have a dropdown menu for selecting Language, and the calendar will render accordingly. This time we don’t need to specifically mention weekdays and other props. These are imported using moment, for example, import ‘moment/locale/hi’ for Hindi language weeks, days, etc.