[源码下载]

重新想象 Windows 8 Store Apps (57) - 本地化和全球化

作者:webabcd

介绍
重新想象 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
[源码下载]

重新想象 Windows 8 Store Apps (57) - 本地化和全球化的更多相关文章

  1. 重新想象 Windows 8 Store Apps 系列文章索引

    [源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  2. 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo

    [源码下载] 重新想象 Windows 8 Store Apps (34) - 通知: Toast Demo, Tile Demo, Badge Demo 作者:webabcd 介绍重新想象 Wind ...

  3. 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解

    [源码下载] 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Toa ...

  4. 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解

    [源码下载] 重新想象 Windows 8 Store Apps (36) - 通知: Tile 详解 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 通知 Tile ...

  5. 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract

    [源码下载] 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...

  6. 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract

    [源码下载] 重新想象 Windows 8 Store Apps (38) - 契约: Search Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 ...

  7. 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract

    [源码下载] 重新想象 Windows 8 Store Apps (39) - 契约: Share Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之  ...

  8. 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件

    [源码下载] 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件 作者:webabcd 介绍重新想象 Windows 8 Store ...

  9. 重新想象 Windows 8 Store Apps (41) - 打印

    [源码下载] 重新想象 Windows 8 Store Apps (41) - 打印 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 打印 示例1.需要打印的文档Pr ...

随机推荐

  1. CSS3学习笔记--line-height:150%与line-height:1.5的真正区别

    代码: <div style="line-height:150%;font-size:16px;"> 父元素内容 <div style="font-si ...

  2. fflua更新-增加对引用的支持

    简介: fflua 发布了有段时间了,很多网友都用了,并且提供了一些很好的反馈.其中一个就是c++接口注册到lua中时,对引用的支持.这样使用起来更加方便. 原有方式: fflua 中注册c++的类用 ...

  3. CSS - Tooltip-arrow 绘制三角形

    问题:纯CSS实现bubble的三角形部分 方法:使用border来绘制三角形:例如 .trangle { ; border-color: transparent; border-style: sol ...

  4. MacBook 蓝牙无法搜索设备

    背景 经常把MacBook合上盖子就塞进包里,用时打开盖子就继续操作,偶尔会出现刚刚还在用的罗技蓝牙鼠标,重新打开笔记本后就连接不上了,而且也无法搜索到周边的蓝牙设备. 解决方案 快捷键:Option ...

  5. AX 与Citrix打印机问题

    国外文章,抄个链接,备查 http://blogs.msdn.com/b/axsupport/archive/2010/07/06/ax-2009-citrix-amp-terminal-server ...

  6. 用Razor語法寫範本-RazorEngine組件介紹【转——非常好,可以用它来代替NVelocity】

    RazorEngine 官網網址:http://razorengine.codeplex.com 在找到RazorEngine之前曾經想過其他的方案,如T4與V8 Engine載jquery.temp ...

  7. 天猫浏览型应用的CDN静态化架构演变

    原文链接:http://www.csdn.net/article/2014-01-22/2818227-CDN-Architecture 在天猫双11活动中,商品详情.店铺等浏览型系统,通常会承受超出 ...

  8. 如何准备PMP考试?

    东西在精,而不在多.话不多说,干货如下: 1.参加培训,不要持续时间太长,通常情况下3个月时间足够了:许多和我一起参加培训的学员,有时候准备6个月时间,反而没有3个月冲刺的时间考试结果好. 2.培训老 ...

  9. Ajax请求WebService跨域问题 [转载]

    1.背景 用Jquery中Ajax方式在asp.net开发环境中WebService接口的调用 2.出现的问题 原因分析:浏览器同源策略的影响(即JavaScript或Cookie只能访问同域下的内容 ...

  10. javascript文件夹选择框的两种解决方案

    javascript文件夹选择框的两种解决方案 解决方案1:调用windows 的shell,但会有安全问题. * browseFolder.js * 该文件定义了BrowseFolder()函数,它 ...