Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Multi Culture Calendar

2.43/5 (13 votes)
11 Sep 2009CPOL 1   1.4K  
A calendar which supports all types of calendars such as persian(Farsi)-Arabic and English calendars by changing the culture

Introduction

As you know, after changing the culture, all controls work with the new culture. In this control, you can use several calendars on a page and change the culture for each one.

The important thing is that Microsoft does not support the UI of the Persian calendar. Now you can use it very easily. It is possible to inherit this control from other controls which you want and make your custom controls.

Using the Code

The code is very easy and I override just the render method. Before the rendering and after that, I change the culture and only for Persian, I use Reflection to change the calendar (Shamsi Calendar).

Here is the important function on this control that shows you how it can be made possible using the Persian calendar:

C#
private void ChangeCulture()
{
DefaultCulture = new System.Globalization.CultureInfo(
    System.Threading.Thread.CurrentThread.CurrentUICulture.Name);

switch (SelectedCulture.Name)
{
case "fa-IR":
System.Globalization.DateTimeFormatInfo info = SelectedCulture.DateTimeFormat;
info.AbbreviatedDayNames = new string[] { "ی", "§", "«", "چ", "پ", "¤", "¬" };
info.DayNames = new string[] { "یک¬ë ى", "§ي¬ë ى", "ﺳﻪ¬ë ى", "چىں©¬ë ى", "پ뤬ë ى",
    "¤êمى", "¬ë ى" };
info.AbbreviatedMonthNames = new string[] { "ه©ي©§یë", "ں©§ï ى¬¢", "¦©§ں§", "¢ï©",
    "ꩧں§", "¬ى©یي©", "êى©", "™ ںë", "™¨©", "§ی", " ىêë", "ں«هë§", "" };
info.MonthNames = new string[] 
    { "ه©ي©§یë", "ں©§ï ى¬¢", "¦©§ں§", "¢ï©", "ꩧں§", "¬ى©یي©", 
    "êى©", "™ ںë", "™¨©", "§ی", " ىêë", "ں«هë§", "" };
info.AMDesignator = "ç.â";
info.PMDesignator = " .â";
info.ShortDatePattern = "yyyy/MM/dd";
info.FirstDayOfWeek = DayOfWeek.Saturday;
System.Globalization.PersianCalendar cal = new System.Globalization.PersianCalendar();
typeof(System.Globalization.DateTimeFormatInfo).GetField(
    "calendar", System.Reflection.BindingFlags.Public |
    System.Reflection.BindingFlags.Instance |
    System.Reflection.BindingFlags.NonPublic).SetValue(info, cal);

object obj = typeof(System.Globalization.DateTimeFormatInfo).GetField(
    "m_cultureTableRecord", System.Reflection.BindingFlags.Public |
    System.Reflection.BindingFlags.Instance |
    System.Reflection.BindingFlags.NonPublic).GetValue(info);

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

typeof(System.Globalization.CultureInfo).GetField("calendar",
    System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance |
    System.Reflection.BindingFlags.NonPublic).SetValue(SelectedCulture, cal);

System.Threading.Thread.CurrentThread.CurrentCulture = SelectedCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = SelectedCulture;
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat = info;
System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat = info;
break;

default:
System.Threading.Thread.CurrentThread.CurrentCulture = SelectedCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = SelectedCulture;
break;
}
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)