calidy-com / dayjs-calendarsystems

Day.js Calendar Systems Plugin : extends Day.js library to allow the use of non-gregorian calendar systems.
Other
16 stars 3 forks source link

problem with locale #5

Closed hmz22 closed 2 months ago

hmz22 commented 2 months ago

i creat custom ant design datepicker and use your lib:

import dayjsGenerateConfig from "rc-picker/lib/generate/dayjs";
import generatePicker from "antd/es/date-picker/generatePicker";
import fa from "antd/es/date-picker/locale/fa_IR";
import {
  PickerProps,
  RangePickerBaseProps,
  RangePickerDateProps,
  RangePickerTimeProps,
} from "antd/lib/date-picker/generatePicker";
import dayjs, { Dayjs } from "dayjs";
import generateCalendar, {
  CalendarProps,
} from "antd/lib/calendar/generateCalendar";
import "antd/dist/reset.css";
import calendarSystems from "@calidy/dayjs-calendarsystems";
import PersianCalendarSystem from "@calidy/dayjs-calendarsystems/calendarSystems/PersianCalendarSystem";

const locale = {
  name: "fa",
  weekdays: "یک‌شنبه_دوشنبه_سه‌شنبه_چهارشنبه_پنج‌شنبه_جمعه_شنبه".split("_"),
  weekdaysShort:
    "یک\u200cشنبه_دوشنبه_سه\u200cشنبه_چهارشنبه_پنج\u200cشنبه_جمعه_شنبه".split(
      "_"
    ),
  weekdaysMin: "ی_د_س_چ_پ_ج_ش".split("_"),
  weekStart: 6,
  months:
    "فروردین_اردیبهشت_خرداد_تیر_مرداد_شهریور_مهر_آبان_آذر_دی_بهمن_اسفند".split(
      "_"
    ),
  monthsShort:
    "فروردین_اردیبهشت_خرداد_تیر_مرداد_شهریور_مهر_آبان_آذر_دی_بهمن_اسفند".split(
      "_"
    ),
  ordinal: function (n: any) {
    return n;
  },
  formats: {
    LT: "HH:mm",
    LTS: "HH:mm:ss",
    L: "DD/MM/YYYY",
    LL: "D MMMM YYYY",
    LLL: "D MMMM YYYY HH:mm",
    LLLL: "dddd, D MMMM YYYY HH:mm",
  },
  relativeTime: {
    future: "در %s",
    past: "%s پیش",
    s: "چند ثانیه",
    m: "یک دقیقه",
    mm: "%d دقیقه",
    h: "یک ساعت",
    hh: "%d ساعت",
    d: "یک روز",
    dd: "%d روز",
    M: "یک ماه",
    MM: "%d ماه",
    y: "یک سال",
    yy: "%d سال",
  },
};

dayjs.locale(locale, undefined, true);
dayjs.extend(calendarSystems);
dayjs.registerCalendarSystem("persian", new PersianCalendarSystem());

const CustomDate = generatePicker({
  ...dayjsGenerateConfig,
  locale: {
    ...dayjsGenerateConfig.locale,
  },
  getNow: () => dayjs().toCalendarSystem("persian"),
});

type CustomRangePickerProps =
  | RangePickerBaseProps<Dayjs>
  | RangePickerDateProps<Dayjs>
  | RangePickerTimeProps<Dayjs>;

function DatePickerJalali({ ...rest }: CustomRangePickerProps) {
  return (
    <>
      <CustomDate.RangePicker style={{ width: "100%" }} {...rest} locale={fa} />
    </>
  );
}

export const DatePickerJalaliSingle = ({ ...rest }: PickerProps<Dayjs>) => {
  return <CustomDate style={{ width: "100%" }} {...rest} locale={fa} />;
};

export default DatePickerJalali;

and my problem is locale with fa: Screenshot 2024-06-19 195652 and if see month name is wrong what problem my do? and also need default persian calender e.g:

dayjs(initialData.validityDate).toCalendarSystem(
          "persian"
        )

I must do for every where need convert but I need just global do it and when need other calendar type change it how can do it? thank you

hmz22 commented 2 months ago

this sample of date picker: https://stackblitz.com/edit/react-xicmts?file=demo.tsx in demo file first is original antd date picker second is your Gregorian and third is Persian and also if check when use third date picker Persian calendar when hover on day have a little lag because of calendar system can fix it? for now for fixing month name in date picker i use this:

export const CustomDateJ = DatePicker.generatePicker({
  ...dayjsGenerateConfig,
  locale: {
    ...dayjsGenerateConfig.locale,
    getShortMonths: () => {
      return "فروردین_اردیبهشت_خرداد_تیر_مرداد_شهریور_مهر_آبان_آذر_دی_بهمن_اسفند".split(
        "_"
      );
    },
  },

  getNow: () => jDayjs(),
});
amirhmoradi commented 2 months ago

@hmz22 Hi, thanks for the info. I did a quick check on the code:

=> I invite you to have a look on the unit tests that I have wrote inside my plugin's code to see how to use the correct month names in Farsi.

I will close this ticket as it does not bring up an issue with the project. If you think there is another bug somewhere, please open a new issue with relevant details.

Warmest.