How to customize CultureInfo's default calendar?

后端 未结 3 1094
后悔当初
后悔当初 2021-02-09 22:53

I want to customize calendar to persian but after setting CultureInfo as this:

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


        
3条回答
  •  不思量自难忘°
    2021-02-09 22:57

    overriding for beginners like me:

    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();
            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; }
    
        }
    }
    

    then in Page_Load:

    PersianCulture fa = new PersianCulture();
    
    Thread.CurrentThread.CurrentCulture = fa;
    

    form here

提交回复
热议问题