WPF 动态切换多语言
控件名:LanguageManager
作 者:WPFDevelopersOrg - 驚鏵
原文链接[1]:https://github.com/WPFDevelopersOrg/WPFDevelopers
码云链接[2]:https://gitee.com/WPFDevelopersOrg/WPFDevelopers
框架支持 .NET4 至 .NET8
;Visual Studio 2022
;此篇描述官方推荐使用 ResXResourceManager
进行多语言管理.resx
,源码地址[3] 可以此处下载.vsix
安装包进行安装。
也可以通过 管理扩展
进行安装(推荐使用扩展
进行安装),在右侧输入resx
搜索结果中第一个就是。
安装成功后,创建 Language.resx
资源文件进行管理多语言。
选择工具
-> ResX Manager
进行管理多语言。
设置中立资源
新增一个新的 Key
添加新的语言
新建 LanguageManager.cs
实现动态切换语言。
public class LanguageManager : INotifyPropertyChanged
{
private readonly ResourceManager _resourceManager;
private static readonly Lazy<LanguageManager> _lazy = new Lazy<LanguageManager>(() => new LanguageManager());
public static LanguageManager Instance => _lazy.Value;
public event PropertyChangedEventHandler PropertyChanged;
public LanguageManager()
{
_resourceManager = new ResourceManager("WPFDevelopers.Languages.Language", typeof(LanguageManager).Assembly);
}
public string this[string name]
{
get
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return _resourceManager.GetString(name);
}
}
public void ChangeLanguage(CultureInfo cultureInfo)
{
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
OnPropertyChanged("Item[]");
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
代码示例 MainWindow.xaml
使用。
引入命名空间 xmlns:local="clr-namespace:WpfApp"
在控件中使用 {Binding [Key], Source={x:Static local:LanguageManager.Instance}}
,将方括号中的"Key"
替换为自己使用的键。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<ComboBox
HorizontalAlignment="Center"
SelectedIndex="0"
SelectionChanged="ComboBox_SelectionChanged">
<ComboBoxItem Tag="zh-cn">中文</ComboBoxItem>
<ComboBoxItem Tag="en-US">English</ComboBoxItem>
<ComboBoxItem Tag="ko-KR">한국어</ComboBoxItem>
</ComboBox>
<StackPanel Grid.Row="1"
Margin="0,10"
HorizontalAlignment="Center">
<TextBlock HorizontalAlignment="Center" Text="{Binding [WD], Source={x:Static local:LanguageManager.Instance}}" />
<Button
Margin="10"
Padding="20,10"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{Binding [Key], Source={x:Static local:LanguageManager.Instance}}" />
<WrapPanel>
<WrapPanel Margin="0,0,15,0">
<TextBlock Text="{Binding [Total], Source={x:Static local:LanguageManager.Instance}}" />
<TextBlock Text="{Binding ElementName=_this, Path=Height, StringFormat=' {0} '}" />
<TextBlock Text="{Binding [Items], Source={x:Static local:LanguageManager.Instance}}" />
</WrapPanel>
<WrapPanel>
<TextBlock Text="{Binding [Items], Source={x:Static local:LanguageManager.Instance}}" />
<TextBlock Text=" / " />
<TextBlock Text="{Binding [Page], Source={x:Static local:LanguageManager.Instance}}" />
</WrapPanel>
</WrapPanel>
</StackPanel>
</Grid>
代码示例 MainWindow.xaml.cs
中 ComboBox
切换更换语言。
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var cBox = sender as ComboBox;
if (cBox != null)
{
var item = cBox.SelectedItem as ComboBoxItem;
if (item != null)
{
LanguageManager.Instance.ChangeLanguage(new CultureInfo(item.Tag.ToString()));
}
}
}
参考资料
原文链接: https://github.com/WPFDevelopersOrg/WPFDevelopers
[2]码云链接: https://gitee.com/WPFDevelopersOrg/WPFDevelopers
[3]源码地址: https://github.com/dotnet/ResXResourceManager