| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace CCDCountWpf.Language
- {
- public static class LanguageManager
- {
- public static event EventHandler LanguageChangeEvent;
- private static int _nowLoadLanguage = 0;
- public static int NowLoadLanguage {
- get
- {
- return _nowLoadLanguage;
- }
- }
- public static List<string> LanguageList
- {
- get
- {
- return new List<string>()
- {
- "Chinese",
- "English"
- };
- }
- }
- public static void ChangeLanguage(string languageCode)
- {
- // 获取现有的非语言资源字典(如样式)
- var existingNonLanguageDictionaries = Application.Current.Resources.MergedDictionaries
- .Where(rd => !IsLanguageResourceDictionary(rd))
- .ToList();
- // 清除现有字典并重新添加
- Application.Current.Resources.MergedDictionaries.Clear();
- // 先添加语言资源字典
- var languageResourceDict = new ResourceDictionary();
- switch (languageCode)
- {
- case "Chinese":
- languageResourceDict.Source = new Uri("/Language/String_Chinese.xaml", UriKind.Relative);
- _nowLoadLanguage = 0;
- break;
- case "English":
- languageResourceDict.Source = new Uri("/Language/String_English.xaml", UriKind.Relative);
- _nowLoadLanguage = 1;
- break;
- }
- Application.Current.Resources.MergedDictionaries.Add(languageResourceDict);
- // 再添加其他非语言资源字典(如样式)
- foreach (var dict in existingNonLanguageDictionaries)
- {
- Application.Current.Resources.MergedDictionaries.Add(dict);
- }
- OnLanguageChangeEvent();
- }
- // 辅助方法:判断是否为语言资源字典
- private static bool IsLanguageResourceDictionary(ResourceDictionary resourceDict)
- {
- if (resourceDict.Source == null) return false;
- var sourcePath = resourceDict.Source.ToString().ToLower();
- return sourcePath.Contains("/language/string_") ||
- sourcePath.Contains("\\language\\string_");
- }
- private static void OnLanguageChangeEvent()
- {
- LanguageChangeEvent?.Invoke(null, EventArgs.Empty);
- }
- }
- }
|