积极答复者
How to update I18N immediately in WPF?

问题
-
I make a I18N demonstration in WPF. I create two Resources.resx, one is "Resources.resx" , the other is "Resources.zh-CN.resx".
I want to change the control's text when changed CultureInfo .
Now I can load resources from Resources.XXX.resx file. but I couldn't dynamic change the contorls' text.
<Window x:Class="Demo05_I18N.MainWindow" 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" xmlns:local="clr-namespace:Demo05_I18N" xmlns:prop="clr-namespace:Demo05_I18N.Properties" Title="MainWindow" Height="176" Width="326"> <Window.Resources> <local:StringList x:Key="CultureList"> <sys:String>zh-CN</sys:String> <sys:String>en</sys:String> </local:StringList> </Window.Resources> <Grid Margin="5"> <Grid.Resources> <Style x:Key="CellControlStyle" TargetType="{x:Type Control}"> <Setter Property="Margin" Value="5" /> </Style> <Style x:Key="CellTextStyle" TargetType="{x:Type TextBlock}"> <Setter Property="Margin" Value="5" /> </Style> </Grid.Resources> <Grid.ColumnDefinitions> <ColumnDefinition Width="120"/> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <TextBlock Name="txtLoginName" Style="{StaticResource CellTextStyle}" Grid.Row="0" Grid.Column="0" Text="{x:Static prop:Resources.LoginName}"/> <TextBlock Style="{StaticResource CellTextStyle}" Grid.Row="1" Grid.Column="0" Text="{x:Static prop:Resources.LoginPwd}"/> <TextBox Style="{StaticResource CellControlStyle}" Grid.Row="0" Grid.Column="1" /> <TextBox Style="{StaticResource CellControlStyle}" Grid.Row="1" Grid.Column="1" /> <Button Style="{StaticResource CellControlStyle}" Grid.Row="2" Grid.Column="1" Content="{x:Static prop:Resources.LoginButton}" /> <TextBlock Style="{StaticResource CellTextStyle}" Grid.Row="3" Grid.Column="0" Text="{x:Static prop:Resources.SwitchCultureInfo}"/> <ComboBox Name="cboCulture" ItemsSource="{StaticResource CultureList}" SelectedIndex="0" Grid.Row="3" Grid.Column="1" SelectionChanged="cboCulture_SelectionChanged" /> </Grid> </Window>
using System.Windows; using System.Windows.Controls; using System.Globalization; namespace Demo05_I18N { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void cboCulture_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (this.IsLoaded) { string sltCulture = cboCulture.SelectedItem.ToString(); Demo05_I18N.Properties.Resources.Culture = new CultureInfo(sltCulture); //txtLoginName.SetValue(TextBlock.TextProperty, Demo05_I18N.Properties.Resources.LoginName);<br/> <strong>//?How to dynamic change?</strong><br/> }
} } }
答案
-
你仅仅通过修改一个资源文件的Culture属性是无法做到切换语言的的,因为你用了静态字段去指定显示的值,这一点你是无法让他随着你的语言环境改变而去自动刷新的。
你需要严格按照MSDN上关于WPF本地化的步骤来进行生成资源文件,和实现切换代码:http://msdn.microsoft.com/zh-cn/library/ms788718.aspx 如何实现的文档:http://msdn.microsoft.com/zh-cn/library/ms746621.aspx
这篇文档还介绍了其它几种方式,其中包括你的这种,但是你的这种是无法实时更改刷新的而且你的这种方法必须在Applicarion启动时指定语言环境:http://www.codeproject.com/Articles/37339/WPF-Localization.aspx
或者你可以自己设计本地化的逻辑,比如使用XML作为本地化资源。 这里下载我的一个例子:http://cid-51b2fdd068799d15.office.live.com/self.aspx/.Public/Samples%5E_2011/20110331%5E_WPFXMLBindingMultiLanguage.zip
Sincerely,
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已建议为答案 Jie BaoModerator 2011年4月25日 8:16
- 已标记为答案 Jie BaoModerator 2011年5月2日 1:59