I18N

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Languages" value="en-US.xaml"/>
</appSettings>
</configuration>

App.xaml.cs

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e); //第一种方式就是用配置文件 配置
string appLang = ConfigurationManager.AppSettings.Get("Languages");
App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(appLang, UriKind.RelativeOrAbsolute) });
//第二种方式 就是获取当前系统的语言
string lang = System.Globalization.CultureInfo.InstalledUICulture.Name.ToString(CultureInfo.InvariantCulture);
switch (lang)
{
case "zh-CN":
App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("zh-CN.xaml", UriKind.RelativeOrAbsolute) }); break;
case "en-US":
App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("en-US.xaml", UriKind.RelativeOrAbsolute) }); break;
default:
App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("en-US.xaml", UriKind.RelativeOrAbsolute) }); break;
}
}

en-US.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="Window1Title">Window</sys:String>
<sys:String x:Key="UserLog">UserLogin</sys:String>
<sys:String x:Key="UserCode">UserCode</sys:String>
<sys:String x:Key="UserName">UserPwd</sys:String>
<sys:String x:Key="BtnLoginOk">Login</sys:String>
<sys:String x:Key="BtnLoginCancel">Cancel</sys:String>
<sys:String x:Key="BtnzhCN">Chinese</sys:String>
<sys:String x:Key="BtnenUS">English</sys:String> </ResourceDictionary>

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="Window1Title">窗体</sys:String>
<sys:String x:Key="UserLog">用户登录</sys:String>
<sys:String x:Key="UserCode">用户名</sys:String>
<sys:String x:Key="UserName">密码</sys:String>
<sys:String x:Key="BtnLoginOk">登录</sys:String>
<sys:String x:Key="BtnLoginCancel">取消</sys:String>
<sys:String x:Key="BtnzhCN">中文</sys:String>
<sys:String x:Key="BtnenUS">英文</sys:String> </ResourceDictionary>

MainWindow.xaml

<Window x:Class="I18N.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="{DynamicResource Window1Title}">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<GroupBox Header="{DynamicResource UserLog}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="45*" />
<RowDefinition Height="48*" />
<RowDefinition Height="58*" />
<RowDefinition Height="100*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="1"
Margin="36,22,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="{DynamicResource UserCode}"
TextWrapping="Wrap" />
<TextBlock Grid.Row="2"
Margin="36,23,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="{DynamicResource UserName}"
TextWrapping="Wrap" />
<TextBox Grid.Row="1"
Width="120"
Height="23"
Margin="125,22,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="TextBox"
TextWrapping="Wrap" />
<TextBox Grid.Row="2"
Width="120"
Height="23"
Margin="125,23,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="TextBox"
TextWrapping="Wrap" />
<Button Grid.Row="3"
Width="75"
Margin="78,29,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="{DynamicResource BtnLoginOk}" />
<Button Grid.Row="3"
Width="75"
Margin="192,29,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="{DynamicResource BtnLoginCancel}" />
<Button Name="btnenUS"
Width="75"
Margin="301,10,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Click="btnenUS_Click"
Content="{DynamicResource BtnenUS}" />
<Button Name="btnzhCN"
Width="75"
Margin="221,10,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Click="btnzhCN_Click"
Content="{DynamicResource BtnzhCN}" /> </Grid>
</GroupBox>
</Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Windows; namespace I18N
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void btnzhCN_Click(object sender, RoutedEventArgs e)
{
App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("zh-CN.xaml", UriKind.RelativeOrAbsolute) });
} private void btnenUS_Click(object sender, RoutedEventArgs e)
{
App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("en-US.xaml", UriKind.RelativeOrAbsolute) });
}
}
}
上一篇:spring-boot-starter-security Spring Boot中集成Spring Security


下一篇:优化MySchool总结习题