將您的應用程式設計為能夠應對全球市場,這可以通過適當格式化日期、時間、數字、電話號碼和貨幣來實現。 您稍後將能夠針對全球市場中的其他文化特性、區域和語言,調整您的應用程式。
簡介
當您建立應用程式時,如果您從更廣泛的語言和文化角度考量,那麼當您的應用程式拓展到新市場時,您將會較少遇到非預期問題。 例如,日期、時間、數字、行事曆、貨幣、電話號碼、度量單位和紙張大小都是在不同文化或語言中顯示方式可能不同的項目。
不同的地區和文化使用不同的日期和時間格式。 這些包括日期中日和月份順序的慣例、時間中的小時和分鐘的分隔,甚至是用作分隔符的標點符號。 此外,日期可能以各種長格式顯示(“2012年3月28日星期三”)或短格式(“3/28/12”),不同文化特性而異。 當然,星期和月份的名稱及縮寫在不同語言之間有所不同。
您可以預覽用於不同語言的格式。 移至 [設定>時間與語言>],然後按兩下 [其他日期、時間] 和 [區域設定>][變更日期、時間或數位格式]。 在 [
本主題使用「使用者配置檔語言清單」、「應用程式指令清單語言清單」和「應用程式運行時間語言清單」等詞彙。 如需這些字彙的意義和如何存取其值的詳細數據,請參閱 瞭解使用者設定檔語言和應用程式指令清單語言。
格式化應用程式運行時間語言清單的日期和時間
如果您需要允許使用者選擇日期或選擇時間,請使用標準 行事曆、日期和時間控制件。 這些會自動使用應用程式運行時間語言清單的最佳日期和時間格式。
如果您需要自行顯示日期或時間,您可以使用 DateTimeFormatter 類別。 根據預設,DateTimeFormatter 會自動使用應用程式運行時間語言清單的最佳日期和時間格式。 因此,下列程式碼會以最適合該清單的方式格式化指定的 DateTime。 例如,假設您的應用程式指令清單語言清單包含英文(美國),這也是您的預設值,以及德文(德國)。 如果目前的日期是 2017 年 11 月 6 日,而使用者配置檔語言清單會先包含德文(德國),則格式器會提供 “06.11.2017”。 如果使用者配置文件語言清單中第一個是美式英文(或不包含英文或德文),則格式將顯示「11/6/2017」(因為“en-US”相符,或被用作預設)。
// 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;
您可以在自己的計算機上測試上述程序代碼,如下所示。
- 請確定您的專案中有資源檔符合「en-US」和「de-DE」資格(請參閱 針對語言、縮放比例、高對比度和其他限定符量身打造您的資源)。
- 在 設定>時間與語言>區域與語言>語言中變更您的用戶個人資料語言清單。 新增德文 (德國),使其成為預設值,然後再次執行程序代碼。
格式化使用者配置檔語言清單的日期和時間
請記住,根據預設,DateTimeFormatter 符合應用程式運行時間語言清單。 如此一來,如果您顯示「日期 <日期>」之類的字元串,則語言會符合日期格式。
如果基於您想要根據使用者配置檔語言清單格式化日期和時間的任何原因,則可以使用類似下列範例的程式代碼來執行此動作。 但是,如果您這樣做,則瞭解使用者可以選擇應用程式沒有翻譯字串的語言。 例如,如果您的應用程式沒有提供德國的德語版本,但使用者選擇該語言作為偏好的語言設置,則可能會導致顯示出看似奇怪的字串,例如「日期為 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);
適當地格式化數字和貨幣
不同的文化會以不同的方式表示數字。 格式差異可能包括要顯示的十進位數、要使用哪些字元做為小數分隔符,以及要使用的貨幣符號。 使用 NumberFormatting 命名空間中的類別來顯示十進位、百分比或千分之一的數位和貨幣。 在大部分情況下,您會希望這些格式器類別使用使用者配置檔的最佳格式。 但您可以使用格式器來顯示任何區域或格式的貨幣。
此範例示範如何根據使用者設定和特定貨幣系統顯示貨幣。
// 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;
您可以透過在 [設定>時間與語言>區域與語言>國家或地區] 中變更國家或地區來在您的電腦上測試上述程式碼。 選擇國家或地區(也許冰島),然後再次執行程序代碼。
使用文化上適當的行事曆
行事歷會因區域和語言而異。 公曆不是每個區域的預設值。 某些區域中的使用者可能會選擇替代行事曆,例如日本紀元歷或阿拉伯曆法。 行事曆上的日期和時間也會受到不同時區和日光節約時間制的影響。
為了確保使用慣用的行事曆格式,您可以使用標準 行事曆、日期和時間控件。 針對更複雜的案例,可能需要直接處理行事曆日期的作業,Windows.Globalization 提供一個 Calendar 類別,為指定的文化特性、區域和行事曆類型提供適當的行事曆表示法。
適當地格式化電話號碼
不同地區的電話號碼格式不同。 數字數目、數位分組方式,電話號碼特定部分的意義會因國家/地區而異。 從 Windows 10 版本 1607 開始,您可以使用 PhoneNumberFormatting 命名空間中的類別來適當地格式化目前區域的電話號碼。
PhoneNumberInfo 剖析數位字元串,並可讓您:判斷數位是否為目前區域中的有效電話號碼;比較兩個數位是否相等;和 以擷取電話號碼的不同功能部分,例如國家/地區代碼或地理區域代碼。
PhoneNumberFormatter 能格式化數字串或 PhoneNumberInfo 以供顯示,即使數字串僅代表部分電話號碼時也能正確處理。 您可以使用這個部分數字格式,隨著使用者輸入數字時即時格式化該數字。
下列範例示範如何使用 PhoneNumberFormatter,在輸入電話號碼時格式化電話號碼。 每次 TextBox 名為 phoneNumberInputTextBox 的文字變更時,文本框的內容都會使用目前的預設區域格式化,並顯示在名為 phoneNumberOutputTextBlock 的 TextBlock 中。 為了示範目的,字串也會使用紐西蘭的區域格式化,並顯示在名為 phoneNumberOutputTextBlockNZ 的 TextBlock 中。
using Windows.Globalization.PhoneNumberFormatting;
PhoneNumberFormatter currentFormatter, NZFormatter;
public MainPage()
{
this.InitializeComponent();
// 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);
}
}
您可以透過在 [設定>時間與語言>區域與語言>國家或地區] 中變更國家或地區來在您的電腦上測試上述程式碼。 選擇國家或地區(也許紐西蘭確認格式相符),然後再次執行程序代碼。 針對測試數據,您可以對紐西蘭的商務電話號碼進行網路搜尋。
用戶的語言和文化喜好設定
如果您想要根據使用者的語言、區域或文化喜好設定提供不同的功能,Windows 可讓您透過 windows.System.UserProfile.GlobalizationPreferences