react-multi-date-picker库的意大利日历转换

huangapple go评论79阅读模式
英文:

Conversion of Italian calendar for react-multi-date-picker library

问题

我在我的项目中使用了react-multi-date-picker,并且我正在使用多种模式。因此,当我的语言是意大利语时,我必须使用意大利语日历。我尝试了locale="it",但它没有起作用。经过研究,我找到了一个解决方案,即创建一个包含意大利语月份、星期几和时段的JavaScript文件。此外,我还为react-date-object提供了引用。在那里创建了各种类型的区域设置JavaScript文件。我创建了相同的文件并尝试将其传递给DatePicker,但它显示了错误的输出。

以下是我的App.js文件用于DatePicker:

import DatePicker from "react-multi-date-picker";
import DatePanel from "react-multi-date-picker/plugins/date_panel";
import italian_it from './italianCalendar';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <DatePicker
          locale={italian_it}
          multiple
          format="DD/MM/YYYY"
          plugins={[<DatePanel />]}
        />
      </header>
    </div>
  );
}

export default App;

我的italianCalendar文件如下:

module.exports = { 
  name: "italian_it", 
  months: [ 
    ["gennaio", "gennaio"], 
    ["febbraio", "febbraio"], 
    ["marzo", "marzo"], 
    ["aprile", "aprile"], 
    ["maggio", "maggio"], 
    ["giugno", "giugno"], 
    ["luglio", "luglio"], 
    ["agosto", "agosto"], 
    ["settembre", "settembre"], 
    ["ottobre", "ottobre"], 
    ["novembre", "novembre"], 
    ["dicembre", "dicembre"], 
  ], 
  weekDays: [ 
    ["domenica", "dom"], 
    ["lunedì", "lun"], 
    ["martedì", "mar"], 
    ["mercoledì", "mer"], 
    ["giovedì", "gio"], 
    ["venerdì", "ven"], 
    ["sabato", "sab"]
  ], 
  digits: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], 
  meridiems: [ 
    ["am", "am"], 
    ["pm", "pm"], 
  ], 
};

输出显示如下:

react-multi-date-picker库的意大利日历转换

参考代码已经添加到了我的沙盒中:https://codesandbox.io/s/keen-worker-mnvdno

英文:

I have used react-multi-date-picker for my project and I am using multiple mode. For this I have to use Italian calendar when my language is Italian. I was tried with locale="it" but it is not working. After research, I got one solution which is, create js file which contain Italian months, weekdays numbers and meridiems. Also I had given reference for react-date-object. In that various types of the locale js files created. created same and tried to pass it to DatePicker but it showing me wrong output.
Actually, calendar is converting well but date is set to wrong days.

Here is the my App.js file for DatePicker

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import DatePicker from &quot;react-multi-date-picker&quot;;
import DatePanel from &quot;react-multi-date-picker/plugins/date_panel&quot;;
import italian_it from &#39;./italianCalendar&#39;;
function App() {
  return (
    &lt;div className=&quot;App&quot;&gt;
      &lt;header className=&quot;App-header&quot;&gt;
        &lt;DatePicker
          locale={italian_it}
          multiple
          format=&quot;DD/MM/YYYY&quot;
          plugins={[&lt;DatePanel /&gt;]}
        /&gt;
      &lt;/header&gt;
    &lt;/div&gt;
  );
}

export default App;

My italianCalendar file is as below,

module.exports = { name: "italian_it", months: [ ["gennaio", "gennaio"], ["febbraio", "febbraio"], ["marzo", "marzo"], ["aprile", "aprile"], ["maggio", "maggio"], ["giugno", "giugno"], ["luglio", "luglio"], ["agosto", "agosto"], ["settembre", "settembre"], ["ottobre", "ottobre"], ["novembre", "novembre"], ["dicembre", "dicembre"], ], weekDays: [ ["domenica", "dom"], ["lunedì", "lun"], ["martedì", "mar"], ["mercoledì", "mer"], ["giovedì", "gio"], ["venerdì", "ven"], ["sabato", "sab"]], digits: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], meridiems: [ ["am", "am"], ["pm", "pm"], ], };

<!-- end snippet -->

Output is showing like this.

react-multi-date-picker库的意大利日历转换

For reference, I have added my code over sandbox

https://codesandbox.io/s/keen-worker-mnvdno

答案1

得分: 1

你可以使用 react-date-object 库。它提供了一个 DateObject 类,可以处理不同类型的日历,尝试这样做:

import React from "react";
import ReactDOM from "react-dom";
import DatePicker from "react-multi-date-picker";
import DatePanel from "react-multi-date-picker/plugins/date_panel";
import { DateObject } from "react-date-object";

// 使用意大利日历创建一个新的 DateObject
const italianDateObject = new DateObject({ calendar: "italian" });

function App() {
  // 将默认日历设置为意大利日历
  const defaultCalendar = italianDateObject.calendar;

  return (
    <div className="App">
      <header className="App-header">
        <DatePicker
          defaultCalendar={defaultCalendar}
          multiple
          format="DD/MM/YYYY"
          plugins={[<DatePanel />]}
        />
      </header>
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));

请注意,以上代码是使用 react-date-object 库创建一个支持意大利日历的日期选择器的示例。

英文:

You could use the react-date-object library. It provides a DateObject class which can handle different kind of calendars, try this :

import React from &quot;react&quot;;
import ReactDOM from &quot;react-dom&quot;;
import DatePicker from &quot;react-multi-date-picker&quot;;
import DatePanel from &quot;react-multi-date-picker/plugins/date_panel&quot;;
import { DateObject } from &quot;react-date-object&quot;;

// Create a new DateObject with the Italian calendar
const italianDateObject = new DateObject({ calendar: &quot;italian&quot; });

function App() {
  // Set the default calendar to the Italian calendar
  const defaultCalendar = italianDateObject.calendar;

  return (
    &lt;div className=&quot;App&quot;&gt;
      &lt;header className=&quot;App-header&quot;&gt;
        &lt;DatePicker
          defaultCalendar={defaultCalendar}
          multiple
          format=&quot;DD/MM/YYYY&quot;
          plugins={[&lt;DatePanel /&gt;]}
        /&gt;
      &lt;/header&gt;
    &lt;/div&gt;
  );
}

ReactDOM.render(&lt;App /&gt;, document.getElementById(&quot;root&quot;));

huangapple
  • 本文由 发表于 2023年3月1日 15:31:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定