using System.Windows; using HandyControl.Tools; namespace SparkClient.Model.Helper; /// /// 多语言工具类 /// public class MultilingualHelper { /// /// 获取多语言字符 /// /// KEY /// 文字 /// public static string getString(string key) { var result = Application.Current.TryFindResource(key); if (result == null) { throw new NullReferenceException(key + " is not a valid key"); } return (string)result; } /// /// 设置语言 /// /// public static void setLanguage(string language) { LoadResources(language); ConfigHelper.Instance.SetLang(language); } /// /// 加载语言配置 /// /// private static void LoadResources(string resourceKey) { // 获取当前应用程序的App实例 var app = (App)Application.Current; // 根据传入的resourceKey获取对应的资源字典 var resourceDictionary = app.Resources[resourceKey] as ResourceDictionary; if (resourceDictionary!= null) { // 获取资源字典的源文件路径(即资源文件的位置) string requestedCulture = resourceDictionary.Source.OriginalString; // 在已合并的资源字典中查找对应的资源字典 var mergedDictionary = Application.Current.Resources.MergedDictionaries.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedCulture)); if (mergedDictionary!= null) { // 先移除当前对应的资源字典 Application.Current.Resources.MergedDictionaries.Remove(mergedDictionary); // 再添加回去,以更新资源 Application.Current.Resources.MergedDictionaries.Add(mergedDictionary); } else { // 如果未找到对应的资源字典,给出提示信息 MessageBox.Show($"We could not find the {resourceKey} resource, check the multi-language configuration"); } } else { // 如果未找到对应的资源字典(根据传入的resourceKey),给出提示信息 MessageBox.Show($"We could not find the {resourceKey} resource, check the multi-language configuration"); } } }