gpt4 book ai didi

c# - 如何自定义CultureInfo的默认日历?

转载 作者:太空狗 更新时间:2023-10-30 00:15:20 24 4
gpt4 key购买 nike

我想将日历自定义为波斯语,但是在将 CultureInfo 设置为这样之后:

CultureInfo fa = new CultureInfo("fa-IR",true);
Thread.CurrentThread.CurrentCulture = fa;

calendar

月份和日期的名称未更改,因此决定覆盖默认日历。

我该怎么做?

最佳答案

重写像我这样的初学者:

public class PersianCulture : CultureInfo
{

private readonly System.Globalization.Calendar cal;

private readonly System.Globalization.Calendar[] optionals;

public PersianCulture()

: this("fa-IR", true)
{

}
public PersianCulture(string cultureName, bool useUserOverride)

: base(cultureName, useUserOverride)
{
cal = base.OptionalCalendars[0];
var optionalCalendars = new List<System.Globalization.Calendar>();
optionalCalendars.AddRange(base.OptionalCalendars);
optionalCalendars.Insert(0, new PersianCalendar());
Type formatType = typeof(DateTimeFormatInfo);
Type calendarType = typeof(System.Globalization.Calendar);
PropertyInfo idProperty = calendarType.GetProperty("ID", BindingFlags.Instance | BindingFlags.NonPublic);
FieldInfo optionalCalendarfield = formatType.GetField("optionalCalendars",BindingFlags.Instance | BindingFlags.NonPublic);
var newOptionalCalendarIDs = new Int32[optionalCalendars.Count];

for (int i = 0; i < newOptionalCalendarIDs.Length; i++)

newOptionalCalendarIDs[i] = (Int32)idProperty.GetValue(optionalCalendars[i], null);
optionalCalendarfield.SetValue(DateTimeFormat, newOptionalCalendarIDs);

optionals = optionalCalendars.ToArray();

cal = optionals[0];

DateTimeFormat.Calendar = optionals[0];
DateTimeFormat.MonthNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };

DateTimeFormat.MonthGenitiveNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };

DateTimeFormat.AbbreviatedMonthNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };

DateTimeFormat.AbbreviatedMonthGenitiveNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };

DateTimeFormat.AbbreviatedDayNames = new string[] { "ی", "د", "س", "چ", "پ", "ج", "ش" };

DateTimeFormat.ShortestDayNames = new string[] { "ی", "د", "س", "چ", "پ", "ج", "ش" };

DateTimeFormat.DayNames = new string[] { "یکشنبه", "دوشنبه", "ﺳﻪشنبه", "چهارشنبه", "پنجشنبه", "جمعه", "شنبه" };
DateTimeFormat.AMDesignator = "ق.ظ";
DateTimeFormat.PMDesignator = "ب.ظ";

}
public override System.Globalization.Calendar Calendar
{

get { return cal; }

}
public override System.Globalization.Calendar[] OptionalCalendars
{

get { return optionals; }

}
}

然后在 Page_Load 中:

PersianCulture fa = new PersianCulture();

Thread.CurrentThread.CurrentCulture = fa;

表格here

关于c# - 如何自定义CultureInfo的默认日历?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15817348/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com