Files
SRCmail/hooks/use-format-event-date.ts
Hamed FallahandGitHub e10fced28a feat: add Jalali (Persian/Shamsi) calendar support with Saturday as week start (#490)
* feat: add Jalali (Persian/Shamsi) calendar support with Saturday as week start

- Add jalaali-js library for Gregorian ↔ Jalali date conversion
- Create lib/jalali-utils.ts with Jalali calendar utilities
- Create hooks/use-calendar-locale.ts for unified calendar locale handling
- Expand FirstDayOfWeek type to include 6 (Saturday)
- Update all calendar views (month, week, day, mini, toolbar) to support
  Jalali calendar display and Saturday-first week ordering
- Add Jalali month names (Farvardin … Esfand) to all locale files
- Add Persian (fa) locale with full translations
- Update settings UI to include Saturday as first day of week option
- Update useFormatEventDate to show Jalali dates when locale is fa
- Auto-detect Jalali calendar when fa locale is active

The calendar system automatically switches to Jalali when the locale is
set to Persian (fa). All internal date handling remains Gregorian (ISO
8601) for JMAP protocol compatibility; Jalali conversion is purely at
the display layer.

* Add PR template for Jalali calendar feature

* chore: remove accidentally added PR template

* fix: add image_too_large key to fa locale for PR #462 compatibility
2026-07-08 15:42:42 +02:00

43 lines
1.5 KiB
TypeScript

import { useCallback } from "react";
import { useTranslations, useLocale } from "next-intl";
import { format } from "date-fns";
import { toJalali, shouldUseJalaliCalendar, JALALI_MONTHS } from "@/lib/jalali-utils";
/**
* Returns a memoized function that formats a calendar event date
* using the current locale for day and month names.
*
* The string will be in the format: "EEE, MMM d, yyyy"
*
* For example: "Wed, Apr 29, 2026" (en)
* "Qua, Abr 29, 2026" (pt)
*
* When the Jalali calendar is active (fa locale), the format uses
* Persian day/month names with the Jalali year, e.g.:
* "چهارشنبه, ۹ اردیبهشت ۱۴۰۵"
*/
export function useFormatEventDate(): (date: Date) => string {
const t = useTranslations("calendar");
const locale = useLocale();
const isJalali = shouldUseJalaliCalendar(locale);
return useCallback(
(date: Date): string => {
if (isJalali) {
const { jy, jm, jd } = toJalali(date);
// Use Gregorian day-of-week for the translation key (date-fns format)
const dayOfWeek = format(date, "EEE").toLowerCase();
const monthName = JALALI_MONTHS[jm - 1];
return `${t(`days.${dayOfWeek}`)}, ${jd} ${monthName} ${jy}`;
}
const dayOfWeek = format(date, "EEE").toLowerCase();
const month = format(date, "MMM").toLowerCase();
const day = format(date, "d");
const year = format(date, "yyyy");
return `${t(`days.${dayOfWeek}`)}, ${t(`months.${month}`)} ${day}, ${year}`;
},
[t, isJalali]
);
}