Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda