背水一战 Windows 10 (80) - 本地化
作者:webabcd
介绍
背水一战 Windows 10 之 本地化
- Demo
- 改变语言
示例
1、演示本地化的基本应用
Localization/LocalizationDemo.xaml
<Page
x: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
<Page
x: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.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; // 获取当前语言环境的指定资源(更多用法请参见 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
[源码下载]
背水一战 Windows 10 (80) - 本地化的更多相关文章
- 背水一战 Windows 10 (13) - 绘图: Stroke, Brush
[源码下载] 背水一战 Windows 10 (13) - 绘图: Stroke, Brush 作者:webabcd 介绍背水一战 Windows 10 之 绘图 Stroke - 笔划 Brush ...
- 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing
[源码下载] 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing 作者:webabcd 介绍背水一 ...
- 背水一战 Windows 10 (111) - 通知(Tile): secondary tile 模板之图片, secondary tile 模板之分组
[源码下载] 背水一战 Windows 10 (111) - 通知(Tile): secondary tile 模板之图片, secondary tile 模板之分组 作者:webabcd 介绍背水一 ...
- 背水一战 Windows 10 (81) - 全球化
[源码下载] 背水一战 Windows 10 (81) - 全球化 作者:webabcd 介绍背水一战 Windows 10 之 全球化 Demo 格式化数字 示例1.演示全球化的基本应用Locali ...
- 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件
[源码下载] 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件 作者:webabcd 介绍 ...
- 背水一战 Windows 10 (57) - 控件(集合类): ListViewBase - 增量加载, 分步绘制
[源码下载] 背水一战 Windows 10 (57) - 控件(集合类): ListViewBase - 增量加载, 分步绘制 作者:webabcd 介绍背水一战 Windows 10 之 控件(集 ...
- 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件
[源码下载] 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件 作者 ...
- 背水一战 Windows 10 (12) - 绘图: Shape, Path
[源码下载] 背水一战 Windows 10 (12) - 绘图: Shape, Path 作者:webabcd 介绍背水一战 Windows 10 之 绘图 Shape - 图形 Path - 路径 ...
- 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog
[源码下载] 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog 作者:webabcd 介绍背水一战 Windows 10 之 控 ...
随机推荐
- python基础数据篇
1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 ? 1 names = ['Alex',"Tenglan",' ...
- PHP判断手机、电脑访问
/*判断用户是手机访问还是电脑访问*/$useragent = $_SERVER['HTTP_USER_AGENT']; if (preg_match('/(android|bb\d+|meego). ...
- intllij IDE 中git ignore 无法删除target目录下的文件
原因: git的本地忽略设置必须保证git的远程仓库分支上没有这个要忽略的文件,如果远程分支上存在这个文件,本地在设置ignore 这个文件,将会失败,无法commit忽略.(有人说是git的bug, ...
- leetcode1029
class Solution(object): def twoCitySchedCost(self, costs: 'List[List[int]]') -> int: costs = sort ...
- ArcGIS自定义工具箱-清空工作空间
ArcGIS自定义工具箱-清空工作空间 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:删除工作空间里的要素类,栅格和独立表 使用方法: 例如"C:\ ...
- 2019.3.28 S21 day02pyth笔记总结
昨日内容补充: 1.字符串:'中国' 'Hello' 字符:中是一个字符,e是一个字符 字节:中是3个字节,e是1个字节 位:01010101是8位,其中0或1分别是1位 unicode用于内存 ...
- SpringJDBC中jdbcTemplate 的使用
一:定义 SpringJDBC是spring官方提供的一个持久层框架,对JDBC进行了封装,提供了一个JDBCTemplated对象简化JDBC的开发.但Spring本身不是一个orm框架,与hibe ...
- Log4J2用法
一. 关于Log4J 2015年5月,Apache宣布Log4J 1.x 停止更新.最新版为1.2.17. 如今,Log4J 2.x已更新至2.7. 官方网址:http://logging.ap ...
- 【Django】网页跳转的问题
这两天有个比较奇怪了问题困扰着我,就是网页跳转之后页面没有变化,虽然url已经变了,但是页面还是原来的,只是表单数据清空了 就是http://127.0.0.1:8000/signup_signin/ ...
- 十三 re模块
一:什么是正则? 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则.(在Python中)它内嵌在Python中,并通过 r ...