一、安装ResXManager插件




二、国际化文件
2.1 创建国际化文件
在项目下创建Resources
文件夹,并创建国际化文件Lang.zh-CN.resx

2.2 编辑国际化内容
选中Lang.zh-CN.resx
,鼠标右键选择【在Resx Manager中打开】


默认只有简体中文,我们添加新语言,选择英语


添加国际化键值对



关闭Resx Manager窗口后,我们可以看到在Resources目录下,自动生成了一个新的国际化文件Lang.zh-CN.en.resx
,我们将名字改成Lang.en-US.resx

三、代码实现
3.1 创建LanguageManager.cs
using System.ComponentModel;
using System.Globalization;
using System.Resources;
namespace wpf_demo
{
internal 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()
{
//获取此命名空间下Resources的Lang的资源,Lang可以修改
_resourceManager = new ResourceManager("wpf_demo.Resources.Lang", 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)
{
CultureInfo.CurrentCulture = cultureInfo;
CultureInfo.CurrentUICulture = cultureInfo;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("item[]")); //字符串集合,对应资源的值
}
}
}
注意:代码中wpf_demo
改成你自己的解决方案名称。
3.2 LangWindow.xaml
<Window x:Class="wpf_demo.LangWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:local="clr-namespace:wpf_demo"
xmlns:res="clr-namespace:System.Windows.Resources;assembly=PresentationFramework"
mc:Ignorable="d"
Title="国际化" Height="450" Width="800">
<Grid>
<VirtualizingStackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock x:Name="txtMsg" Text="{Binding [username], Source={x:Static local:LanguageManager.Instance}}"></TextBlock>
<Button Click="CnAction" Content="中文" Margin="0 10 0 0"></Button>
<Button Click="EnAction" Content="英文" Margin="0 10 0 0"></Button>
</VirtualizingStackPanel>
</Grid>
</Window>
3.3 LangWindow.xaml.cs
using System.Globalization;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace wpf_demo
{
public partial class LangWindow : Window
{
public LangWindow()
{
InitializeComponent();
SetDefaultLang();
}
//国际化默认使用系统语言环境
private void SetDefaultLang()
{
CultureInfo currentCulture = CultureInfo.CurrentCulture;
string systemLanguage = currentCulture.TwoLetterISOLanguageName; // 返回两个字母的语言简写,例如"en"代表英语,"zh"代表中文
bool isCn = "zh".Equals(systemLanguage);
if (isCn == true)
{
LanguageManager.Instance.ChangeLanguage(new CultureInfo("zh-CN"));
}
else
{
LanguageManager.Instance.ChangeLanguage(new CultureInfo("en-US"));
}
}
//中文显示
private void CnAction(object sender, RoutedEventArgs e)
{
LanguageManager.Instance.ChangeLanguage(new CultureInfo("zh-CN"));
}
//英文显示
private void EnAction(object sender, RoutedEventArgs e)
{
LanguageManager.Instance.ChangeLanguage(new CultureInfo("en-US"));
}
}
}
代码结构

四、效果


参考资料:WPF本地化/国际化,多语言切换
网友评论