LocaleManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Ryujinx.Ava.UI.ViewModels;
  2. using Ryujinx.Common;
  3. using Ryujinx.Common.Utilities;
  4. using Ryujinx.Ui.Common.Configuration;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.Globalization;
  9. namespace Ryujinx.Ava.Common.Locale
  10. {
  11. class LocaleManager : BaseModel
  12. {
  13. private const string DefaultLanguageCode = "en_US";
  14. private Dictionary<LocaleKeys, string> _localeStrings;
  15. private Dictionary<LocaleKeys, string> _localeDefaultStrings;
  16. private readonly ConcurrentDictionary<LocaleKeys, object[]> _dynamicValues;
  17. public static LocaleManager Instance { get; } = new LocaleManager();
  18. public LocaleManager()
  19. {
  20. _localeStrings = new Dictionary<LocaleKeys, string>();
  21. _localeDefaultStrings = new Dictionary<LocaleKeys, string>();
  22. _dynamicValues = new ConcurrentDictionary<LocaleKeys, object[]>();
  23. Load();
  24. }
  25. public void Load()
  26. {
  27. // Load the system Language Code.
  28. string localeLanguageCode = CultureInfo.CurrentCulture.Name.Replace('-', '_');
  29. // If the view is loaded with the UI Previewer detached, then override it with the saved one or default.
  30. if (Program.PreviewerDetached)
  31. {
  32. if (!string.IsNullOrEmpty(ConfigurationState.Instance.Ui.LanguageCode.Value))
  33. {
  34. localeLanguageCode = ConfigurationState.Instance.Ui.LanguageCode.Value;
  35. }
  36. else
  37. {
  38. localeLanguageCode = DefaultLanguageCode;
  39. }
  40. }
  41. // Load en_US as default, if the target language translation is incomplete.
  42. LoadDefaultLanguage();
  43. LoadLanguage(localeLanguageCode);
  44. }
  45. public string this[LocaleKeys key]
  46. {
  47. get
  48. {
  49. // Check if the locale contains the key.
  50. if (_localeStrings.TryGetValue(key, out string value))
  51. {
  52. // Check if the localized string needs to be formatted.
  53. if (_dynamicValues.TryGetValue(key, out var dynamicValue))
  54. {
  55. try
  56. {
  57. return string.Format(value, dynamicValue);
  58. }
  59. catch (Exception)
  60. {
  61. // If formatting failed use the default text instead.
  62. if (_localeDefaultStrings.TryGetValue(key, out value))
  63. {
  64. try
  65. {
  66. return string.Format(value, dynamicValue);
  67. }
  68. catch (Exception)
  69. {
  70. // If formatting the default text failed return the key.
  71. return key.ToString();
  72. }
  73. }
  74. }
  75. }
  76. return value;
  77. }
  78. // If the locale doesn't contain the key return the default one.
  79. if (_localeDefaultStrings.TryGetValue(key, out string defaultValue))
  80. {
  81. return defaultValue;
  82. }
  83. // If the locale text doesn't exist return the key.
  84. return key.ToString();
  85. }
  86. set
  87. {
  88. _localeStrings[key] = value;
  89. OnPropertyChanged();
  90. }
  91. }
  92. public string UpdateAndGetDynamicValue(LocaleKeys key, params object[] values)
  93. {
  94. _dynamicValues[key] = values;
  95. OnPropertyChanged("Item");
  96. return this[key];
  97. }
  98. private void LoadDefaultLanguage()
  99. {
  100. _localeDefaultStrings = LoadJsonLanguage();
  101. }
  102. public void LoadLanguage(string languageCode)
  103. {
  104. foreach (var item in LoadJsonLanguage(languageCode))
  105. {
  106. this[item.Key] = item.Value;
  107. }
  108. }
  109. private Dictionary<LocaleKeys, string> LoadJsonLanguage(string languageCode = DefaultLanguageCode)
  110. {
  111. var localeStrings = new Dictionary<LocaleKeys, string>();
  112. string languageJson = EmbeddedResources.ReadAllText($"Ryujinx.Ava/Assets/Locales/{languageCode}.json");
  113. var strings = JsonHelper.Deserialize(languageJson, CommonJsonContext.Default.StringDictionary);
  114. foreach (var item in strings)
  115. {
  116. if (Enum.TryParse<LocaleKeys>(item.Key, out var key))
  117. {
  118. localeStrings[key] = item.Value;
  119. }
  120. }
  121. return localeStrings;
  122. }
  123. }
  124. }