You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
3.0 KiB
96 lines
3.0 KiB
using System.Windows; |
|
using HandyControl.Tools; |
|
|
|
namespace BrilliantSightClient.Model.Helper; |
|
|
|
/// <summary> |
|
/// 多语言工具类 |
|
/// </summary> |
|
public class MultilingualHelper |
|
{ |
|
/// <summary> |
|
/// 获取多语言字符 |
|
/// </summary> |
|
/// <param name="key">KEY</param> |
|
/// <returns>文字</returns> |
|
/// <exception cref="NullReferenceException"></exception> |
|
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; |
|
} |
|
|
|
/// <summary> |
|
/// 获取多语言字符 |
|
/// </summary> |
|
/// <returns>文字</returns> |
|
/// <exception cref="NullReferenceException"></exception> |
|
public static string getLangType() |
|
{ |
|
return ConfigHelper.Instance.Lang.ToString(); |
|
} |
|
|
|
/// <summary> |
|
/// 设置语言 |
|
/// </summary> |
|
/// <param name="language"></param> |
|
public static void setLanguage(string language) |
|
{ |
|
LoadResources(language); |
|
if ("zh-pro".Equals(language)) |
|
{ |
|
ConfigHelper.Instance.SetLang("zn-ch"); |
|
return; |
|
} |
|
|
|
ConfigHelper.Instance.SetLang(language); |
|
} |
|
|
|
/// <summary> |
|
/// 加载语言配置 |
|
/// </summary> |
|
/// <param name="resourceKey"></param> |
|
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"); |
|
} |
|
|
|
|
|
} |
|
|
|
} |