How to Write World Ready Windows Phone Applications
Introduction
Windows Phone has built-in support to build world ready applications. This is because of existing support for globalization and localization offered by Silverlight.
Before we dig into how we can build a globalized Windows Phone application, let us take a minute to understand what it means to be a truly globalized application.
A globalized application is one that can adapt to user's preference for date format, number format, etc. Built-in support for .NET Framework ensures that globalization is a relatively easy task for a Windows Phone application developer.
Globalization Support in Windows Phone
Windows Phone 7.5 "Mango " ( also referred to as Windows Phone OS 7.1) supports displaying in 22 cultures (16 more than the first version of Windows Phone (Windows Phone 7.0). The supported display cultures are - Chinese Simplified (zh-CN), Chinese Traditional (zh-TW), Czech (cs-CZ), Danish (da-DK), Dutch (nl-NL), English US (en-US), English UK, (en-GB), Finnish (fi-FI), French (fr-FR), German (de-DE), Greek (el-GR), Hungarian (hu-HU), Italian (it-IT), Japanese (ja-JP), Korean (ko-KR), Norwegian (nb-NO), Polish (pl-PL), Portuguese (pt-BR), Russian (ru-RU), Spanish (es-ES) and Swedish (sv-SE).
For sorting and formatting (for numbers, currency, time and date), application developers can piggyback on the CultureInfo class to use the built-in support, which already exists inside the .NET Framework.
Since Windows Phone platform distinguishes between Reading Fonts and UI Fonts, it is imperative that application developers design and test their application using both the fonts to ensure the application works properly. This is especially important in the Eastern Asian languages where the UI font is different from the Reading font.
To ensure that the character set is preserved when using the WebBrowser control, application developers need to include the right encoding metadata.
To properly represent the text in a WebBrowser control 昧昨昪昪, we need to represent it as below.
string content = <html><head><meta content="text/html; charset =utf-16"/></head><body>昧昨昪昪</body></html>;
Hands On
Let us create a very simple application, which support two locales (en-US and en-GB, which stands for US English and UK English).
Create a new Visual Studio Silverlight for Windows Phone project and call the solution WindowsPhoneGlobAppDemo.
Now in the default XAML page generated, add a couple of buttons (title them US and UK) and a couple of textblock and text boxes. Label each pair- one for date and another for currency).
In our simple globalized application, we will make sure that the controls representing date and currency are able to render the date and currency values in the right format depending on the region selected.
Now on the click event for the US button, add the following code
private void button1_Click(object sender, RoutedEventArgs e) { CultureInfo currentCulture = new CultureInfo("en-US"); Thread.CurrentThread.CurrentCulture = currentCulture; Thread.CurrentThread.CurrentUICulture = currentCulture; UpdateContent(); }
Add a similar code for click event for the UK button; note that the CultureInfo object being generated is for the "en-GB" locale.
private void button2_Click(object sender, RoutedEventArgs e) { CultureInfo currentCulture = new CultureInfo("en-GB"); Thread.CurrentThread.CurrentCulture = currentCulture; Thread.CurrentThread.CurrentUICulture = currentCulture; UpdateContent(); }
Finally, create the helper method UpdateContent whose job is to update the content after formatting it appropriately for the locale.
void UpdateContent() { float currency = float.Parse("100.50"); textBoxCurrency.Text = currency.ToString("c", NumberFormatInfo.CurrentInfo); DateTime date = DateTime.Now; textBoxDate.Text = date.ToShortDateString(); }
Now build and run your application.
If you are running into compilation issues, you can download a working copy of the source code below.
When we run the application and we click on the US button, we notice that the values for the currency and Date fields are formatted per the US locale.
Figure 1: Values for the currency and Date fields are formatted per
the US locale
We have the currency with the "$' prefix and the Date in the mm/dd/yyyy format.
Now, if we click on the UK button, we see...
Figure 2: The currency is now prepended by pound
...that the currency is now prepended by pound. And the date is in DD/MM/YYYY format.
We are able to see that without writing any locate specific code, we were able represent the data in the right format corresponding to the region.
So what really happened?
When we clicked on the US and UK buttons, we asked the current thread to switch its locale. By changing the active locale, the date formatting and currency formatting was automatically changed to represent the currently selected locale. That is how we were able to see pounds and dollar signs.
Summary
In this article, we learned how we can build globalized Windows Phone applications using the CultureInfo class. I hope you have found the information useful.

Comments
There are no comments yet. Be the first to comment!