LocaleManager.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using Ryujinx.Ava.UI.ViewModels;
  2. using Ryujinx.Common;
  3. using Ryujinx.Common.Logging;
  4. using Ryujinx.Common.Utilities;
  5. using Ryujinx.UI.Common.Configuration;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text.Encodings.Web;
  13. using System.Text.Json;
  14. using System.Text.Json.Serialization;
  15. using System.Text.Unicode;
  16. namespace Ryujinx.Ava.Common.Locale
  17. {
  18. class LocaleManager : BaseModel
  19. {
  20. private const string DefaultLanguageCode = "en_US";
  21. private readonly Dictionary<LocaleKeys, string> _localeStrings;
  22. private Dictionary<LocaleKeys, string> _localeDefaultStrings;
  23. private readonly ConcurrentDictionary<LocaleKeys, object[]> _dynamicValues;
  24. private string _localeLanguageCode;
  25. public static LocaleManager Instance { get; } = new();
  26. public event Action LocaleChanged;
  27. public LocaleManager()
  28. {
  29. _localeStrings = new Dictionary<LocaleKeys, string>();
  30. _localeDefaultStrings = new Dictionary<LocaleKeys, string>();
  31. _dynamicValues = new ConcurrentDictionary<LocaleKeys, object[]>();
  32. Load();
  33. }
  34. private void Load()
  35. {
  36. var localeLanguageCode = !string.IsNullOrEmpty(ConfigurationState.Instance.UI.LanguageCode.Value) ?
  37. ConfigurationState.Instance.UI.LanguageCode.Value : CultureInfo.CurrentCulture.Name.Replace('-', '_');
  38. // Load en_US as default, if the target language translation is missing or incomplete.
  39. LoadDefaultLanguage();
  40. LoadLanguage(localeLanguageCode);
  41. // Save whatever we ended up with.
  42. if (Program.PreviewerDetached)
  43. {
  44. ConfigurationState.Instance.UI.LanguageCode.Value = _localeLanguageCode;
  45. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  46. }
  47. }
  48. public string this[LocaleKeys key]
  49. {
  50. get
  51. {
  52. // Check if the locale contains the key.
  53. if (_localeStrings.TryGetValue(key, out string value))
  54. {
  55. // Check if the localized string needs to be formatted.
  56. if (_dynamicValues.TryGetValue(key, out var dynamicValue))
  57. try
  58. {
  59. return string.Format(value, dynamicValue);
  60. }
  61. catch
  62. {
  63. // If formatting failed use the default text instead.
  64. if (_localeDefaultStrings.TryGetValue(key, out value))
  65. try
  66. {
  67. return string.Format(value, dynamicValue);
  68. }
  69. catch
  70. {
  71. // If formatting the default text failed return the key.
  72. return key.ToString();
  73. }
  74. }
  75. return value;
  76. }
  77. // If the locale doesn't contain the key return the default one.
  78. return _localeDefaultStrings.TryGetValue(key, out string defaultValue)
  79. ? defaultValue
  80. : key.ToString(); // If the locale text doesn't exist return the key.
  81. }
  82. set
  83. {
  84. _localeStrings[key] = value;
  85. OnPropertyChanged();
  86. }
  87. }
  88. public bool IsRTL() =>
  89. _localeLanguageCode switch
  90. {
  91. "ar_SA" or "he_IL" => true,
  92. _ => false
  93. };
  94. public static string FormatDynamicValue(LocaleKeys key, params object[] values)
  95. => Instance.UpdateAndGetDynamicValue(key, values);
  96. public string UpdateAndGetDynamicValue(LocaleKeys key, params object[] values)
  97. {
  98. _dynamicValues[key] = values;
  99. OnPropertyChanged("Item");
  100. return this[key];
  101. }
  102. private void LoadDefaultLanguage()
  103. {
  104. _localeDefaultStrings = LoadJsonLanguage(DefaultLanguageCode);
  105. }
  106. public void LoadLanguage(string languageCode)
  107. {
  108. var locale = LoadJsonLanguage(languageCode);
  109. if (locale == null)
  110. {
  111. _localeLanguageCode = DefaultLanguageCode;
  112. locale = _localeDefaultStrings;
  113. }
  114. else
  115. {
  116. _localeLanguageCode = languageCode;
  117. }
  118. foreach ((LocaleKeys key, string val) in locale)
  119. {
  120. _localeStrings[key] = val;
  121. }
  122. OnPropertyChanged("Item");
  123. LocaleChanged?.Invoke();
  124. }
  125. private static Dictionary<LocaleKeys, string> LoadJsonLanguage(string languageCode)
  126. {
  127. var localeStrings = new Dictionary<LocaleKeys, string>();
  128. string fileData = EmbeddedResources.ReadAllText($"Ryujinx/Assets/locales.json");
  129. if (fileData == null)
  130. {
  131. // We were unable to find file for that language code.
  132. return null;
  133. }
  134. LocalesJson json = JsonHelper.Deserialize(fileData, LocalesJsonContext.Default.LocalesJson);
  135. foreach (LocalesEntry locale in json.Locales)
  136. {
  137. if (locale.Translations.Count != json.Languages.Count)
  138. {
  139. Logger.Error?.Print(LogClass.UI, $"Locale key {{{locale.ID}}} is missing languages!");
  140. throw new Exception("Missing locale data!");
  141. }
  142. if (Enum.TryParse<LocaleKeys>(locale.ID, out var localeKey))
  143. {
  144. if (locale.Translations.TryGetValue(languageCode, out string val) && val != "")
  145. {
  146. localeStrings[localeKey] = val;
  147. }
  148. else
  149. {
  150. locale.Translations.TryGetValue("en_US", out val);
  151. localeStrings[localeKey] = val;
  152. }
  153. }
  154. }
  155. return localeStrings;
  156. }
  157. }
  158. public struct LocalesJson
  159. {
  160. public List<string> Languages { get; set; }
  161. public List<LocalesEntry> Locales { get; set; }
  162. }
  163. public struct LocalesEntry
  164. {
  165. public string ID { get; set; }
  166. public Dictionary<string, string> Translations { get; set; }
  167. }
  168. [JsonSourceGenerationOptions(WriteIndented = true)]
  169. [JsonSerializable(typeof(LocalesJson))]
  170. internal partial class LocalesJsonContext : JsonSerializerContext { }
  171. }