CulturesHelper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. namespace AwInitilizer.Cultures
  10. {
  11. public enum Languages
  12. {
  13. English, TraditionalChinese, SimplfyChinese
  14. }
  15. public class CulturesHelper
  16. {
  17. private static Lazy<CulturesHelper> _Instance = new Lazy<CulturesHelper>(() => new CulturesHelper());
  18. public static CulturesHelper Instance
  19. {
  20. get
  21. {
  22. return _Instance.Value;
  23. }
  24. }
  25. private static string resourcePrefix = "StringResource";
  26. private static string culturesFolder = "Cultures";
  27. private static List<CultureInfo> _supportedCultures = new List<CultureInfo>();
  28. public static List<CultureInfo> SupportedCultures
  29. {
  30. get
  31. {
  32. return _supportedCultures;
  33. }
  34. }
  35. private static ResourceDictionary resourceDictionary;
  36. public static ResourceDictionary ResourceDictionary
  37. {
  38. get
  39. {
  40. return resourceDictionary;
  41. }
  42. }
  43. public Languages CurrentCulture { get; private set; }
  44. public event EventHandler<Languages> OnCultureChange;
  45. private CulturesHelper()
  46. {
  47. CultureInfo cultureInfo = new CultureInfo("");
  48. List<string> files = Directory.GetFiles(string.Format("{0}\\{1}", Directory.GetCurrentDirectory(), culturesFolder))
  49. .Where(s => s.Contains(resourcePrefix) && s.ToLower().EndsWith("xaml")).ToList();
  50. foreach (string file in files)
  51. {
  52. try
  53. {
  54. string cultureName = file.Substring(file.IndexOf(".") + 1).Replace(".xaml", "");
  55. cultureInfo = CultureInfo.GetCultureInfo(cultureName);
  56. if (cultureInfo != null)
  57. {
  58. _supportedCultures.Add(cultureInfo);
  59. }
  60. }
  61. catch (ArgumentException)
  62. {
  63. }
  64. }
  65. }
  66. public void ChangeCulture(CultureInfo culture)
  67. {
  68. if (_supportedCultures.Contains(culture))
  69. {
  70. string loadedFileName = string.Format("{0}\\{1}\\{2}.{3}.xaml",
  71. Directory.GetCurrentDirectory(),
  72. culturesFolder,
  73. resourcePrefix,
  74. culture.Name);
  75. //FileStream fileStream = new FileStream(loadedFileName, FileMode.Open);
  76. //resourceDictionary = XamlReader.Load(fileStream) as ResourceDictionary;
  77. resourceDictionary = new ResourceDictionary() { Source = new Uri(loadedFileName) };
  78. Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
  79. //MainWindow.window.Resources.MergedDictionaries.Add(resourceDictionary);
  80. //Properties.Settings.Default.DefaultCulture = culture;
  81. //Properties.Settings.Default.Save();
  82. if (culture.Name == "en-US")
  83. {
  84. CurrentCulture = Languages.English;
  85. }
  86. else if (culture.Name == "zh-TW")
  87. {
  88. CurrentCulture = Languages.TraditionalChinese;
  89. }
  90. else if (culture.Name == "zh-CHS")
  91. {
  92. CurrentCulture = Languages.SimplfyChinese;
  93. }
  94. OnCultureChange?.Invoke(this, CurrentCulture);
  95. }
  96. }
  97. }
  98. }