1. 在WPF中国际化使用的是 .xaml文件的格式
如图:Resource Dictionary (WPF)
2. 创建默认的语言文件和其他语言文件
这里以英语为默认语言,新建一个 Resource Dictionary (WPF)文件,并命名为DefaultLanguage.xaml,内容如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"><!--这行新增加的-->
<sys:String x:Key="OK">
OK
</sys:String>
<sys:String x:Key="Cancel">
Cancel
</sys:String>
</ResourceDictionary>
默认语言文件的 BuildAction要设置为 Page,如图:
为了便于管理,一般将所有的语言文件都放在一个目录下,这里创建lang目录,
然后在创建另一个语言文件,这里是中文,命名为 zh_CN.xaml,内容如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String x:Key="OK">
确定
</sys:String>
<sys:String x:Key="Cancel">
取消
</sys:String>
</ResourceDictionary>
其他非默认语言的设置应该如下:
BuildAction设置为:Content ;CopyToOutputDirectory设置为:Copy if newer (先这样做吧,原因未清)
3.在App.xaml中配置默认语言:
<Application x:Class="LanTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary><!--这个节点就是配置默认语言的-->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="lang\DefaultLanguage.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
4.实际使用(敲代码了)
4.1. 界面效果如下:
4.2. 界面的.xaml代码
1 <Window x:Class="LanTest.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" Height="350