LanguageManager.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Windows;
  6. using MuchInfo.Chart.Data.EnumTypes;
  7. using MuchInfo.Chart.DataAdapter;
  8. namespace MuchInfo.Chart.WPF.Helpers
  9. {
  10. public class LanguageManager
  11. {
  12. #region Methods
  13. #region Public Static Methods
  14. /// <summary>
  15. /// 查找资料
  16. /// </summary>
  17. /// <param name="key">The key.</param>
  18. /// <returns>System.String.</returns>
  19. public static string FindResource(string key)
  20. {
  21. try
  22. {
  23. var findResource = Application.Current.FindResource(key);
  24. if (findResource == null) return string.Empty;
  25. return findResource.ToString();
  26. }
  27. catch
  28. {
  29. return string.Empty;
  30. }
  31. }
  32. /// <summary>
  33. /// 设置图表语言
  34. /// </summary>
  35. /// <param name="cultureString">The culture string.</param>
  36. /// <exception cref="System.Exception">cannot find resource. + ex.ToString()</exception>
  37. public static void SetLanguage(string cultureString)
  38. {
  39. try
  40. {
  41. var dictionaryList = Application.Current.Resources.MergedDictionaries.ToList();
  42. //先删除languages文件夹下加载了的资源
  43. var languageDic = dictionaryList.Where(z => z.Source != null && z.Source.OriginalString.StartsWith(LanguageConst.ResourceStart)).ToList();
  44. if (languageDic.Any())
  45. {
  46. foreach (var dictionary in languageDic)
  47. {
  48. Application.Current.Resources.MergedDictionaries.Remove(dictionary);
  49. }
  50. }
  51. //添加新资源
  52. var resourceDictionary = new ResourceDictionary
  53. {
  54. Source = new Uri(LanguageConst.ResourceStart + cultureString + ".xaml", UriKind.Relative)
  55. };
  56. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
  57. //更改整个环境语言
  58. var culture = new CultureInfo(cultureString);
  59. Thread.CurrentThread.CurrentCulture = culture;
  60. Thread.CurrentThread.CurrentUICulture = culture;
  61. }
  62. catch (Exception ex)
  63. {
  64. throw new Exception(ex.ToString());
  65. }
  66. }
  67. /// <summary>
  68. /// 设置编辑器的语言
  69. /// </summary>
  70. /// <param name="languageType"></param>
  71. public static void SetEditorLanguage(ChartLanguageType languageType)
  72. {
  73. var service = new DataService();
  74. service.SetEditorLanguage(languageType);
  75. }
  76. #endregion Public Static Methods
  77. #endregion Methods
  78. }
  79. }