Click here to Skip to main content
15,878,809 members
Articles / Web Development / ASP.NET

Multi Culture Calendar

Rate me:
Please Sign up or sign in to vote.
2.43/5 (13 votes)
11 Sep 2009CPOL 46.5K   1.4K   24   12
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)


Written By
Software Developer (Senior) Morhipo
Turkey Turkey
System Architect and software developer. Designing and developing infrastructures and frameworks using c#.net for win and web base solutions.
I have more than 10 years work experience in developing and designing.

Comments and Discussions

 
GeneralI don't know how it works! Pin
clickmeplz115-Apr-11 20:13
clickmeplz115-Apr-11 20:13 
GeneralRe: I don't know how it works! Pin
Farshid300316-Apr-11 22:57
Farshid300316-Apr-11 22:57 
Generalhi farshid3003 Pin
Alishafaat3-Sep-10 9:39
Alishafaat3-Sep-10 9:39 
GeneralRe: hi farshid3003 Pin
Farshid300316-Apr-11 23:00
Farshid300316-Apr-11 23:00 
GeneralCode access security Pin
arash.rohani25-Jun-10 5:08
arash.rohani25-Jun-10 5:08 
GeneralRe: Code access security Pin
Farshid300325-Jun-10 8:25
Farshid300325-Jun-10 8:25 
GeneralRe: Code access security Pin
arash.rohani25-Jun-10 9:48
arash.rohani25-Jun-10 9:48 
GeneralRe: Code access security Pin
Farshid300329-Jun-10 5:18
Farshid300329-Jun-10 5:18 
Generalit coulden't work with janus Pin
amin_ramin888610-Aug-08 5:15
amin_ramin888610-Aug-08 5:15 
GeneralRe: it coulden't work with janus Pin
Farshid300311-Sep-09 5:12
Farshid300311-Sep-09 5:12 
GeneralArticle seems incomplete Pin
Rob Graham26-Apr-08 11:05
Rob Graham26-Apr-08 11:05 
GeneralRe: Article seems incomplete Pin
Farshid300311-Sep-09 5:16
Farshid300311-Sep-09 5:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.