Globalization Basics for Windows Phone 7

 

If you had a chance to look at our previous post, then you already know a little about Globalization. In this post, I am going to show you a hands-on approach of what this really means.

By now you know that Globalization is not about the language of your application, but about the way the application behaves according to the settings applied by your environment, for example the Operating System. These include date, time, currency, numbers, calendar, sort orders, etc.

Note: Managed applications are Globalized by default (e.g. dates are formatted according to the user locale even if you don’t invoke any of the Globalization classes). Still, there are useful Globalization capabilities in the .NET Framework, in case you want to use them explicitly.

So, how does an application like this…

 

end up like this, with Globalization in mind.

 

 

The first thing you should know about is the NameSpace System.Globalization. Straight from the MSDN Library definition,

The System.Globalization namespace contains classes that define culture-related information, including language, country/region, calendars in use, format patterns for dates, currency, and numbers, and sort order for strings. < More >

 

System.Globalization includes the CultureInfo Class, and it is defined in the MSDN Library as,

Provides information about a specific culture, such as the names of the culture, the writing system, the calendar used, and how to format dates and sort strings. < More >

 

Now that we have these valuable bits of information, we can make some simple additions to any piece of .NET Framework code to begin globalizing applications. In this case, we use the Windows Phone 7 application shown above as the example.

 

Start by setting the culture. In the first image I have selected Spain.

 

                CultureInfo newCulture = new CultureInfo(culture);

                Thread.CurrentThread.CurrentCulture = newCulture;

 

Here, culture = es-ES for Spanish Spain, and it is set by clicking on the country name Spain.

 

Now that I have set the culture of the application thread (note that I need to use System.Threading to do this), I can now format the Time and Date according to the locale set to the thread (Spain).

 

                myDT = DateTime.Today;

                myDT2 = DateTime.Now;

 

                tbDate.Text = myDT.ToString("D");

                tbTime.Text = myDT2.ToString("T");

 

D = Long Date Pattern

T = Long Time Pattern

More on formatting strings here.

 

And just like that, I have globalized the application. Now every time I select a different locale, I will automatically get the Date and Time according to the locale defined by the country selected.

 

Note: Because Windows Phone 7 version 1 only supports a subset of all the locales in the .NET Framework, will want to add some error detection code to your application.