Пример #1
0
        public static CultureInfo GetPersianCulture()
        {
            //*****************************************************************************
            //*****************************2008-04-02 By A. Samarian*****Begin*************
            //Modified:2010-09-18

            //Modified by: Asad Samarian
            //*****************************************************************************
            CultureInfo persianCulture = new CultureInfo("fa-IR");
            DateTimeFormatInfo info = persianCulture.DateTimeFormat;

            info.DayNames = new string[] { "یکشنبه", "دوشنبه", "ﺳﻪشنبه", "چهارشنبه", "پنجشنبه", "جمعه", "شنبه" };
            info.AbbreviatedDayNames = new string[] { "ی", "د", "س", "چ", "پ", "ج", "ش" };

            info.MonthNames = new string[] { "فروردین", "ارديبهشت", "خرداد", "تير", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
            info.AbbreviatedMonthNames = new string[] { "فروردین", "ارديبهشت", "خرداد", "تير", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
            //It Seems .NET 4 use Genitive manes for months.
            info.MonthGenitiveNames = new string[] { "فروردین", "ارديبهشت", "خرداد", "تير", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
            info.AbbreviatedMonthGenitiveNames = new string[] { "فروردین", "ارديبهشت", "خرداد", "تير", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };

            info.AMDesignator = "ق.ظ";
            info.PMDesignator = "ب.ظ";
            info.ShortDatePattern = "yyyy/MM/dd";
            info.LongDatePattern = "dddd dd MMMM yyyy";
            info.FullDateTimePattern = "dddd dd MMMM yyyy, HH:mm:ss";
            info.FirstDayOfWeek = DayOfWeek.Saturday;
            PersianCalendar cal = new PersianCalendar();

            // 2010-07-18 this part throws an error under .NET 4, Asad says it is still needed for 3.5
            if (ConfigHelper.GetBoolProperty("UseNet35PersianHelper", false)) //this setting is true in the We.config for 3.5 .NET but not 4.0
            {
                typeof(DateTimeFormatInfo).GetField("calendar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic).SetValue(info, cal);
                object obj = typeof(DateTimeFormatInfo).GetField("m_cultureTableRecord",

                    BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic).GetValue(info);

                obj.GetType().GetMethod("UseCurrentCalendar",
                    BindingFlags.NonPublic | BindingFlags.Instance).Invoke(obj, new object[] { cal.GetType().GetProperty("ID", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(cal, null) });

                //typeof(DateTimeFormatInfo).GetField("calendar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic).SetValue(info, cal);
                //typeof(CultureInfo).GetField("calendar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic).SetValue(persianCulture, cal);
            }

            typeof(DateTimeFormatInfo).GetField("calendar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic).SetValue(info, cal);
            typeof(CultureInfo).GetField("calendar", (BindingFlags.NonPublic | (BindingFlags.Public | BindingFlags.Instance))).SetValue(persianCulture, cal);

            persianCulture.DateTimeFormat = info;
            persianCulture.NumberFormat.CurrencyDecimalDigits = 0;
            return persianCulture;

            //*****************************************************************************
            //*************************2008-04-02 By A. Samarian*****End*******************
            //Modified:2010-09-18
            //Modified by: Asad Samarian
            //*****************************************************************************
        }
        public static void SetPersianCalendar()
        {
            CultureInfo info = new CultureInfo("fa-IR")
                                   {
                                       NumberFormat = { DigitSubstitution = DigitShapes.NativeNational }
                                   };


            DateTimeFormatInfo dateTimeFormat = info.DateTimeFormat;
            dateTimeFormat.AMDesignator = "ق.ظ";
            dateTimeFormat.PMDesignator = "ب.ظ";
            dateTimeFormat.DateSeparator = "/";
            dateTimeFormat.ShortDatePattern = "yyyy/MM/dd";
            dateTimeFormat.FirstDayOfWeek = DayOfWeek.Saturday;
            dateTimeFormat.AbbreviatedDayNames = new[] { "ی", "د", "س", "چ", "پ", "ج", "ش" };
            dateTimeFormat.DayNames = new[] { "یکشنبه", "دوشنبه", "سه‌شنبه", "چهارشنبه", "پنجشنبه", "جمعه", "شنبه" };
            dateTimeFormat.MonthGenitiveNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
            dateTimeFormat.ShortestDayNames = new[] { "ی", "د", "س", "چ", "پ", "ج", "ش" };
            dateTimeFormat.AbbreviatedMonthNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
            dateTimeFormat.MonthNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
            PersianCalendar calendar = new PersianCalendar();
            typeof(DateTimeFormatInfo).GetField("calendar", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).SetValue(dateTimeFormat, calendar);
            object obj2 = typeof(DateTimeFormatInfo).GetField("m_cultureTableRecord", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).GetValue(dateTimeFormat);
            obj2.GetType().GetMethod("UseCurrentCalendar", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(obj2, new[] { calendar.GetType().GetProperty("ID", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(calendar, null) });
            typeof(CultureInfo).GetField("calendar", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).SetValue(info, calendar);
            Thread.CurrentThread.CurrentCulture = info;
            Thread.CurrentThread.CurrentUICulture = info;
            CultureInfo.CurrentCulture.DateTimeFormat = dateTimeFormat;
            CultureInfo.CurrentUICulture.DateTimeFormat = dateTimeFormat;
        }