背水一戰 Windows 10 之 全球化: Demo, 格式化數字 ...
背水一戰 Windows 10 (81) - 全球化
作者:webabcd
介紹
背水一戰 Windows 10 之 全球化
- Demo
- 格式化數字
示例
1、演示全球化的基本應用
Localization/GlobalizationDemo.xaml
<Page x:Class="Windows10.Localization.GlobalizationDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.Localization" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" /> </StackPanel> </Grid> </Page>
Localization/GlobalizationDemo.xaml.cs
/* * 演示全球化的基本應用 * * * 註:本地化和全球化的區別 * 1、全球化的產品應該適用於任何一個本地市場 * 2、本地化通常會有 UI 的調整,語言的翻譯,甚至是針對本地開發的一些特殊的功能 * 3、一個全球化的產品做本地化時,一般只做語言翻譯 */ using System; using Windows.Globalization; using Windows.System.UserProfile; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace Windows10.Localization { public sealed partial class GlobalizationDemo : Page { public GlobalizationDemo() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { // 首選語言 lblMsg.Text = "Current Languages: " + string.Join(", ", GlobalizationPreferences.Languages); lblMsg.Text += Environment.NewLine; // 首選日曆(比如:GregorianCalendar 提供了世界上大多數國家/地區使用的標準日曆系統) lblMsg.Text += "Current Calendars: " + string.Join(", ", GlobalizationPreferences.Calendars); lblMsg.Text += Environment.NewLine; // 時鐘顯示(比如:24HourClock) lblMsg.Text += "Current Clocks: " + string.Join(", ", GlobalizationPreferences.Clocks); lblMsg.Text += Environment.NewLine; // 區域(比如:CN) lblMsg.Text += "Current HomeGeographicRegion: " + GlobalizationPreferences.HomeGeographicRegion; lblMsg.Text += Environment.NewLine; // 一周的第一天是周幾(比如:中國是 Monday) lblMsg.Text += "Current WeekStartsOn: " + GlobalizationPreferences.WeekStartsOn.ToString(); lblMsg.Text += Environment.NewLine; lblMsg.Text += Environment.NewLine; // Language - 語言對象,通過指定 BCP-47 語言標記來實例化語言對象 Windows.Globalization.Language language = new Windows.Globalization.Language("zh-Hans-CN"); // 判斷指定的 BCP-47 語言標記的格式是否正確 lblMsg.Text += "zh-Hans-CN IsWellFormed: " + Windows.Globalization.Language.IsWellFormed("zh-Hans-CN"); lblMsg.Text += Environment.NewLine; // 語言的本地化名稱 lblMsg.Text += "zh-Hans-CN Language DisplayName: " + language.DisplayName; lblMsg.Text += Environment.NewLine; // 語言本身的名稱 lblMsg.Text += "zh-Hans-CN Language NativeName: " + language.NativeName; lblMsg.Text += Environment.NewLine; // 語言的 BCP-47 語言標記 lblMsg.Text += "zh-Hans-CN Language LanguageTag: " + language.LanguageTag; lblMsg.Text += Environment.NewLine; // 語言的 ISO 15924 腳本代碼 lblMsg.Text += "zh-Hans-CN Language Script: " + language.Script; lblMsg.Text += Environment.NewLine; // 獲取當前輸入法編輯器 (IME) 的 BCP-47 語言標記 lblMsg.Text += "zh-Hans-CN Language CurrentInputMethodLanguageTag: " + Windows.Globalization.Language.CurrentInputMethodLanguageTag; lblMsg.Text += Environment.NewLine; lblMsg.Text += Environment.NewLine; // GeographicRegion - 區域對象(關於 ISO 3166-1 請參見:http://zh.wikipedia.org/zh-cn/ISO_3166-1) GeographicRegion geographicRegion = new GeographicRegion(); // 獲取當前的區域對象。 // 區域的本地化名稱 lblMsg.Text += "Current Region DisplayName: " + geographicRegion.DisplayName; lblMsg.Text += Environment.NewLine; // 區域本身的名稱 lblMsg.Text += "Current Region NativeName: " + geographicRegion.NativeName; lblMsg.Text += Environment.NewLine; // 該區域內使用的貨幣類型 lblMsg.Text += "Current Region CurrenciesInUse: " + string.Join(",", geographicRegion.CurrenciesInUse); lblMsg.Text += Environment.NewLine; // 該區域的 ISO 3166-1 二位字母標識 lblMsg.Text += "Current Region CodeTwoLetter: " + geographicRegion.CodeTwoLetter; lblMsg.Text += Environment.NewLine; // 該區域的 ISO 3166-1 三位字母標識 lblMsg.Text += "Current Region CodeThreeLetter: " + geographicRegion.CodeThreeLetter; // 該區域的 ISO 3166-1 數字標識 lblMsg.Text += Environment.NewLine; lblMsg.Text += "Current Region CodeThreeDigit: " + geographicRegion.CodeThreeDigit; lblMsg.Text += Environment.NewLine; lblMsg.Text += Environment.NewLine; // Calendar - 日曆對象,預設返回當前系統的預設日曆 Calendar calendarDefault = new Calendar(); // 第一個參數:將日曆轉換為字元串時,優先使用的語言標識列表;第二個參數:指定日曆的類型;第三個參數:指定是12小時制還是24小時制 Calendar calendarHebrew = new Calendar(new[] { "zh-CN" }, CalendarIdentifiers.Hebrew, ClockIdentifiers.TwentyFourHour); lblMsg.Text += "Gregorian Day: " + calendarDefault.DayAsString(); // 西曆的日期 lblMsg.Text += Environment.NewLine; lblMsg.Text += "Hebrew Day: " + calendarHebrew.DayAsString(); // 希伯來歷的日期 // Calendar 還有很多屬性和方法,不再一一介紹,需要時查 msdn } } }
2、演示不同語言環境下對數字的格式化
Localization/NumberFormatting.xaml
<Page x:Class="Windows10.Localization.NumberFormatting" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.Localization" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" /> </StackPanel> </Grid> </Page>
Localization/NumberFormatting.xaml.cs
/* * 演示不同語言環境下對數字的格式化 */ using System; using Windows.Globalization.NumberFormatting; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace Windows10.Localization { public sealed partial class NumberFormatting : Page { public NumberFormatting() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { // 百分比格式化 PercentFormatter percentFormatter = new PercentFormatter(); // PercentFormatter percentFormatter = new PercentFormatter(new[] { "zh-Hans-CN" }, "CN"); lblMsg.Text = percentFormatter.Format(3.1415926); lblMsg.Text += Environment.NewLine; // 千分比格式化 PermilleFormatter permilleFormatter = new PermilleFormatter(); // PermilleFormatter permilleFormatter = new PermilleFormatter(new[] { "zh-Hans-CN" }, "CN"); lblMsg.Text += permilleFormatter.Format(3.1415926); lblMsg.Text += Environment.NewLine; // 數字格式化 DecimalFormatter decimalFormatter = new DecimalFormatter(); // DecimalFormatter decimalFormatter = new DecimalFormatter(new[] { "zh-Hans-CN" }, "CN"); lblMsg.Text += decimalFormatter.Format(3.1415926); lblMsg.Text += Environment.NewLine; // 貨幣格式化 CurrencyFormatter currencyFormatter = new CurrencyFormatter("CNY"); // CurrencyFormatter currencyFormatter = new CurrencyFormatter("CNY", new[] { "zh-Hans-CN" }, "CN"); lblMsg.Text += currencyFormatter.Format(3.1415926); } } }
OK
[源碼下載]