介紹
重新想象 Windows 8 Store Apps 之 本地化和全球化
本地化 - Demo
本地化 - 改變語言
全球化 - Demo
全球化 - 格式化數字
示例
1、演示本地化的基本應用
Localization/LocalizationDemo.xaml
<Page x:Class="XamlDemo.Localization.LocalizationDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.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="120 0 0 0"> <TextBlock FontSize="14.667"> <Run>本地化資源文件,以下舉例說明:</Run> <LineBreak /> <Run>1、在 en 目錄下的是英文資源文件,在 zh-CN 目錄下的是簡體中文(zh 代表語言,CN 代表國家或地區)資源文件</Run> <LineBreak /> <Run>2、Resources.lang-en.resw 代表英文資源文件,Resources.lang-zh-CN.resw 代表簡體中文資源文件</Run> <LineBreak /> <Run>3、圖片資源的本地化可以參照以上命名規則,同時可與 scale 和 high contrast 相結合</Run> <LineBreak /> <Run>4、Package.appxmanifest 中引用的字符串也支持本地化,引用方式:ms-resource:Hello 或 ms-resource:///Resources/Hello</Run> <LineBreak /> <Run>5、Tile 和 Toast 中引用的字符串也支持本地化,引用方式:ms-resource:Hello 或 ms-resource:///Resources/Hello</Run> <LineBreak /> <Run>6、當無法找到某語言對應的資源時,系統會自動使用 Package.appxmanifest 中設置的默認語言所對應的資源</Run> </TextBlock> <!-- 通過 x:Uid 本地化控件的各個屬性,請參看資源文件中的 HelloTextBlock.FontSize 和 HelloTextBlock.Text --> <TextBlock x:Uid="HelloTextBlock" Margin="0 10 0 0" /> <!-- code - behind 方式獲取本地化資源 --> <TextBlock x:Name="lblMsg" FontSize="14.667" Margin="0 10 0 0" /> <!-- 圖片的本地化 --> <Image Source="/Localization/Logo.png" Width="200" Height="100" Margin="0 10 0 0" HorizontalAlignment="Left" /> </StackPanel> </Grid> </Page>
Localization/LocalizationDemo.xaml.cs
/* * 演示本地化的基本應用 * * 另: * Visual Studio 2012 的多語言應用程序工具包請參見:http://msdn.microsoft.com/zh -cn/windows/apps/hh848309 */ using Windows.ApplicationModel.Resources; using Windows.ApplicationModel.Resources.Core; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace XamlDemo.Localization { public sealed partial class LocalizationDemo : Page { public LocalizationDemo() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { /* * ResourceLoader resourceLoader = new ResourceLoader(); - 獲取默認的 ResourceLoader(Resources.resw 中的資源) * ResourceLoader resourceLoader = new ResourceLoader("MyResources"); - 獲取指定的 ResourceLoader(MyResources.resw 中的資源) * ResourceLoader resourceLoader = new ResourceLoader("ClassLibrary/MyResources"); - 獲取指定類庫的指定的 ResourceLoader(ClassLibrary 類庫中的 MyResources.resw 中的資源) */ // 獲取默認的 ResourceLoader(即 Resources.resw 中的資源) ResourceLoader resourceLoader = new ResourceLoader(); // 通過資源標識,獲取指定的資源 lblMsg.Text = resourceLoader.GetString("Hello"); } } }
2、演示與“改變語言”相關的一些應用
Localization/Language.xaml
<Page x:Class="XamlDemo.Localization.Language" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.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="120 0 0 0"> <ComboBox Name="cmbLanguage" Width="800" HorizontalAlignment="Left" /> <Button Name="btnGetEng" Content="獲取英文資源" Margin="0 10 0 0" Click="btnGetEng_Click_1" /> <TextBlock Name="lblMsg" FontSize="14.667" Margin="0 10 0 0" /> </StackPanel> </Grid> </Page>
Localization/Language.xaml.cs
/* * 演示與“改變語言”相關的一些應用 * * 1、演示如何改變當前的語言環境 * 2、演示如何監測當前語言環境發生的變化 * 3、演示如何獲取指定語言環境下的資源 */ using System; using System.Collections.Generic; using System.Text; using Windows.ApplicationModel.Resources.Core; using Windows.Globalization; using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace XamlDemo.Localization { public sealed partial class Language : Page { public Language() { this.InitializeComponent(); this.Loaded += Language_Loaded; } void Language_Loaded(object sender, RoutedEventArgs e) { string currentLanguage; // 獲取當前的語言 ResourceManager.Current.DefaultContext.QualifierValues.TryGetValue("Language", out currentLanguage); lblMsg.Text = "current language: " + currentLanguage; lblMsg.Text += Environment.NewLine; // ApplicationLanguages.ManifestLanguages - 遍歷 Package.appxmanifest 中的語言列表 foreach (string strLang in ApplicationLanguages.ManifestLanguages) { // 關於 Language 的說明詳見 GlobalizationDemo.xaml var lang = new Windows.Globalization.Language(strLang); cmbLanguage.Items.Add(string.Format("DisplayName:{0}, NativeName:{1}, LanguageTag:{2}, Script:{3}", lang.DisplayName, lang.NativeName, lang.LanguageTag, lang.Script)); } cmbLanguage.SelectionChanged += cmbLanguage_SelectionChanged; // 獲取當前語言環境的指定資源 lblMsg.Text += ResourceManager.Current.MainResourceMap.GetValue("Resources/Hello").ValueAsString; // 當前語言環境發生改變時所觸發的事件(通過 API 更改或者通過“電腦設置 -> 常規 -> 語言首選項”更改都會觸發此事件) ResourceManager.Current.DefaultContext.QualifierValues.MapChanged += async (s, m) => { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lblMsg.Text += Environment.NewLine; lblMsg.Text += ResourceManager.Current.MainResourceMap.GetValue("Resources/Hello").ValueAsString; }); }; } void cmbLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e) { // ApplicationLanguages.PrimaryLanguageOverride - 設置或獲取首選語言的 BCP-47 語言標記 if (cmbLanguage.SelectedValue.ToString().Contains("LanguageTag:en")) Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "en"; else if (cmbLanguage.SelectedValue.ToString().Contains("LanguageTag:zh-Hans-CN")) Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "zh-Hans-CN"; StringBuilder sb = new StringBuilder(); // ApplicationLanguages.Languages - 運行時級別的語言列表 foreach (string item in ApplicationLanguages.Languages) { sb.Append(item); sb.Append(","); } lblMsg.Text += Environment.NewLine; lblMsg.Text += "ApplicationLanguages.Languages: " + sb.ToString(); } private void btnGetEng_Click_1(object sender, RoutedEventArgs e) { // 指定 ResourceContext 為 en 語言環境 ResourceContext resourceContext = new ResourceContext(); resourceContext.Languages = new List<string>() { "en" }; // 獲取 en 語言環境下的 Resources 映射 ResourceMap resourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources"); lblMsg.Text += Environment.NewLine; // 獲取指定的語言環境下的指定標識的資源 lblMsg.Text += "英語的 Hello: " + resourceMap.GetValue("Hello", resourceContext).ValueAsString; } } }
3、演示全球化的基本應用
Localization/GlobalizationDemo.xaml
<Page x:Class="XamlDemo.Localization.GlobalizationDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.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="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> </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 XamlDemo.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"); // 語言的本地化名稱 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 } } }
查看本欄目
4、演示不同語言環境下對數字的格式化
Localization/NumberFormatting.xaml
<Page x:Class="XamlDemo.Localization.NumberFormatting" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.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="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> </StackPanel> </Grid> </Page>
Localization/NumberFormatting.xaml.cs
/* * 演示不同語言環境下對數字的格式化 */ using System; using Windows.Globalization.NumberFormatting; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace XamlDemo.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
[源碼下載]:http://files.cnblogs.com/webabcd/Windows8.rar