How to: Build a Globalized Application for Windows Phone
March 22, 2012
You can use the CultureInfo class to help globalize an application that you create. The guidance and code examples in this topic are based on an end-to-end code sample called Globalization Sample, which can be downloaded at Code Samples for Windows Phone.
using System; using System.Windows.Controls; using Microsoft.Phone.Controls; using System.Globalization; using System.Threading; namespace sdkGlobalizationCS { public partial class MainPage : PhoneApplicationPage { public MainPage() { InitializeComponent(); SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape; BuildApplicationBar(); } private void LocList_SelectedIndexChanged(object sender, SelectionChangedEventArgs e) { // Set the current culture according to the selected locale and display information such as // date, time, currency, etc in the appropriate format. string nl; string cul; nl = locList.SelectedIndex.ToString(); switch (nl) { case "0": cul = "zh-CN"; break; case "1": cul = "zh-TW"; break; case "2": cul = "cs-CZ"; break; case "3": cul = "da-DK"; break; case "4": cul = "nl-NL"; break; case "5": cul = "en-GB"; break; case "6": cul = "en-US"; break; case "7": cul = "fi-FI"; break; case "8": cul = "fr-FR"; break; case "9": cul = "de-DE"; break; case "10": cul = "el-GR"; break; case "11": cul = "hu-HU"; break; case "12": cul = "it-IT"; break; case "13": cul = "ja-JP"; break; case "14": cul = "ko-KR"; break; case "15": cul = "nb-NO"; break; case "16": cul = "pl-PL"; break; case "17": cul = "pt-BR"; break; case "18": cul = "pt-PT"; break; case "19": cul = "ru-RU"; break; case "20": cul = "es-ES"; break; case "21": cul = "sv-SE"; break; default: cul = "en-US"; break; } // set this thread's current culture to the culture associated with the selected locale CultureInfo newCulture = new CultureInfo(cul); Thread.CurrentThread.CurrentCulture = newCulture; CultureInfo cc, cuic; cc = Thread.CurrentThread.CurrentCulture; cuic = Thread.CurrentThread.CurrentUICulture; // display the culture name in the language of the selected locale regionalFrmt.Text = cc.NativeName; // display the culture name in the localized language displayLang.Text = cuic.DisplayName; // display the date formats (long and short form) for the current culuture DateTime curDate = DateTime.Now; longDate.Text = cc.DateTimeFormat.LongDatePattern.ToString() + " " + curDate.ToString("D"); shortDate.Text = cc.DateTimeFormat.ShortDatePattern.ToString() + " " + curDate.ToString("d"); // display the time format (long form) for the current culture longTime.Text = cc.DateTimeFormat.LongTimePattern + " " + curDate.ToString("T"); // display the currency format and currency symbol for the current culture Int64 money = 123456789; currencyFrmt.Text = money.ToString("C"); } } }
You should never hard code the correct format of a Culture. Instead, you should assign a new CultureInfo object to the CurrentCulture property of the current thread. The following code example instantiates a new CultureInfo object that represents the en-US culture. The CurrentCulture and CurrentUICulture properties in the current thread are then set to the new CultureInfo object.
String cul; cul = "en-US"; CultureInfo newCulture = new CultureInfo(cul); Thread.CurrentThread.CurrentCulture = newCulture; Thread.CurrentThread.CurrentUICulture = newCulture;
The following code example assigns the new CurrentCulture and CurrentUICulture objects to the variables cc and cuic, respectively.
You should never develop your own sorting rules in a globalized application. The CultureInfo.Compareinfo property defines how to compare and sort strings for a specific culture. You can use the overloaded Array.Sort method, which references the CultureInfo.CurrentCulture property, for sorting values in your application.
The following code example sorts and a list of strings according to the current culture.
// create a list of strings and use Array.Sort to take advantage of the current culture string[] stringArray = new string[] { "Æble", "Apple", "Znefol" }; Array.Sort(stringArray); sortedStrings.Text = ""; foreach (string s in stringArray) { sortedStrings.Text += s + "\n"; }
You can alternately choose to use the CultureInfo.InvariantCulture property to sort data that will not be displayed directly to users. Using this property guarantees that the data is sorted in a culture-independent format. For more information about InvariantCulture, see Using the InvariantCulture Property. For more information about sorting options, see CompareOptions Enumeration.