calendar display type supports Gregorian with Persian, date display type supports Persian calendar

This commit is contained in:
MaysWind
2025-09-06 23:02:09 +08:00
parent 757f9e5b02
commit a469d66358
24 changed files with 209 additions and 20 deletions
+10 -2
View File
@@ -3,7 +3,8 @@ import type { TypeAndName } from '@/core/base.ts';
export enum CalendarType {
Gregorian = 0,
Buddhist = 1,
Chinese = 2
Chinese = 2,
Persian = 3
}
export interface ChineseCalendarLocaleData {
@@ -14,6 +15,11 @@ export interface ChineseCalendarLocaleData {
readonly solarTermNames: string[];
}
export interface PersianCalendarLocaleData {
readonly monthNames: string[];
readonly monthShortNames: string[];
}
export class CalendarDisplayType implements TypeAndName {
private static readonly allInstances: CalendarDisplayType[] = [];
private static readonly allInstancesByType: Record<number, CalendarDisplayType> = {};
@@ -23,6 +29,7 @@ export class CalendarDisplayType implements TypeAndName {
public static readonly Gregorian = new CalendarDisplayType(1, 'Gregorian', 'Gregorian', CalendarType.Gregorian);
public static readonly Buddhist = new CalendarDisplayType(2, 'Buddhist', 'Buddhist', CalendarType.Buddhist);
public static readonly GregorianWithChinese = new CalendarDisplayType(3, 'GregorianWithChinese', 'Gregorian with Chinese', CalendarType.Gregorian, CalendarType.Chinese);
public static readonly GregorianWithPersian = new CalendarDisplayType(4, 'GregorianWithPersian', 'Gregorian with Persian', CalendarType.Gregorian, CalendarType.Persian);
public static readonly Default = CalendarDisplayType.Gregorian;
@@ -64,7 +71,8 @@ export class DateDisplayType implements TypeAndName {
public static readonly LanguageDefaultType: number = 0;
public static readonly Gregorian = new DateDisplayType(1, 'Gregorian', 'Gregorian', CalendarType.Gregorian);
public static readonly Chinese = new DateDisplayType(2, 'Buddhist', 'Buddhist', CalendarType.Buddhist);
public static readonly Buddhist = new DateDisplayType(2, 'Buddhist', 'Buddhist', CalendarType.Buddhist);
public static readonly Persian = new DateDisplayType(3, 'Persian', 'Persian', CalendarType.Persian);
public static readonly Default = DateDisplayType.Gregorian;
+3 -1
View File
@@ -1,5 +1,5 @@
import type { TypeAndName, TypeAndDisplayName } from '@/core/base.ts';
import type { CalendarType, ChineseCalendarLocaleData } from '@/core/calendar.ts';
import type { CalendarType, ChineseCalendarLocaleData, PersianCalendarLocaleData } from '@/core/calendar.ts';
import type { NumeralSystem } from '@/core/numeral.ts';
export interface DateTime {
@@ -16,6 +16,7 @@ export interface DateTime {
getLocalizedCalendarMonthDisplayShortName(options: DateTimeFormatOptions): string;
getGregorianCalendarDay(): number;
getLocalizedCalendarDay(options: DateTimeFormatOptions): string;
isLocalizedCalendarFirstDayOfMonth(options: DateTimeFormatOptions): boolean;
getGregorianCalendarYearDashMonthDashDay(): TextualYearMonthDay;
getGregorianCalendarYearDashMonth(): TextualYearMonth;
getWeekDay(): WeekDay;
@@ -37,6 +38,7 @@ export interface DateTimeFormatOptions {
calendarType: CalendarType;
localeData: DateTimeLocaleData;
chineseCalendarLocaleData: ChineseCalendarLocaleData;
persianCalendarLocaleData: PersianCalendarLocaleData;
}
export interface DateTimeLocaleData {
+31
View File
@@ -1,6 +1,8 @@
import moment from 'moment-timezone';
import { type unitOfTime } from 'moment/moment';
import jalaali, { type JalaaliDateObject } from 'jalaali-js';
import {
type ChineseCalendarLocaleData,
CalendarType
@@ -91,6 +93,7 @@ class MomentDateTime implements DateTime {
private readonly instance: moment.Moment;
private chineseDateInfo?: ChineseYearMonthDayInfo | undefined = undefined;
private persianDateInfo?: JalaaliDateObject | undefined = undefined;
private constructor(instance: moment.Moment) {
this.instance = instance;
@@ -105,6 +108,8 @@ class MomentDateTime implements DateTime {
return (this.instance.year() + 543).toString();
} else if (options && options.calendarType === CalendarType.Chinese) {
return this.getChineseDateInfo(options.chineseCalendarLocaleData)?.displayYear ?? '';
} else if (options && options.calendarType === CalendarType.Persian) {
return this.getPersianDateInfo().jy.toString();
}
return this.instance.year().toString();
@@ -148,6 +153,8 @@ class MomentDateTime implements DateTime {
public getLocalizedCalendarMonth(options: DateTimeFormatOptions): string {
if (options && options.calendarType === CalendarType.Chinese) {
return this.getChineseDateInfo(options.chineseCalendarLocaleData)?.displayMonth ?? '';
} else if (options && options.calendarType === CalendarType.Persian) {
return this.getPersianDateInfo().jm.toString();
}
return (this.instance.month() + 1).toString();
@@ -160,6 +167,8 @@ class MomentDateTime implements DateTime {
if (options && options.calendarType === CalendarType.Chinese) {
return this.getChineseDateInfo(options.chineseCalendarLocaleData)?.displayMonth ?? '';
} else if (options && options.calendarType === CalendarType.Persian) {
return options.persianCalendarLocaleData.monthNames[this.getPersianDateInfo().jm - 1] ?? '';
}
const names = options.localeData.months();
@@ -173,6 +182,8 @@ class MomentDateTime implements DateTime {
if (options && options.calendarType === CalendarType.Chinese) {
return this.getChineseDateInfo(options.chineseCalendarLocaleData)?.displayMonth ?? '';
} else if (options && options.calendarType === CalendarType.Persian) {
return options.persianCalendarLocaleData.monthShortNames[this.getPersianDateInfo().jm - 1] ?? '';
}
const names = options.localeData.monthsShort();
@@ -186,11 +197,23 @@ class MomentDateTime implements DateTime {
public getLocalizedCalendarDay(options: DateTimeFormatOptions): string {
if (options && options.calendarType === CalendarType.Chinese) {
return this.getChineseDateInfo(options.chineseCalendarLocaleData)?.displayDay ?? '';
} else if (options && options.calendarType === CalendarType.Persian) {
return this.getPersianDateInfo().jd.toString();
}
return this.instance.date().toString();
}
public isLocalizedCalendarFirstDayOfMonth(options: DateTimeFormatOptions): boolean {
if (options && options.calendarType === CalendarType.Chinese) {
return this.getChineseDateInfo(options.chineseCalendarLocaleData)?.day === 1;
} else if (options && options.calendarType === CalendarType.Persian) {
return this.getPersianDateInfo().jd === 1;
}
return this.instance.date() === 1;
}
public getGregorianCalendarYearDashMonthDashDay(): TextualYearMonthDay {
return (this.instance.year() + '-' + (this.instance.month() + 1).toString().padStart(2, NumeralSystem.WesternArabicNumerals.digitZero) + '-' + this.instance.date().toString().padStart(2, NumeralSystem.WesternArabicNumerals.digitZero)) as TextualYearMonthDay;
}
@@ -337,6 +360,14 @@ class MomentDateTime implements DateTime {
return this.chineseDateInfo;
}
private getPersianDateInfo(): JalaaliDateObject {
if (!this.persianDateInfo) {
this.persianDateInfo = jalaali.toJalaali(this.instance.year(), this.instance.month() + 1, this.instance.date());
}
return this.persianDateInfo;
}
static isYearFirstTime(dateTime: MomentDateTime): boolean {
const currentUnixTime = dateTime.instance.clone().set({ millisecond: 0 }).unix();
const expectedUnxTime = dateTime.instance.clone().set({ millisecond: 0 }).startOf('year').unix();
+30
View File
@@ -0,0 +1,30 @@
{
"monthNames": [
"فروردین",
"اردیبهشت",
"خرداد",
"تیر",
"مرداد",
"شهریور",
"مهر",
"آبان",
"آذر",
"دی",
"بهمن",
"اسفند"
],
"monthShortNames": [
"فرو",
"ارد",
"خرد",
"تیر",
"مرد",
"شهر",
"مهر",
"آبا",
"آذر",
"دی",
"بهم",
"اسف"
]
}
+12
View File
@@ -0,0 +1,12 @@
import fa from './fa.json';
type PersianCalendarLocaleDataKey = 'monthNames' | 'monthShortNames';
type PersianCalendarLocaleData = {
[K in PersianCalendarLocaleDataKey]: string[];
};
export const DEFAULT_CONTENT: PersianCalendarLocaleData = fa;
export const ALL_LANGUAGES: Record<string, PersianCalendarLocaleData> = {
'fa': fa
}
+3 -1
View File
@@ -139,7 +139,9 @@
"calendar": {
"Gregorian": "Gregorian",
"Buddhist": "Buddhist",
"Gregorian with Chinese": "Gregorian with Chinese"
"Persian": "Persian",
"Gregorian with Chinese": "Gregorian with Chinese",
"Gregorian with Persian": "Gregorian with Persian"
},
"datetime": {
"AM": {
+3 -1
View File
@@ -139,7 +139,9 @@
"calendar": {
"Gregorian": "Gregorian",
"Buddhist": "Buddhist",
"Gregorian with Chinese": "Gregorian with Chinese"
"Persian": "Persian",
"Gregorian with Chinese": "Gregorian with Chinese",
"Gregorian with Persian": "Gregorian with Persian"
},
"datetime": {
"AM": {
+3 -1
View File
@@ -139,7 +139,9 @@
"calendar": {
"Gregorian": "Gregorian",
"Buddhist": "Buddhist",
"Gregorian with Chinese": "Gregorian with Chinese"
"Persian": "Persian",
"Gregorian with Chinese": "Gregorian with Chinese",
"Gregorian with Persian": "Gregorian with Persian"
},
"datetime": {
"AM": {
+54 -1
View File
@@ -16,18 +16,25 @@ import {
DEFAULT_CONTENT as CHINESE_CALENDAR_DEFAULT_CONTENT
} from '@/locales/calendar/chinese/index.ts';
import {
ALL_LANGUAGES as PERSIAN_CALENDAR_ALL_LANGUAGES,
DEFAULT_CONTENT as PERSIAN_CALENDAR_DEFAULT_CONTENT
} from '@/locales/calendar/persian/index.ts';
import {
TextDirection
} from '@/core/text.ts';
import {
type ChineseCalendarLocaleData,
type PersianCalendarLocaleData,
CalendarType,
CalendarDisplayType,
DateDisplayType
} from '@/core/calendar.ts';
import {
type DateTime,
type DateTimeFormatOptions,
type DateTimeLocaleData,
type TextualYearMonth,
@@ -174,7 +181,9 @@ import {
getBrowserTimezoneOffset,
getBrowserTimezoneOffsetMinutes,
getCurrentUnixTime,
getYearMonthDayDateTime,
parseDateTimeFromUnixTime,
getGregorianCalendarYearMonthDays,
getDateTimeFormatType,
getFiscalYearTimeRangeFromUnixTime,
getFiscalYearTimeRangeFromYear,
@@ -481,6 +490,16 @@ export function useI18n() {
return chineseCalendarLocaleData;
}
function getPersianCalendarLocaleData(): PersianCalendarLocaleData {
const localeData = PERSIAN_CALENDAR_ALL_LANGUAGES[locale.value] ?? PERSIAN_CALENDAR_DEFAULT_CONTENT;
const persianCalendarLocaleData: PersianCalendarLocaleData = {
monthNames: localeData['monthNames'],
monthShortNames: localeData['monthShortNames']
};
return persianCalendarLocaleData;
}
function getAllCurrencyDisplayTypes(numeralSystem: NumeralSystem, decimalSeparator: string): TypeAndDisplayName[] {
const defaultCurrencyDisplayTypeName = t('default.currencyDisplayType');
let defaultCurrencyDisplayType = CurrencyDisplayType.parse(defaultCurrencyDisplayTypeName);
@@ -713,7 +732,8 @@ export function useI18n() {
numeralSystem: numeralSystem,
calendarType: calendarType,
localeData: getDateTimeLocaleData(),
chineseCalendarLocaleData: getChineseCalendarLocaleData()
chineseCalendarLocaleData: getChineseCalendarLocaleData(),
persianCalendarLocaleData: getPersianCalendarLocaleData()
};
}
@@ -773,6 +793,24 @@ export function useI18n() {
return currencyDisplayType;
}
function getCalendarAlternateDisplayDate(dateTime: DateTime, dateTimeFormatOptions: DateTimeFormatOptions): CalendarAlternateDate {
const numeralSystem = getCurrentNumeralSystemType();
let displayDate = numeralSystem.replaceWesternArabicDigitsToLocalizedDigits(dateTime.getLocalizedCalendarDay(dateTimeFormatOptions));
if (dateTime.isLocalizedCalendarFirstDayOfMonth(dateTimeFormatOptions)) {
displayDate = dateTime.getLocalizedCalendarMonthDisplayShortName(dateTimeFormatOptions);
}
const alternateDate: CalendarAlternateDate = {
year: dateTime.getGregorianCalendarYear(),
month: dateTime.getGregorianCalendarMonth(),
day: dateTime.getGregorianCalendarDay(),
displayDate: displayDate
};
return alternateDate;
}
// public functions
function translateIf(text: string | undefined, isTranslate?: boolean): string {
if (!isDefined(text)) {
@@ -1922,6 +1960,17 @@ export function useI18n() {
ret.push(alternateDate);
}
return ret;
} else if (calendarDisplayType === CalendarType.Persian) {
const dateTimeFormatOptions = getDateTimeFormatOptions();
const monthDays: number = getGregorianCalendarYearMonthDays(yearMonth);
const ret: CalendarAlternateDate[] = [];
for (let i = 1; i <= monthDays; i++) {
const dateTime = getYearMonthDayDateTime(yearMonth.year, yearMonth.month1base, i);
ret.push(getCalendarAlternateDisplayDate(dateTime, dateTimeFormatOptions));
}
return ret;
}
@@ -1944,6 +1993,10 @@ export function useI18n() {
}
return getChineseCalendarAlternateDisplayDate(chineseDate);
} else if (calendarDisplayType === CalendarType.Persian) {
const dateTimeFormatOptions = getDateTimeFormatOptions();
const dateTime = getYearMonthDayDateTime(yearMonthDay.year, yearMonthDay.month, yearMonthDay.day);
return getCalendarAlternateDisplayDate(dateTime, dateTimeFormatOptions);
}
return undefined;
+3 -1
View File
@@ -139,7 +139,9 @@
"calendar": {
"Gregorian": "Gregorian",
"Buddhist": "Buddhist",
"Gregorian with Chinese": "Gregorian with Chinese"
"Persian": "Persian",
"Gregorian with Chinese": "Gregorian with Chinese",
"Gregorian with Persian": "Gregorian with Persian"
},
"datetime": {
"AM": {
+3 -1
View File
@@ -139,7 +139,9 @@
"calendar": {
"Gregorian": "Gregorian",
"Buddhist": "Buddhist",
"Gregorian with Chinese": "Gregorian with Chinese"
"Persian": "Persian",
"Gregorian with Chinese": "Gregorian with Chinese",
"Gregorian with Persian": "Gregorian with Persian"
},
"datetime": {
"AM": {
+3 -1
View File
@@ -139,7 +139,9 @@
"calendar": {
"Gregorian": "Gregorian",
"Buddhist": "Buddhist",
"Gregorian with Chinese": "Gregorian with Chinese"
"Persian": "Persian",
"Gregorian with Chinese": "Gregorian with Chinese",
"Gregorian with Persian": "Gregorian with Persian"
},
"datetime": {
"AM": {
+3 -1
View File
@@ -139,7 +139,9 @@
"calendar": {
"Gregorian": "Gregorian",
"Buddhist": "Buddhist",
"Gregorian with Chinese": "Gregorian with Chinese"
"Persian": "Persian",
"Gregorian with Chinese": "Gregorian with Chinese",
"Gregorian with Persian": "Gregorian with Persian"
},
"datetime": {
"AM": {
+3 -1
View File
@@ -139,7 +139,9 @@
"calendar": {
"Gregorian": "Gregorian",
"Buddhist": "Buddhist",
"Gregorian with Chinese": "Gregorian with Chinese"
"Persian": "Persian",
"Gregorian with Chinese": "Gregorian with Chinese",
"Gregorian with Persian": "Gregorian with Persian"
},
"datetime": {
"AM": {
+3 -1
View File
@@ -139,7 +139,9 @@
"calendar": {
"Gregorian": "Gregorian",
"Buddhist": "Buddhist",
"Gregorian with Chinese": "Gregorian with Chinese"
"Persian": "Persian",
"Gregorian with Chinese": "Gregorian with Chinese",
"Gregorian with Persian": "Gregorian with Persian"
},
"datetime": {
"AM": {
+3 -1
View File
@@ -139,7 +139,9 @@
"calendar": {
"Gregorian": "Gregorian",
"Buddhist": "Buddhist",
"Gregorian with Chinese": "Gregorian with Chinese"
"Persian": "Persian",
"Gregorian with Chinese": "Gregorian with Chinese",
"Gregorian with Persian": "Gregorian with Persian"
},
"datetime": {
"AM": {
+3 -1
View File
@@ -139,7 +139,9 @@
"calendar": {
"Gregorian": "公历",
"Buddhist": "佛教日历",
"Gregorian with Chinese": "公历+农历"
"Persian": "伊朗历",
"Gregorian with Chinese": "公历+农历",
"Gregorian with Persian": "公历+伊朗历"
},
"datetime": {
"AM": {
+3 -1
View File
@@ -139,7 +139,9 @@
"calendar": {
"Gregorian": "公曆",
"Buddhist": "佛曆",
"Gregorian with Chinese": "公曆+農曆"
"Persian": "伊朗曆",
"Gregorian with Chinese": "公曆+農曆",
"Gregorian with Persian": "公曆+伊朗曆"
},
"datetime": {
"AM": {