C# WPF程序引用WPFLocalizeExtension第三方库实现全球化
1 public static class LangHelper 2 { 3 /// <summary> 4 /// 根据关键字获取当前语言环境下的提示信息 5 /// </summary> 6 /// <param name="key"></param> 7 /// <param name="resourceFileName"></param> 8 /// <param name="addSpaceAfter"></param> 9 /// <returns></returns> 10 public static string GetLocalizedString(string key, string resourceFileName = "Langs", bool addSpaceAfter = false) 11 { 12 var localizedString = String.Empty; 13 14 // Build up the fully-qualified name of the key 15 var assemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; 16 var fullKey = assemblyName + ":" + resourceFileName + ":" + key; 17 var locExtension = new LocExtension(fullKey); 18 locExtension.ResolveLocalizedValue(out localizedString); 19 20 // Add a space to the end, if requested 21 if (addSpaceAfter) 22 { 23 localizedString += " "; 24 } 25 26 return localizedString; 27 } 28 }
1 <Window 2 ... 3 xmlns:lex="http://wpflocalizeextension.codeplex.com" 4 lex:LocalizeDictionary.DesignCulture="en-US" 5 lex:ResxLocalizationProvider.DefaultAssembly="AppSample" 6 lex:ResxLocalizationProvider.DefaultDictionary="Langs"> 7 <!-- 解释一下 --> 8 <!-- 9 lex:LocalizeDictionary.DesignCulture="en-US" 默认en-US英文,zh-CN中文,这里采用英文 10 lex:ResxLocalizationProvider.DefaultAssembly="AppSample" AppSample为程序集名称 11 lex:ResxLocalizationProvider.DefaultDictionary="Langs" 找不到时,去找Langs资源下 12 --> 13 <!-- 其他代码 --> 14 </Window>
4 GitHub
4.1 LocalizeExtension开源地址
https://github.com/XAMLMarkupExtensions/WPFLocalizationExtension
4.2 测试Demo地址
https://github.com/JeremyWu917/DynamicChangeLanguageSample
作者:Jeremy.Wu
出处:https://www.cnblogs.com/jeremywucnblog/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。