Freigeben über


How to build a globalized app for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

You can use the CultureInfo class to help globalize an app. The guidance and code examples in this topic are based on an end-to-end code sample called Globalization Sample.

This topic contains the following sections.

Code example: Globalization Sample

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");


        }
    }
}

Changing the current culture

You should never hard-code the correct format of a culture value. 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.

CultureInfo cc, cuic; 
cc = Thread.CurrentThread.CurrentCulture;
cuic = Thread.CurrentThread.CurrentUICulture;

Displaying the current culture name in the language of the selected locale

The following code example sets the text field of the regionalFrmt textbox to the language of the selected locale. The current culture name will be displayed in the language associated with the current culture.

regionalFrmt.Text = cc.NativeName;

Displaying the current culture name in the localized language

The following code example sets the text field of the displayLang textbox to the localized version of the current culture name. For example, if the localized language is French, then the current culture name will be displayed in French.

displayLang.Text = cuic.DisplayName;

Displaying the date (long and short forms)

The following code example gets the current date and time. Then, it sets the text fields of the longDate and shortDate textboxes to strings that contain the date pattern and the current date.

DateTime curDate = DateTime.Now;
longDate.Text = cc.DateTimeFormat.LongDatePattern.ToString() + " " + curDate.ToString("D");
shortDate.Text = cc.DateTimeFormat.ShortDatePattern.ToString() + "   " + curDate.ToString("d");

Displaying the time (long form)

The following code example sets the text field of the longTime textbox to a string that contains the LongTimePattern and the current time. The string will be formatted according to the time format for the current culture

longTime.Text = cc.DateTimeFormat.LongTimePattern + "   " + curDate.ToString("T");

Displaying the currency format and currency symbol

The following code example sets the text field of the currencyFrmt textbox to the value associated with the variable money. The variable’s ToString() method converts the value to a string that is formatted according to the currency format of the current culture.

Int64 money = 123456789; 
currencyFrmt.Text = money.ToString("C");

Displaying sort order

You should never develop your own sorting rules in a globalized app. 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 app.

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";
}

Alternatively, you can choose to use the CultureInfo..::.InvariantCulture property to sort data that won’t be displayed directly to users. Using this property guarantees that the data is sorted in a culture-independent format. For more info about sorting options, see CompareOptions.

See Also

Reference

CultureInfo

Other Resources

How to build a localized app for Windows Phone 8

Globalization and localization for Windows Phone 8