LanguageLoader.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. namespace HistoryView.Helper;
  2. public partial class LanguageLoader : ObservableObject
  3. {
  4. [ObservableProperty]
  5. private ObservableDictionary<string, Dictionary<object, object>> _Languages = [];
  6. [ObservableProperty]
  7. private object? _SelectedLanguage;
  8. public static string DefaultLanguage { get; } = "中文";
  9. partial void OnSelectedLanguageChanged(object? value)
  10. {
  11. if (value is not KeyValuePair<string, Dictionary<object, object>> items)
  12. return;
  13. foreach (var item in items.Value)
  14. {
  15. if (!App.Current.Resources.Contains(item.Key))
  16. continue;
  17. App.Current.Dispatcher.Invoke(() =>
  18. {
  19. App.Current.Resources[item.Key] = item.Key switch
  20. {
  21. "LanguageFlag" => new BitmapImage(new Uri(Path.Combine(FilePaths.LanguageBasePath, item.Value.ToString()!))),
  22. _ => item.Value,
  23. };
  24. });
  25. }
  26. }
  27. public void LoadLanguages(string path)
  28. {
  29. this.Languages = [];
  30. List<string> sheetNames = MiniExcel.GetSheetNames(path);
  31. foreach (string sheet in sheetNames)
  32. {
  33. Dictionary<object, object> tempLang = [];
  34. var rows = MiniExcel.Query(path, sheetName: sheet).Cast<IDictionary<string, object>>();
  35. foreach (var row in rows)
  36. {
  37. if (!row.TryGetValue("A", out object? A) || !row.TryGetValue("B", out object? B))
  38. continue;
  39. if (A is null || B is null)
  40. continue;
  41. tempLang[A] = B;
  42. }
  43. this.Languages[sheet] = tempLang;
  44. }
  45. foreach (var item in this.Languages)
  46. {
  47. if (item.Key != DefaultLanguage)
  48. continue;
  49. this.SelectedLanguage = item;
  50. }
  51. }
  52. }