背水一战 Windows 10 (80) - 本地化
原文: 背水一战 Windows 10 (80) - 本地化

[源码下载]

背水一战 Windows 10 (80) - 本地化

作者:webabcd

介绍
背水一战 Windows 10 之 本地化

  • Demo
  • 改变语言

示例
1、演示本地化的基本应用
Localization/LocalizationDemo.xaml

<Pagex:Class="Windows10.Localization.LocalizationDemo"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"><Grid.Resources><ResourceDictionary><local:LocalizedStrings x:Key="Localized"/></ResourceDictionary></Grid.Resources><StackPanel Margin="10 0 10 10"><TextBlock><Run>本地化资源文件,以下举例说明:</Run><LineBreak /><Run>1、在 en 目录下的是英文资源文件,在 zh-hans 目录下的是简体中文(zh 代表中文,hans 代表简体中文)资源文件(关于限定符的详细说明请参见 /Resource/Qualifiers/)</Run><LineBreak /><Run>2、Resources.lang-en.resw 代表英文资源文件,Resources.lang-zh-hans.resw 代表简体中文资源文件(关于限定符的详细说明请参见 /Resource/Qualifiers/)</Run><LineBreak /><Run>3、Package.appxmanifest 中引用的字符串也支持本地化,引用方式:ms-resource:Hello 或 ms-resource:///Resources/Hello</Run><LineBreak /><Run>4、Tile 和 Toast 中引用的字符串也支持本地化,引用方式:ms-resource:Hello 或 ms-resource:///Resources/Hello</Run><LineBreak /><Run>5、当无法找到某语言对应的资源时,系统会自动使用 Package.appxmanifest 中设置的默认语言所对应的资源</Run></TextBlock><!--通过 x:Uid 本地化控件的各个属性,请参看资源文件中的 HelloTextBlock.FontSize 和 HelloTextBlock.Text--><TextBlock x:Uid="HelloTextBlock" Margin="5" /><!--图片的本地化--><Image Source="/Localization/Logo.png" Width="200" Height="100" Margin="5" HorizontalAlignment="Left" /><!--code - behind 方式获取本地化资源--><TextBlock x:Name="lblMsg1" Margin="5" /><!--code - behind 方式获取本地化资源--><TextBlock x:Name="lblMsg2" Margin="5" /><!--code - behind 方式获取本地化资源--><TextBlock x:Name="lblMsg3" Margin="5" /><!--code - behind 方式获取本地化资源--><TextBlock x:Name="lblMsg4" Margin="5" /><!--code - behind 方式获取本地化资源--><TextBlock x:Name="lblMsg5" Margin="5" /><!--绑定本地化资源--><TextBlock x:Name="lblMsg6" Margin="5" Text="{Binding [Hello], Source={StaticResource Localized}}" /></StackPanel></Grid>
</Page>

Localization/LocalizationDemo.xaml.cs

/** 演示本地化的基本应用* * * 注:建议使用多语言应用工具包 https://developer.microsoft.com/zh-cn/windows/develop/multilingual-app-toolkit*/using System;
using System.Resources;
using Windows.ApplicationModel.Resources;
using Windows.ApplicationModel.Resources.Core;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;namespace Windows10.Localization
{public sealed partial class LocalizationDemo : Page{public LocalizationDemo(){this.InitializeComponent(); }protected override void OnNavigatedTo(NavigationEventArgs e){/** ResourceLoader resourceLoader = ResourceLoader.GetForViewIndependentUse(); - 获取默认的 ResourceLoader(Resources.resw 中的资源)* ResourceLoader resourceLoader = ResourceLoader.GetForViewIndependentUse("MyResources"); - 获取指定的 ResourceLoader(MyResources.resw 中的资源)* ResourceLoader resourceLoader = ResourceLoader.GetForViewIndependentUse("ClassLibrary/MyResources"); - 获取指定类库的指定的 ResourceLoader(ClassLibrary 类库中的 MyResources.resw 中的资源)* resourceLoader.GetString(), resourceLoader.GetStringForUri() - 通过资源标识,获取当前语言环境的指定的资源* * GetForCurrentView() 和 GetForViewIndependentUse() 的区别如下:* 1、GetForCurrentView() - 在 UI 线程上执行* 2、GetForViewIndependentUse() - 在非 UI 线程上执行(注:Independent 这个词在 uwp 中就时非 UI 线程的意思,比如 Independent Animation 就是不依赖 UI 线程的)*/// 获取默认的 ResourceLoader(即 Resources.resw 中的资源)ResourceLoader resourceLoader = ResourceLoader.GetForViewIndependentUse();// 通过资源标识,获取当前语言环境的指定的资源(资源名:Hello)lblMsg1.Text = resourceLoader.GetString("Hello");// 通过资源标识,获取当前语言环境的指定的资源(资源名:HelloTextBlock.Text)lblMsg2.Text = resourceLoader.GetString("HelloTextBlock/Text");// 通过资源标识,获取当前语言环境的指定的资源(资源名:Hello)lblMsg3.Text = resourceLoader.GetStringForUri(new Uri("ms-resource:///Resources/Hello"));// 通过资源标识,获取当前语言环境的指定的资源(资源名:HelloTextBlock.Text)lblMsg4.Text = resourceLoader.GetStringForUri(new Uri("ms-resource:///Resources/HelloTextBlock/Text"));// 获取当前语言环境的指定的资源的另一种方式lblMsg5.Text = Windows.ApplicationModel.Resources.Core.ResourceManager.Current.MainResourceMap.GetValue("Resources/Hello", ResourceContext.GetForCurrentView()).ValueAsString;}}// 用于演示如何绑定本地化资源public class LocalizedStrings{public string this[string key]{get{return ResourceLoader.GetForCurrentView().GetString(key);}}}
}

2、演示与“改变语言”相关的一些应用
Localization/Language.xaml

<Pagex:Class="Windows10.Localization.Language"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 Name="root" Margin="10 0 10 10"><!--通过此种方式获取本地化资源时,如果在页面加载后修改了语言首选项的话是不会立即有效果的,需要重新加载页面才行(懒的写了,要看效果的话就先返回,然后再进来就好)--><TextBlock x:Uid="HelloTextBlock" Margin="5" /><ComboBox Name="cmbLanguage" Width="800" HorizontalAlignment="Left" Margin="5" /><Button Name="btnGetEnglish" Content="获取英文资源" Margin="5" Click="btnGetEnglish_Click" /><Button Name="btnGetChinese" Content="获取简体中文资源" Margin="5" Click="btnGetChinese_Click" /><TextBlock Name="lblMsg" Margin="5" /></StackPanel></Grid>
</Page>

Localization/Language.xaml.cs

/** 演示与“改变语言”相关的一些应用* * 1、演示如何改变当前的语言环境* 2、演示如何监测当前语言环境发生的变化* 3、演示如何获取指定语言环境下的资源*/using System;
using System.Collections.Generic;
using System.Text;
using Windows.ApplicationModel.Resources;
using Windows.ApplicationModel.Resources.Core;
using Windows.Globalization;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;namespace Windows10.Localization
{public sealed partial class Language : Page{public Language(){this.InitializeComponent();this.Loaded += Language_Loaded;}void Language_Loaded(object sender, RoutedEventArgs e){// 获取当前的语言string currentLanguage;ResourceContext.GetForCurrentView().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.xamlvar 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;// 获取当前语言环境的指定资源(更多用法请参见 LocalizationDemo.xaml)lblMsg.Text += ResourceLoader.GetForViewIndependentUse().GetString("Hello");// 当前语言环境发生改变时所触发的事件(通过 API 更改或者通过“电脑设置 -> 常规 -> 语言首选项”更改都会触发此事件)// 注:当通过 API(ApplicationLanguages.PrimaryLanguageOverride)修改语言环境时,如果监听了 MapChanged 事件的话,则有很大的几率会导致崩溃,本例就是这样,原因未知ResourceContext.GetForCurrentView().QualifierValues.MapChanged += async (s, m) =>{await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>{lblMsg.Text += Environment.NewLine;lblMsg.Text += ResourceLoader.GetForViewIndependentUse().GetString("Hello");});};}private void cmbLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e){// ApplicationLanguages.PrimaryLanguageOverride - 设置或获取首选语言(BCP-47 语言标记)if (cmbLanguage.SelectedValue.ToString().ToLower().Contains("en-us"))ApplicationLanguages.PrimaryLanguageOverride = "en-US";else if (cmbLanguage.SelectedValue.ToString().ToLower().Contains("zh-hans"))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 btnGetEnglish_Click(object sender, RoutedEventArgs e){// 指定 ResourceContext 为 en-US 语言环境ResourceContext resourceContext = new ResourceContext();resourceContext.Languages = new List<string>() { "en-US" };// 获取 en-US 语言环境下的 Resources 映射ResourceMap resourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");lblMsg.Text += Environment.NewLine;// 获取指定的语言环境下的指定标识的资源lblMsg.Text += "英语的 Hello: " + resourceMap.GetValue("Hello", resourceContext).ValueAsString;}private void btnGetChinese_Click(object sender, RoutedEventArgs e){// 指定 ResourceContext 为 zh-Hans-CN 语言环境ResourceContext resourceContext = new ResourceContext();resourceContext.Languages = new List<string>() { "zh-Hans-CN" };// 获取 zh-Hans 语言环境下的 Resources 映射ResourceMap resourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");lblMsg.Text += Environment.NewLine;// 获取指定的语言环境下的指定标识的资源lblMsg.Text += "简体中文的 Hello: " + resourceMap.GetValue("Hello", resourceContext).ValueAsString;}}
}

OK
[源码下载]

posted on 2018-03-08 10:46 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/8527052.html

背水一战 Windows 10 (80) - 本地化相关推荐

  1. 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing

    背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing 原文:背水一战 Windows 10 (34) ...

  2. 背水一战 Windows 10 (81) - 全球化

    背水一战 Windows 10 (81) - 全球化 原文: 背水一战 Windows 10 (81) - 全球化 [源码下载] 背水一战 Windows 10 (81) - 全球化 作者:webab ...

  3. 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue...

    原文:背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue [源码下载] 背水一战 ...

  4. 背水一战 Windows 10 (65) - 控件(WebView): 对 WebView 中的内容截图, 通过 Share Contract 分享 WebView 中的被选中的内容...

    原文:背水一战 Windows 10 (65) - 控件(WebView): 对 WebView 中的内容截图, 通过 Share Contract 分享 WebView 中的被选中的内容 [源码下载 ...

  5. 背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar

    原文:背水一战 Windows 10 (40) - 控件(导航类): AppBar, CommandBar [源码下载] 背水一战 Windows 10 (40) - 控件(导航类): AppBar, ...

  6. 背水一战 Windows 10 (83) - 用户和账号: 数据账号的添加和管理, OAuth 2.0 验证

    原文:背水一战 Windows 10 (83) - 用户和账号: 数据账号的添加和管理, OAuth 2.0 验证 [源码下载] 背水一战 Windows 10 (83) - 用户和账号: 数据账号的 ...

  7. 背水一战 Windows 10 (47) - 控件(ScrollViewer 特性): Chaining, Rail, Inertia, Snap, Zoom

    原文:背水一战 Windows 10 (47) - 控件(ScrollViewer 特性): Chaining, Rail, Inertia, Snap, Zoom [源码下载] 背水一战 Windo ...

  8. 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定...

    原文:背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定 [源码下载] 背水 ...

  9. 背水一战 Windows 10 (10) - 资源: StaticResource, ThemeResource

    原文:背水一战 Windows 10 (10) - 资源: StaticResource, ThemeResource [源码下载] 背水一战 Windows 10 (10) - 资源: Static ...

最新文章

  1. 邁向IT專家成功之路的三十則鐵律 鐵律十七:IT人休閒之道-清心
  2. IIS HTTP 错误 404.17 - Not Found 解决方法
  3. linux sftp远程连接命令
  4. Python接口自动化实战 ( 第一阶段) - 封装接口请求类和异常处理
  5. POJ 3621 最优比率生成环
  6. 毛坦厂中学是“高考工厂”?白岩松:我做不出嘲讽它的事情!
  7. Python学习笔记1环境搭建
  8. http --- 用于HTTP调试的最小型Perl Web 服务器
  9. 2014-08-26 遇到的小问题
  10. Qt中的角度和正方向描述清单
  11. C程序员要学C++吗?
  12. BlazeDS入门教程-很详细-赞原创作者一个
  13. CentOS 7下无法启动网络(service network start)错误解决办法(转)
  14. Spring之代理模式实例
  15. 广播风暴检测_什么是广播路由算法?如何解决广播风暴?
  16. Django默认用户模型类和父类 AbstractUser 介绍
  17. 接口设计需要考虑哪些方面
  18. 中山大学2021计算机考研复试线,2021中山大学研究生复试分数线
  19. 不可不学的摄影技巧.1—构图
  20. Java-TreeSet与Comparable的详讲与实现

热门文章

  1. 海豚供应链与花旗、汇丰等多家中外资银行合作,获数千万美金授信融资
  2. 一次漏洞挖掘的简单组合拳
  3. offsetX各种值总结
  4. MATLAB汽车号牌识别系统设计
  5. servlet解决浏览器的跨域
  6. 他是香港九龙皇帝,疯狂涂鸦51年,无数次进出警察局,黄家驹都曾为他写歌
  7. 微信开发系列(六)_js调用微信扫码
  8. wxid 转微信号(2)
  9. 艾弗森的史诗——漂泊的十三年
  10. 出国旅游带着贴身“随行翻译”很有必要