背水一战 Windows 10 (81) - 全球化
背水一战 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
[源码下载]