1、VS2019新建项目
2、引入Nuget包
3、修改XML代码引入命名空间并设置
<Window x:Class="WPFMultiLanguageTest.MainWindow"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:lex="https://github.com/XAMLMarkupExtensions/WPFLocalizationExtension"lex:LocalizeDictionary.DesignCulture="zh"lex:LocalizeDictionary.OutputMissingKeys="True"lex:ResxLocalizationProvider.DefaultAssembly="WPFMultiLanguageTest"lex:ResxLocalizationProvider.DefaultDictionary="LanguageResource"xmlns:local="clr-namespace:WPFMultiLanguageTest"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><StackPanel Margin="5,10,5,5"><Button Content="{lex:BLoc 英语}" Width="200" Click="BtnChangeEnglish" Margin="5"/><Button Name="btnChinese" Content="中文" Width="200" Click="BtnChangeChinese" Margin="5"/></StackPanel></Grid> </Window>
4、后台语言切换代码如下
using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; 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; using WPFMultiLanguageTest.LocalLanguage;namespace WPFMultiLanguageTest {/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();WPFLocalizeExtension.Engine.LocalizeDictionary.Instance.Culture = new CultureInfo("zh");}private void BtnChangeEnglish(object sender, RoutedEventArgs e){WPFLocalizeExtension.Engine.LocalizeDictionary.Instance.Culture = new CultureInfo("en");MessageBox.Show("切换英语");btnChinese.Content = SysLan.Get("中文");}private void BtnChangeChinese(object sender, RoutedEventArgs e){WPFLocalizeExtension.Engine.LocalizeDictionary.Instance.Culture = new CultureInfo("zh");MessageBox.Show("切换中文");btnChinese.Content= SysLan.Get("中文");}} }
5、资源文件中添加所需资源的键值对信息
参考文章:WPFLocalizeExtension 多语言组件使用和原理及拓展 - YYAN1987 - 博客园,感谢作者的分享。
如果您有更多或更好的思路请多多指导我,本人还有很多需要学习的地方。
以上为本次学习的全部内容,谢谢阅读!