全球化您的日期/時間/數字格式Globalize your date/time/number formats

藉由為日期、時間、數字、電話號碼及貨幣進行適當的格式設定,將您的應用程式設計為可全球通用。Design your app to be global-ready by appropriately formatting dates, times, numbers, phone numbers, and currencies. 您的應用程式在將來便可因應全球市場中的其他文化特性、地區及語言。You'll then be able later to adapt your app for additional cultures, regions, and languages in the global market.


在建立您的應用程式時,若您思考的範圍超越單一語言和文化,則當您的應用程式成長進入新的市場時,便會遭遇到更少未預期的問題 (若有的話)。When creating your app, if you think more broadly than a single language and culture then you'll have fewer (if any) unexpected issues when your app grows into new markets. 例如,日期、時間、數字、行事曆、貨幣、電話號碼、度量單位和紙張大小等項目,全部都可以根據不同的文化特性或語言以不同方式顯示。For example, dates, times, numbers, calendars, currency, telephone numbers, units of measurement, and paper sizes are all items that can be displayed differently in different cultures or languages.

不同的地區和文化使用不同的日期和時間格式。Different regions and cultures use different date and time formats. 這包含使用不同的慣例來排列日期的日、月順序、時間的小時和分鐘區隔方式,即使是做為分隔符號的標點符號也會有所不同。These include conventions for the order of day and month in the date, for the separation of hours and minutes in the time, and even for what punctuation is used as a separator. 此外,日期可能會因為不同文化而顯示為各種長格式 (「Wednesday, March 28, 2012」) 或短格式 (「3/28/12」)。In addition, dates may be displayed in various long formats ("Wednesday, March 28, 2012") or short formats ("3/28/12"), which vary across cultures. 不同語言之間的星期幾與月份的名稱和縮寫當然也會有所不同。And, of course, the names and abbreviations for the days of the week and months of the year differ between languages.

您可以預覽不同語言使用的格式。You can preview the formats used for different languages. 移至 [設定] [ > 時間] & [語言 > 地區] & 語言],然後按一下 [其他日期]、[時間]、[& 地區設定] > 變更日期、時間或數位格式Go to Settings > Time & Language > Region & language, and click Additional date, time, & regional settings > Change date, time, or number formats. [格式] 索引標籤上,從 [格式] 下拉式功能表中選取語言,然後在 [範例] 中預覽格式。On the Formats tab, select a language from the Format drop-down and preview the formats in Examples.

此主題會使用「使用者設定檔語言清單」、「應用程式資訊清單語言清單」及「應用程式執行階段語言清單」等三個術語。This topic uses the terms "user profile language list", "app manifest language list", and "app runtime language list". 如需取得這些術語意義和存取其值的詳細資訊,請參閱了解使用者設定檔語言和應用程式資訊清單語言For details on exactly what those terms mean and how to access their values, see Understand user profile languages and app manifest languages.

為應用程式執行階段語言清單格式化日期和時間Format dates and times for the app runtime language list

如果需要讓使用者選擇日期或選取時間,請使用標準日曆、日期和時間控制項If you need to allow users to choose a date, or to select a time, then use the standard calendar, date, and time controls. 這些會自動使用最適合應用程式執行階段語言清單的日期和時間格式。These automatically use the best date and time format for the app runtime language list.

若您需要自行顯示日期或時間,您可以使用 DateTimeFormatter 類別。If you need to display dates or times yourself then you can use the DateTimeFormatter class. 根據預設,DateTimeFormatter 會自動使用最適合應用程式執行階段語言清單的日期和時間格式。By default, DateTimeFormatter automatically uses the best date and time format for the app runtime language list. 因此,以下程式碼會將特定 DateTime 格式化為最適合該清單的格式。So, the code below formats a given DateTime in the best way for that list. 例如,假設您的應用程式資訊清單語言清單包含您預設語言的英文 (美國) 和德文 (德國)。As an example, assume that your app manifest language list includes English (United States), which is also your default, and German (Germany). 若目前的日期是 2017 年 11 月 6 日,使用者設定檔語言清單首先包含德文 (德國),則格式器將為 "06.11.2017"。If the current date is Nov 6 2017 and the user profile language list contains German (Germany) first, then the formatter gives "06.11.2017". 若使用者設定檔語言清單首先包含英文 (美國) (或並未包含英文或德文),則格式器將為 "11/6/2017" (因為 "en-US" 符合,或作為預設值使用)。If the user profile language list contains English (United States) first (or if it contains neither English nor German), then the formatter gives "11/6/2017" (since "en-US" matches, or is used as the default).

    // Use the DateTimeFormatter class to display dates and times using basic formatters.

    var shortDateFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("shortdate");
    var shortTimeFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("shorttime");

    var dateTimeToFormat = DateTime.Now;

    var shortDate = shortDateFormatter.Format(dateTimeToFormat);
    var shortTime = shortTimeFormatter.Format(dateTimeToFormat);

    var results = "Short Date: " + shortDate + "\n" +
                  "Short Time: " + shortTime;

您可以在您自己的電腦上以這種方式測試。You can test the code above on your own PC like this.

為使用者設定檔語言清單格式化日期和時間Format dates and times for the user profile language list

請記住,根據預設,DateTimeFormatter 會比對應用程式執行階段語言清單。Remember that, by default, DateTimeFormatter matches the app runtime language list. 如此一來,若您顯示字串,例如「日期是 <date>」,則語言便會符合日期格式。That way, if you display strings such as "The date is <date>", then the language will match the date format.

若您因為任何理由,想要只根據使用者設定檔語言清單格式化日期和/或時間,您可以使用程式碼執行此作葉,如以下範例所示。If for whatever reason you want to format dates and/or times only according to the user profile language list, then you can do that using code like the example below. 但是若您決定執行此作業,請了解使用者可能會選擇您的應用程式不具備翻譯字串的語言。But if you do so then understand that the user can choose a language for which your app doesn't have translated strings. 例如,若您的應用程式並未當地語系化成德文 (德國),但使用者選擇該語言作為其慣用語言,這可能會導致奇怪的字串顯示結果,例如「日期是 06.11.2017」。For example, if your app is not localized into German (Germany), but the user chooses that as their preferred language, then that could result in the display of arguably odd-looking strings such as "The date is 06.11.2017".

    // Use the DateTimeFormatter class to display dates and times using basic formatters.

    var userLanguages = Windows.System.UserProfile.GlobalizationPreferences.Languages;

    var shortDateFormatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("shortdate", userLanguages);

    var results = "Short Date: " + shortDateFormatter.Format(DateTime.Now);

以適當方式格式化數字及貨幣Format numbers and currencies appropriately

不同文化會以不同方式來格式化數字。Different cultures format numbers differently. 格式的差異可能是要顯示到小數第幾位、小數分隔符號要使用哪個字元,以及要使用哪個貨幣符號。Format differences may include how many decimal digits to display, what characters to use as decimal separators, and what currency symbol to use. 使用 NumberFormatting 命名空間中的類別來顯示小數點、百分比或千分比數字及貨幣。Use classes in the NumberFormatting namespace to display decimal, percent, or permille numbers, and currencies. 在大多數的時候,您會希望這些格式器類別使用最適合使用者設定檔的格式。Most of the time, you will want these formatter classes to use the best format for the user profile. 但您也可以使用格式器來顯示任何區域或格式的貨幣。But you may use the formatters to display a currency for any region or format.

這個範例顯是如何根據使用者設定檔和給定的特定貨幣系統顯示貨幣。This example shows how to display currencies both per the user profile, and for a specific given currency system.

    // This scenario uses the CurrencyFormatter class to format a number as a currency.

    var userCurrency = Windows.System.UserProfile.GlobalizationPreferences.Currencies[0];

    var valueToBeFormatted = 12345.67;

    var userCurrencyFormatter = new Windows.Globalization.NumberFormatting.CurrencyFormatter(userCurrency);
    var userCurrencyValue = userCurrencyFormatter.Format(valueToBeFormatted);

    // Create a formatter initialized to a specific currency,
    // in this case US Dollar (specified as an ISO 4217 code) 
    // but with the default number formatting for the current user.
    var currencyFormatUSD = new Windows.Globalization.NumberFormatting.CurrencyFormatter("USD");
    var currencyValueUSD = currencyFormatUSD.Format(valueToBeFormatted);

    // Create a formatter initialized to a specific currency.
    // In this case it's the Euro with the default number formatting for France.
    var currencyFormatEuroFR = new Windows.Globalization.NumberFormatting.CurrencyFormatter("EUR", new[] { "fr-FR" }, "FR");
    var currencyValueEuroFR = currencyFormatEuroFR.Format(valueToBeFormatted);

    // Results for display.
    var results = "Fixed number (" + valueToBeFormatted + ")\n" +
                    "With user's default currency: " + userCurrencyValue + "\n" +
                    "Formatted US Dollar: " + currencyValueUSD + "\n" +
                    "Formatted Euro (fr-FR defaults): " + currencyValueEuroFR;

您可以在您自己的電腦上測試上述程式碼,方法是變更 [設定時間] 中的國家或地區, > & 語言 > 地區] & [語言] [ > 國家或地區]。You can test the code above on your own PC by changing the country or region in Settings > Time & Language > Region & language > Country or region. 選擇國家或地區 (假設是冰島),然後重新執行程式碼。Choose a country or region (perhaps Iceland), and run the code again.

使用符合當地文化的日曆Use a culturally appropriate calendar

不同地區及語言的行事曆也有所不同。The calendar differs across regions and languages. 公曆 (西曆) 不是每個地區的預設行事曆。The Gregorian calendar is not the default for every region. 有些地區的使用者可能會選擇其他日曆,像是日本年號年曆或阿拉伯陰曆。Users in some regions may choose alternate calendars, such as the Japanese era calendar, or Arabic lunar calendars. 不同的時區及日光節約時間也會對日曆上的日期和時間有顯著影響。Dates and times on the calendar are also sensitive to different time zones and daylight-saving time.

若要確保使用的是慣用日曆格式,您可以使用標準日曆、日期和時間控制項To ensure that the preferred calendar format is used, you can use the standard calendar, date, and time controls. 如果遇到更複雜的案例,需要在日曆日期上直接使用操作,Windows.Globalization 可以提供 Calendar 類別,對特定文化、地區及日曆類型,提供適當的日曆表示法。For more complex scenarios, where working directly with operations on calendar dates may be required, Windows.Globalization provides a Calendar class that gives an appropriate calendar representation for the given culture, region, and calendar type.

以適當方式格式化電話號碼Format phone numbers appropriately

不同地區格式化電話號碼的方式也有所不同。Phone numbers are formatted differently across regions. 數字位數、數字分組方式及電話號碼特定部分的意義,都會隨每個國家/地區而有所不同。The number of digits, how the digits are grouped, and the significance of certain parts of the phone number vary between countries. 從 Windows 10 版本 1607 開始,您可以使用 PhoneNumberFormatting 命名空間中的類別來針對目前地區適當地格式化電話號碼。Starting in Windows 10, version 1607, you can use classes in the PhoneNumberFormatting namespace to format phone numbers appropriately for the current region.

PhoneNumberInfo 會剖析數字字串,並可讓您:判斷數字是否為目前地區中的有效電話號碼、比較兩個號碼是否相等、並擷取電話號碼不同的功能部分,例如國碼 (地區碼)。PhoneNumberInfo parses a string of digits and allows you to: determine whether the digits are a valid phone number in the current region; compare two numbers for equality; and to extract the different functional parts of the phone number, such as country code or geographical area code.

PhoneNumberFormatter會格式化數位的字串或PhoneNumberInfo以顯示,即使數位的字串代表部分電話號碼也一樣。PhoneNumberFormatter formats a string of digits or a PhoneNumberInfo for display, even when the string of digits represents a partial phone number. 您可以使用此部分的號碼格式,在使用者輸入號碼時格式化號碼。You can use this partial number formatting to format a number as a user is entering the number.

以下範例示範如何使用 PhoneNumberFormatter,在輸入電話號碼時格式化電話號碼。The example below shows how to use PhoneNumberFormatter to format a phone number as it is being entered. 每當名為 phoneNumberInputTextBox 的 TextBox 內有文字變更時,就會使用目前預設地區設定來格式化文字方塊內容,並顯示在名為 phoneNumberOutputTextBlock 的 TextBlock 中。Each time text changes in a TextBox named phoneNumberInputTextBox, the contents of the text box are formatted using the current default region and displayed in a TextBlock named phoneNumberOutputTextBlock. 為了便於示範,字串也使用紐西蘭的地區設定格式化,並顯示在名為 phoneNumberOutputTextBlockNZ 的 TextBlock 中。For demonstration purposes, the string is also formatted using the region for New Zealand, and displayed in a TextBlock named phoneNumberOutputTextBlockNZ.

    using Windows.Globalization.PhoneNumberFormatting;

    PhoneNumberFormatter currentFormatter, NZFormatter;

    public MainPage()

        // Use the default formatter for the current region
        this.currentFormatter = new PhoneNumberFormatter();

        // Create an explicit formatter for New Zealand. 
        PhoneNumberFormatter.TryCreate("NZ", out this.NZFormatter);

    private void phoneNumberInputTextBox_TextChanged(object sender, TextChangedEventArgs e)
        // Format for the default region.
        this.phoneNumberOutputTextBlock.Text = currentFormatter.FormatPartialString(this.phoneNumberInputTextBox.Text);

        // If the NZFormatter was created successfully, format the partial string for the NZ TextBlock.
        if(this.NZFormatter != null)
            this.phoneNumberOutputTextBlockNZ.Text = this.NZFormatter.FormatPartialString(this.phoneNumberInputTextBox.Text);

您可以在您自己的電腦上測試上述程式碼,方法是變更 [設定時間] 中的國家或地區, > & 語言 > 地區] & [語言] [ > 國家或地區]。You can test the code above on your own PC by changing the country or region in Settings > Time & Language > Region & language > Country or region. 選擇國家或地區 (假設是紐西蘭,以確認該格式相符),然後重新執行程式碼。Choose a country or region (perhaps New Zealand to confirm that the formats match), and run the code again. 如需取得測試資料,您可以使用 Web 搜尋位於紐西蘭公司的電話號碼。For test data, you can do a web search for the phone number of a business in New Zealand.

使用者語言及文化喜好設定The user's language and cultural preferences

如果您想要只根據使用者的語言、地區或文化喜好設定提供不同功能,Windows 可以讓您透過 Windows.System.UserProfile.GlobalizationPreferences 存取這些喜好設定。For scenarios where you wish to provide different functionality based solely on the user's language, region, or cultural preferences, Windows gives you a way to access those preferences, through Windows.System.UserProfile.GlobalizationPreferences. 如有需要,可以使用 GlobalizationPreferences 類別取得使用者目前地理區域、慣用語言、慣用貨幣等項目的值。When needed, use the GlobalizationPreferences class to get the value of the user's current geographic region, preferred languages, preferred currencies, and so on. 但請記得,若您應用程式的字串/影像並未針對使用者的慣用語言進行當地語系化,則日期、時間和其他為該慣用語言格式化的資料可能不會與您顯示的字串相符。But remember that if your app's strings/images aren't localized for the user's preferred language then dates and times and other data formatted for that preferred language won't match the strings that you display.

重要 APIImportant APIs