[源码下载]

背水一战 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) - 本地化的更多相关文章

  1. 背水一战 Windows 10 (13) - 绘图: Stroke, Brush

    [源码下载] 背水一战 Windows 10 (13) - 绘图: Stroke, Brush 作者:webabcd 介绍背水一战 Windows 10 之 绘图 Stroke - 笔划 Brush ...

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

    [源码下载] 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing 作者:webabcd 介绍背水一 ...

  3. 背水一战 Windows 10 (111) - 通知(Tile): secondary tile 模板之图片, secondary tile 模板之分组

    [源码下载] 背水一战 Windows 10 (111) - 通知(Tile): secondary tile 模板之图片, secondary tile 模板之分组 作者:webabcd 介绍背水一 ...

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

    [源码下载] 背水一战 Windows 10 (81) - 全球化 作者:webabcd 介绍背水一战 Windows 10 之 全球化 Demo 格式化数字 示例1.演示全球化的基本应用Locali ...

  5. 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件

    [源码下载] 背水一战 Windows 10 (74) - 控件(控件基类): UIElement - 与 CanDrag 相关的事件, 与 AllowDrop 相关的事件 作者:webabcd 介绍 ...

  6. 背水一战 Windows 10 (57) - 控件(集合类): ListViewBase - 增量加载, 分步绘制

    [源码下载] 背水一战 Windows 10 (57) - 控件(集合类): ListViewBase - 增量加载, 分步绘制 作者:webabcd 介绍背水一战 Windows 10 之 控件(集 ...

  7. 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件

    [源码下载] 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件 作者 ...

  8. 背水一战 Windows 10 (12) - 绘图: Shape, Path

    [源码下载] 背水一战 Windows 10 (12) - 绘图: Shape, Path 作者:webabcd 介绍背水一战 Windows 10 之 绘图 Shape - 图形 Path - 路径 ...

  9. 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog

    [源码下载] 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog 作者:webabcd 介绍背水一战 Windows 10 之 控 ...

随机推荐

  1. canvas 2.0 图片绘制

    绘制图片drawImage 2013.02.21 by 十年灯·一条评论 本文属于<html5 Canvas画图系列教程> 这里的绘制图片是指把一张现成的图片,绘制到Canvas上面. 有 ...

  2. Eurekalog

    Eurekalog ‪E:\Program Files (x86)\Neos Eureka S.r.l\EurekaLog 7\Packages\Studio25\EurekaLogComponent ...

  3. netbeans 正则替换

    单引号替换为双引号: tablename1('xhw_aa')  tablename2('xhw_bb') tablename3('xhw_cc') tablename4('xhw_dd')   (t ...

  4. jschDemo

    jsch是java的sftp实现 import com.jcraft.jsch.*; import java.io.OutputStream; public class JschStart { pub ...

  5. vue 初始化data中的数据

    Object.assign(this.$data, this.$options.data());

  6. 云笔记项目-补充JS面向对象编程基础知识

    简单介绍: 此部分知识为在做云笔记项目中补充,因为云笔记项目中涉及到前端js,里面写了很多js脚本,用到了创建js属性和方法,在js中直接声明的属性和方法最终都会变成window的对象,即其成为了全局 ...

  7. java实现两个不同list对象合并后并排序

    工作上遇到一个要求两个不同list对象合并后并排序1.问题描述从数据库中查询两张表的当天数据,并对这两张表的数据,进行合并,然后根据时间排序.2.思路从数据库中查询到的数据放到各自list中,先遍历两 ...

  8. C语言之标准源文件模板

    /*======================================================================================* * 版权 : xxx ...

  9. php curl抓取类分享

    class UsualFunForNetWorkHelper { /*** * post请求数据 */ public static function HttpsPost($url, $data = n ...

  10. 508. Most Frequent Subtree Sum 最频繁的子树和

    [抄题]: Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum ...