diff options
| author | Ac_K <Acoustik666@gmail.com> | 2023-01-21 02:06:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-21 02:06:19 +0100 |
| commit | 8474d52778d6bc45146b59a1fc921e6702f4b96a (patch) | |
| tree | ce34a56ec8a2296bf3e5f891853a31a8d6bab6e0 /Ryujinx.Ava/Common | |
| parent | dd7a924596ff5925baa8b7f3ec85ceda8cb1cd8a (diff) | |
Ava UI: Fix `string.Format` issues in Locale (#4305)
* Ava UI: Fix `string.Format` issues in Locale
* LoacLanguage everytime now
* Apply suggestions from code review
Co-authored-by: TSRBerry <20988865+TSRBerry@users.noreply.github.com>
* fix UpdateAndGetDynamicValue
Co-authored-by: TSRBerry <20988865+TSRBerry@users.noreply.github.com>
Diffstat (limited to 'Ryujinx.Ava/Common')
| -rw-r--r-- | Ryujinx.Ava/Common/ApplicationHelper.cs | 7 | ||||
| -rw-r--r-- | Ryujinx.Ava/Common/Locale/LocaleExtension.cs | 2 | ||||
| -rw-r--r-- | Ryujinx.Ava/Common/Locale/LocaleManager.cs | 84 |
3 files changed, 62 insertions, 31 deletions
diff --git a/Ryujinx.Ava/Common/ApplicationHelper.cs b/Ryujinx.Ava/Common/ApplicationHelper.cs index 8e5bdaec..2dc3d37f 100644 --- a/Ryujinx.Ava/Common/ApplicationHelper.cs +++ b/Ryujinx.Ava/Common/ApplicationHelper.cs @@ -80,8 +80,7 @@ namespace Ryujinx.Ava.Common { Dispatcher.UIThread.Post(async () => { - await ContentDialogHelper.CreateErrorDialog( - string.Format(LocaleManager.Instance[LocaleKeys.DialogMessageCreateSaveErrorMessage], result.ToStringWithName())); + await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.DialogMessageCreateSaveErrorMessage, result.ToStringWithName())); }); return false; @@ -100,7 +99,7 @@ namespace Ryujinx.Ava.Common Dispatcher.UIThread.Post(async () => { - await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance[LocaleKeys.DialogMessageFindSaveErrorMessage], result.ToStringWithName())); + await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.DialogMessageFindSaveErrorMessage, result.ToStringWithName())); }); return false; @@ -164,7 +163,7 @@ namespace Ryujinx.Ava.Common Dispatcher.UIThread.Post(async () => { UserResult result = await ContentDialogHelper.CreateConfirmationDialog( - string.Format(LocaleManager.Instance[LocaleKeys.DialogNcaExtractionMessage], ncaSectionType, Path.GetFileName(titleFilePath)), + LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.DialogNcaExtractionMessage, ncaSectionType, Path.GetFileName(titleFilePath)), "", "", LocaleManager.Instance[LocaleKeys.InputDialogCancel], diff --git a/Ryujinx.Ava/Common/Locale/LocaleExtension.cs b/Ryujinx.Ava/Common/Locale/LocaleExtension.cs index 6c54becd..b82c405d 100644 --- a/Ryujinx.Ava/Common/Locale/LocaleExtension.cs +++ b/Ryujinx.Ava/Common/Locale/LocaleExtension.cs @@ -20,7 +20,7 @@ namespace Ryujinx.Ava.Common.Locale ReflectionBindingExtension binding = new($"[{keyToUse}]") { - Mode = BindingMode.OneWay, + Mode = BindingMode.OneWay, Source = LocaleManager.Instance }; diff --git a/Ryujinx.Ava/Common/Locale/LocaleManager.cs b/Ryujinx.Ava/Common/Locale/LocaleManager.cs index 5bcaa437..1374bfee 100644 --- a/Ryujinx.Ava/Common/Locale/LocaleManager.cs +++ b/Ryujinx.Ava/Common/Locale/LocaleManager.cs @@ -13,56 +13,87 @@ namespace Ryujinx.Ava.Common.Locale { private const string DefaultLanguageCode = "en_US"; - private Dictionary<LocaleKeys, string> _localeStrings; - private ConcurrentDictionary<LocaleKeys, object[]> _dynamicValues; + private Dictionary<LocaleKeys, string> _localeStrings; + private Dictionary<LocaleKeys, string> _localeDefaultStrings; + private readonly ConcurrentDictionary<LocaleKeys, object[]> _dynamicValues; public static LocaleManager Instance { get; } = new LocaleManager(); - public Dictionary<LocaleKeys, string> LocaleStrings { get => _localeStrings; set => _localeStrings = value; } - public LocaleManager() { - _localeStrings = new Dictionary<LocaleKeys, string>(); - _dynamicValues = new ConcurrentDictionary<LocaleKeys, object[]>(); + _localeStrings = new Dictionary<LocaleKeys, string>(); + _localeDefaultStrings = new Dictionary<LocaleKeys, string>(); + _dynamicValues = new ConcurrentDictionary<LocaleKeys, object[]>(); Load(); } public void Load() { + // Load the system Language Code. string localeLanguageCode = CultureInfo.CurrentCulture.Name.Replace('-', '_'); + // If the view is loaded with the UI Previewer detached, then override it with the saved one or default. if (Program.PreviewerDetached) { if (!string.IsNullOrEmpty(ConfigurationState.Instance.Ui.LanguageCode.Value)) { localeLanguageCode = ConfigurationState.Instance.Ui.LanguageCode.Value; } + else + { + localeLanguageCode = DefaultLanguageCode; + } } - // Load english first, if the target language translation is incomplete, we default to english. + // Load en_US as default, if the target language translation is incomplete. LoadDefaultLanguage(); - if (localeLanguageCode != DefaultLanguageCode) - { - LoadLanguage(localeLanguageCode); - } + LoadLanguage(localeLanguageCode); } public string this[LocaleKeys key] { get { + // Check if the locale contains the key. if (_localeStrings.TryGetValue(key, out string value)) { + // Check if the localized string needs to be formatted. if (_dynamicValues.TryGetValue(key, out var dynamicValue)) { - return string.Format(value, dynamicValue); + try + { + return string.Format(value, dynamicValue); + } + catch (Exception) + { + // If formatting failed use the default text instead. + if (_localeDefaultStrings.TryGetValue(key, out value)) + { + try + { + return string.Format(value, dynamicValue); + } + catch (Exception) + { + // If formatting the default text failed return the key. + return key.ToString(); + } + } + } } return value; } + // If the locale doesn't contain the key return the default one. + if (_localeDefaultStrings.TryGetValue(key, out string defaultValue)) + { + return defaultValue; + } + + // If the locale text doesn't exist return the key. return key.ToString(); } set @@ -73,42 +104,43 @@ namespace Ryujinx.Ava.Common.Locale } } - public void UpdateDynamicValue(LocaleKeys key, params object[] values) + public string UpdateAndGetDynamicValue(LocaleKeys key, params object[] values) { _dynamicValues[key] = values; OnPropertyChanged("Item"); + + return this[key]; } - public void LoadDefaultLanguage() + private void LoadDefaultLanguage() { - LoadLanguage(DefaultLanguageCode); + _localeDefaultStrings = LoadJsonLanguage(); } public void LoadLanguage(string languageCode) { - string languageJson = EmbeddedResources.ReadAllText($"Ryujinx.Ava/Assets/Locales/{languageCode}.json"); - - if (languageJson == null) + foreach (var item in LoadJsonLanguage(languageCode)) { - return; + this[item.Key] = item.Value; } + } - var strings = JsonHelper.Deserialize<Dictionary<string, string>>(languageJson); + private Dictionary<LocaleKeys, string> LoadJsonLanguage(string languageCode = DefaultLanguageCode) + { + var localeStrings = new Dictionary<LocaleKeys, string>(); + string languageJson = EmbeddedResources.ReadAllText($"Ryujinx.Ava/Assets/Locales/{languageCode}.json"); + var strings = JsonHelper.Deserialize<Dictionary<string, string>>(languageJson); foreach (var item in strings) { if (Enum.TryParse<LocaleKeys>(item.Key, out var key)) { - this[key] = item.Value; + localeStrings[key] = item.Value; } } - if (Program.PreviewerDetached) - { - ConfigurationState.Instance.Ui.LanguageCode.Value = languageCode; - ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath); - } + return localeStrings; } } }
\ No newline at end of file |
