LocaleManager.cs 3.2 KB

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