Click here to Skip to main content
Licence CPOL
First Posted 26 Apr 2008
Views 18,175
Downloads 515
Bookmarked 21 times

Multi Culture Calendar

By | 11 Sep 2009 | Article
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:

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)

About the Author

Farshid3003

Web Developer
Ariyan System Pardaz
Turkey Turkey

Member

Member of Australia Computer Society(ACS).
System Architect and software developer. Lots of time I design and develop some infrastructures and frameworks using c#.net for win and web base solution. I have about 8 years work experience in developing and designing.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralI don't know how it works! Pinmemberclickmeplz120:13 15 Apr '11  
GeneralRe: I don't know how it works! PinmemberFarshid300322:57 16 Apr '11  
Generalhi farshid3003 PinmemberAlishafaat9:39 3 Sep '10  
GeneralRe: hi farshid3003 PinmemberFarshid300323:00 16 Apr '11  
GeneralCode access security Pinmemberarash.rohani5:08 25 Jun '10  
GeneralRe: Code access security PinmemberFarshid30038:25 25 Jun '10  
GeneralRe: Code access security Pinmemberarash.rohani9:48 25 Jun '10  
GeneralRe: Code access security PinmemberFarshid30035:18 29 Jun '10  
Generalit coulden't work with janus Pinmemberamin_ramin88865:15 10 Aug '08  
GeneralRe: it coulden't work with janus PinmemberFarshid30035:12 11 Sep '09  
GeneralArticle seems incomplete PinmemberRob Graham11:05 26 Apr '08  
GeneralRe: Article seems incomplete PinmemberFarshid30035:16 11 Sep '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120528.1 | Last Updated 11 Sep 2009
Article Copyright 2008 by Farshid3003
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid