LocaleManager.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Ryujinx.Ava.Ui.ViewModels;
  2. using Ryujinx.Common;
  3. using Ryujinx.Ui.Common.Configuration;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Text.Json;
  8. namespace Ryujinx.Ava.Common.Locale
  9. {
  10. class LocaleManager : BaseModel
  11. {
  12. private const string DefaultLanguageCode = "en_US";
  13. private Dictionary<string, string> _localeStrings;
  14. private ConcurrentDictionary<string, object[]> _dynamicValues;
  15. public static LocaleManager Instance { get; } = new LocaleManager();
  16. public Dictionary<string, string> LocaleStrings { get => _localeStrings; set => _localeStrings = value; }
  17. public LocaleManager()
  18. {
  19. _localeStrings = new Dictionary<string, string>();
  20. _dynamicValues = new ConcurrentDictionary<string, object[]>();
  21. Load();
  22. }
  23. public void Load()
  24. {
  25. string localeLanguageCode = CultureInfo.CurrentCulture.Name.Replace('-', '_');
  26. if (Program.PreviewerDetached)
  27. {
  28. if (!string.IsNullOrEmpty(ConfigurationState.Instance.Ui.LanguageCode.Value))
  29. {
  30. localeLanguageCode = ConfigurationState.Instance.Ui.LanguageCode.Value;
  31. }
  32. }
  33. // Load english first, if the target language translation is incomplete, we default to english.
  34. LoadDefaultLanguage();
  35. if (localeLanguageCode != DefaultLanguageCode)
  36. {
  37. LoadLanguage(localeLanguageCode);
  38. }
  39. }
  40. public string this[string key]
  41. {
  42. get
  43. {
  44. if (_localeStrings.TryGetValue(key, out string value))
  45. {
  46. if (_dynamicValues.TryGetValue(key, out var dynamicValue))
  47. {
  48. return string.Format(value, dynamicValue);
  49. }
  50. return value;
  51. }
  52. return key;
  53. }
  54. set
  55. {
  56. _localeStrings[key] = value;
  57. OnPropertyChanged();
  58. }
  59. }
  60. public void UpdateDynamicValue(string key, params object[] values)
  61. {
  62. _dynamicValues[key] = values;
  63. OnPropertyChanged("Item");
  64. }
  65. public void LoadDefaultLanguage()
  66. {
  67. LoadLanguage(DefaultLanguageCode);
  68. }
  69. public void LoadLanguage(string languageCode)
  70. {
  71. string languageJson = EmbeddedResources.ReadAllText($"Ryujinx.Ava/Assets/Locales/{languageCode}.json");
  72. if (languageJson == null)
  73. {
  74. return;
  75. }
  76. var strings = JsonSerializer.Deserialize<Dictionary<string, string>>(languageJson);
  77. foreach (var item in strings)
  78. {
  79. this[item.Key] = item.Value;
  80. }
  81. if (Program.PreviewerDetached)
  82. {
  83. ConfigurationState.Instance.Ui.LanguageCode.Value = languageCode;
  84. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  85. }
  86. }
  87. }
  88. }