Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
Multi Language React DatePicker

I'm trying to translate the component React DatePicker. But, I'm getting an error when trying to do this.

My code so far:

import DatePicker, { registerLocale } from 'react-datepicker';

const MONTHSDATE = ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'];
const DAYSDATE = ['Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado'];

registerLocale('ptBR', {
        localize: {
            month: n => MONTHSDATE[n],
            day: n => DAYSDATE[n]
        },
        formatLong: {}
    });
    

<DatePicker
    selected={realizedDateFrom}
    onChange={(date) => setRealizedDateFrom(date)}
    placeholderText={translate.t("activity_report_realized_from")}
    className="MuiInputBase-root MuiFilledInput-root activity-report-datepicker"
    dateFormat={'dd/MM/yyyy'}
    showMonthDropdown
    showYearDropdown
    formatWeekDay={nameOfDay => nameOfDay.substr(0, 3)}
    locale={translate.t("datapicker_translate")} //using translate getting word 'ptBR' for portuguese and 'en' for english
/>

When I change the language, this code works well just when i pick the date from calendar...

English Calendar Portuguese Calendar

But when I try to write/input from the keyboard I get this error

enter image description here

The source code from this component is --> Project: https://github.com/Hacker0x01/react-datepicker

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use date-fns for ptBR. (Basically, the error is because the calendar needs more translations than the one which you write).

So, the recommended is to use them from date-fns, one example would be:

import React from "react";
import DatePicker from "react-datepicker";
import { registerLocale } from "react-datepicker";

import "react-datepicker/dist/react-datepicker.css";
import { en, ptBR } from "date-fns/locale";

registerLocale("ptBR", ptBR);
registerLocale("en", en);

export default function App() {
  const [language, setLanguage] = React.useState("ptBR");

  return (
    <div className="App">
      <h3>Change the Language ({language})</h3>
      <DatePicker locale={language} />
      <button onClick={() => setLanguage("ptBr")}>ptBr</button>
      <button onClick={() => setLanguage("en")}>eng</button>
    </div>
  );
}

The example is running on CodeSandbox: https://codesandbox.io/s/empty-browser-yfvg7?file=/src/App.js

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!