最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Multi Language React DatePicker - Stack Overflow

matteradmin7PV0评论

I'm trying to translate the ponent 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...

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

The source code from this ponent is --> Project:

I'm trying to translate the ponent 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...

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

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

Share Improve this question asked Sep 23, 2021 at 19:53 Rafael BrenoRafael Breno 1132 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

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 remended 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

Post a comment

comment list (0)

  1. No comments so far