Creating multi-language websites – managing dates

I am currently work on a project which has many DateTime fields in its database that needs to be shown to the end-user; but, in a correct format. Unfortunately, most websites, which I have visited lately, didn’t manage date formats. For instance, if the website’s current language is Persian, the date format should be different. Persians are using Shamsi calendar notwithstanding the fact that Gregorian calendar is used by many other countries including England and United States. So, if the user changed the website’s language to another, which has special calendar (Like Persian), date formats also have to be changed.

There are several ways to manage this issue; but, here is my opinion:

Scenario

I have a website which supports two languages: English and Persian. When the user changed the website’s language to Persian, all dates in that website have to be changed to Shamsi; also, if the user switched back to English, dates must be shown in Gregorian format.

Note: My database stores all dates in datetime fields; so, if you want to use this solution in your own applications, you have to do the same thing. I say this because I have seen some developers who are storing dates in integer data types or some others who store Persian dates instead of standards dates in their databases.

The solution

Since extension methods were introduced by Microsoft, I have used them in nearly all of my projects to achieve different kinds of approaches. I just love them because they give me ability to extend .NET APIs and classes in just the way I want. As a result, I preferred using them in this solution too.

The first thing you need is a method for converting Gregorian dates to Persian. As you know, this would be a very simple method:

private static string ConvertToPersian(DateTime dt) {
    var pc = new PersianCalendar();
return string.Format("{0}/{1}/{2}", pc.GetYear(dt), pc.GetMonth(dt).ToString("00"), pc.GetDayOfMonth(dt).ToString("00")); }

Then, you need to create an extension method for DateTime object. I will name it “Localized” because what this method does, is converting the specified date to the correct format according to the website’s language:

public static string Localized(this DateTime dt) {
    var currentCulture = Thread.CurrentThread.CurrentCulture;
if (currentCulture.Name.Equals("fa-IR", StringComparison.OrdinalIgnoreCase)) { return ConvertToPersian(dt); } else { return dt.ToShortDateString(); } }

As you can see in the above code, the website’s current culture has been retrieved from System.Threading.Thread.CurrentThread.CurrentCulture property; consequently, as the above code, you can get the website’s current language and return the correct format of date.

After the implementation of this extension method in an application, the “Localized” method can be accessed in all DateTime objects. The following example demonstrates the use of this method:

static void Main(string[] args) {
    Console.WriteLine("English date: {0}", DateTime.Now.Localized());

// Changing system language to persian. var culture = new CultureInfo("fa-IR"); Thread.CurrentThread.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = culture; Console.WriteLine("Persian date: {0}", DateTime.Now.Localized()); Console.ReadLine(); }

And the result would be something like this:

LocalizedMethodSample

 

Hope it helps.