共用方式為


快速入門:使用全球通用格式 (HTML)

[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]

以適當方式格式化日期、時間、數字以及貨幣,將您的應用程式設計成全球通用的應用程式。這種做法能讓您的應用程式在將來適應全球市場中其他文化特性、地區及語言。

簡介

Windows 的使用遍及世界各地不同的市場,而且擁有許多不同文化特性、地理區域或語言的客戶。很多應用程式開發人員都會很自然地根據自己的語言和文化思考來建立應用程式。但是,當應用程式開始跨足其他市場時,調整應用程式以適應新語言和新區域就可能會遭遇意外的困難。 例如,日期、時間、數字、行事曆、貨幣、電話號碼、度量單位和紙張大小等項目,都有可能根據不同文化或語言以不同方式顯示。

一開始設計應用程式時多想一想,就可以簡化適應新市場的程序。 設計應用程式時遵循下列指導原則,讓應用程式可以全球通用。

先決條件

針對全球市場進行規劃

工作

  1. 以適當方式格式化日期和時間。

    有許多方法可以正確顯示日期和時間。不同的區域與文化會使用不同的慣例來排列日期的日、月順序、時間的小時和分鐘區隔方式,即使是做為分隔符號的標點符號也會有所不同。 此外,日期可能會因為不同文化而顯示為各種長格式 ("Wednesday, March 28, 2012") 或短格式 ("3/28/12")。 每種語言的星期幾與月份的名稱和縮寫當然也會有所不同。

    如果需要讓使用者選擇日期或選取時間,請使用標準日期和時間選擇器控制項。這些控制項會自動使用使用者慣用語言及區域的日期和時間格式。

    如果需要自行顯示日期或時間,請使用 Date/TimeNumber 格式器,自動以使用者慣用的格式顯示日期、時間及數字。以下程式碼將使用目前的慣用語言和區域,來格式化指定的 DateTime。例如,假設目前日期是 2012 年 6 月 3 日。如果使用者慣用英文 (美國),則格式器會產生 "6/3/2012";但如果使用者慣用德文 (德國),則會產生 "03.06.2012":

    // Use the Windows.Globalization.DateTimeFormatting.DateTimeFormatter class
    // to display dates and times using basic formatters.
    
    // Formatters for dates and times, using shortdate format.
    var sdatefmt = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("shortdate");
    var stimefmt = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("shorttime");
    
    // Obtain the date that will be formatted.
    var dateToFormat = new Date();
    
    // Perform the actual formatting.
    var sdate = sdatefmt.format(dateToFormat);
    var stime = stimefmt.format(dateToFormat);
    
    // Results for display.
    var results = "Short Date: " + sdate + "\n" +
                  "Short Time: " + stime;
    
  2. 以適當方式格式化數字及貨幣。

    不同文化會以不同方式來格式化數字。格式的差異可能是要顯示到小數第幾位、小數分隔符號要使用哪個字元,以及要使用哪個貨幣符號。使用 NumberFormatting 來顯示小數點、百分比或千分比數字及貨幣。 在大部分情況下,您只要根據使用者目前的喜好設定來顯示數字或貨幣即可。 不過您也可以使用格式器來顯示特定區域或格式的貨幣。

    以下程式碼提供的範例是如何依照使用者慣用的語言及區域顯示貨幣,或針對特定貨幣系統顯示貨幣:

    // This scenario uses the Windows.Globalization.NumberFormatting.CurrencyFormatter class
    // to format a number as a currency.
    
    // Determine the current user's default currency.
    var userCurrency = Windows.System.UserProfile.GlobalizationPreferences.currencies;
    
    // Number to be formatted.
    var fractionalNumber = 12345.67;
    
    // Currency formatter using the current user's preference settings for number formatting.
    var userCurrencyFormat = new Windows.Globalization.NumberFormatting.CurrencyFormatter(userCurrency);
    var currencyDefault = userCurrencyFormat.format(fractionalNumber);
    
    // 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 currencyUSD = currencyFormatUSD.format(fractionalNumber);
    
    // 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", ["fr-FR"], "FR");
    var currencyEuroFR = currencyFormatEuroFR.format(fractionalNumber);
    
    // Results for display.
    var results = "Fixed number (" + fractionalNumber + ")\n" +
                  "With user's default currency: " + currencyDefault + "\n" +
                  "Formatted US Dollar: " + currencyUSD + "\n" +
                  "Formatted Euro (fr-FR defaults): " + currencyEuroFR;
    
  3. 使用符合當地文化的行事曆。

    不同區域及語言的行事曆也有所不同。公曆 (西曆) 不是每個地區的預設行事曆。有些地區的使用者可能會選擇其他行事曆,像是日本年號年曆或阿拉伯陰曆。不同的時區及日光節約時間也會對行事曆上的日期和時間有顯著影響。

    使用標準日期和時間選擇器控制項可讓使用者選擇日期,確保使用慣用的行事曆格式。 如果遇到更複雜的案例,需要在行事曆日期上直接使用操作,Windows.Globalization 可以提供 Calendar 類別,針對特定文化、區域及行事曆類型,提供適當的行事曆表示法。

  4. 尊重使用者的語言及文化喜好設定。

    如果要根據使用者的語言、地區或文化喜好設定提供不同功能,Windows 可以讓您透過 Windows.System.UserProfile.GlobalizationPreferences 存取這些喜好設定。 如有需要,可以使用 GlobalizationPreferences 類別取得使用者目前地理區域、慣用語言、慣用貨幣等項目的值。

相關主題

新增 DatePicker 和 TimePicker

全球化應用程式的指導方針和檢查清單

針對全球市場進行規劃

參考

Windows.Globalization.Calendar

Windows.Globalization.DateTimeFormatting

Windows.Globalization.NumberFormatting

Windows.System.UserProfile.GlobalizationPreferences

範例

行事曆詳細資料及數學範例

日期和時間格式化範例

全球化喜好設定範例

數字格式化及剖析範例