2022-12-29 14:24:05 +00:00
|
|
|
|
using Ryujinx.Ava.UI.ViewModels;
|
2022-05-15 11:30:15 +00:00
|
|
|
|
using Ryujinx.Common;
|
2022-11-23 17:55:26 +00:00
|
|
|
|
using Ryujinx.Common.Utilities;
|
2022-05-15 11:30:15 +00:00
|
|
|
|
using Ryujinx.Ui.Common.Configuration;
|
2023-01-03 18:45:08 +00:00
|
|
|
|
using System;
|
2022-05-15 11:30:15 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Ava.Common.Locale
|
|
|
|
|
{
|
|
|
|
|
class LocaleManager : BaseModel
|
|
|
|
|
{
|
|
|
|
|
private const string DefaultLanguageCode = "en_US";
|
|
|
|
|
|
2023-01-21 01:06:19 +00:00
|
|
|
|
private Dictionary<LocaleKeys, string> _localeStrings;
|
|
|
|
|
private Dictionary<LocaleKeys, string> _localeDefaultStrings;
|
|
|
|
|
private readonly ConcurrentDictionary<LocaleKeys, object[]> _dynamicValues;
|
2022-05-15 11:30:15 +00:00
|
|
|
|
|
|
|
|
|
public static LocaleManager Instance { get; } = new LocaleManager();
|
|
|
|
|
|
|
|
|
|
public LocaleManager()
|
|
|
|
|
{
|
2023-01-21 01:06:19 +00:00
|
|
|
|
_localeStrings = new Dictionary<LocaleKeys, string>();
|
|
|
|
|
_localeDefaultStrings = new Dictionary<LocaleKeys, string>();
|
|
|
|
|
_dynamicValues = new ConcurrentDictionary<LocaleKeys, object[]>();
|
2022-05-15 11:30:15 +00:00
|
|
|
|
|
|
|
|
|
Load();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Load()
|
|
|
|
|
{
|
2023-01-21 01:06:19 +00:00
|
|
|
|
// Load the system Language Code.
|
2022-05-15 11:30:15 +00:00
|
|
|
|
string localeLanguageCode = CultureInfo.CurrentCulture.Name.Replace('-', '_');
|
|
|
|
|
|
2023-01-21 01:06:19 +00:00
|
|
|
|
// If the view is loaded with the UI Previewer detached, then override it with the saved one or default.
|
2022-05-15 11:30:15 +00:00
|
|
|
|
if (Program.PreviewerDetached)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(ConfigurationState.Instance.Ui.LanguageCode.Value))
|
|
|
|
|
{
|
|
|
|
|
localeLanguageCode = ConfigurationState.Instance.Ui.LanguageCode.Value;
|
|
|
|
|
}
|
2023-01-21 01:06:19 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
localeLanguageCode = DefaultLanguageCode;
|
|
|
|
|
}
|
2022-05-15 11:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 01:06:19 +00:00
|
|
|
|
// Load en_US as default, if the target language translation is incomplete.
|
2022-07-05 18:06:31 +00:00
|
|
|
|
LoadDefaultLanguage();
|
2022-05-15 11:30:15 +00:00
|
|
|
|
|
2023-01-21 01:06:19 +00:00
|
|
|
|
LoadLanguage(localeLanguageCode);
|
2022-05-15 11:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-03 18:45:08 +00:00
|
|
|
|
public string this[LocaleKeys key]
|
2022-05-15 11:30:15 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2023-01-21 01:06:19 +00:00
|
|
|
|
// Check if the locale contains the key.
|
2022-05-15 11:30:15 +00:00
|
|
|
|
if (_localeStrings.TryGetValue(key, out string value))
|
|
|
|
|
{
|
2023-01-21 01:06:19 +00:00
|
|
|
|
// Check if the localized string needs to be formatted.
|
2022-05-15 11:30:15 +00:00
|
|
|
|
if (_dynamicValues.TryGetValue(key, out var dynamicValue))
|
|
|
|
|
{
|
2023-01-21 01:06:19 +00:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-15 11:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 01:06:19 +00:00
|
|
|
|
// 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.
|
2023-01-03 18:45:08 +00:00
|
|
|
|
return key.ToString();
|
2022-05-15 11:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_localeStrings[key] = value;
|
|
|
|
|
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 01:06:19 +00:00
|
|
|
|
public string UpdateAndGetDynamicValue(LocaleKeys key, params object[] values)
|
2022-05-15 11:30:15 +00:00
|
|
|
|
{
|
|
|
|
|
_dynamicValues[key] = values;
|
|
|
|
|
|
|
|
|
|
OnPropertyChanged("Item");
|
2023-01-21 01:06:19 +00:00
|
|
|
|
|
|
|
|
|
return this[key];
|
2022-05-15 11:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 01:06:19 +00:00
|
|
|
|
private void LoadDefaultLanguage()
|
2022-07-05 18:06:31 +00:00
|
|
|
|
{
|
2023-01-21 01:06:19 +00:00
|
|
|
|
_localeDefaultStrings = LoadJsonLanguage();
|
2022-07-05 18:06:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-15 11:30:15 +00:00
|
|
|
|
public void LoadLanguage(string languageCode)
|
|
|
|
|
{
|
2023-01-21 01:06:19 +00:00
|
|
|
|
foreach (var item in LoadJsonLanguage(languageCode))
|
2022-05-15 11:30:15 +00:00
|
|
|
|
{
|
2023-01-21 01:06:19 +00:00
|
|
|
|
this[item.Key] = item.Value;
|
2022-05-15 11:30:15 +00:00
|
|
|
|
}
|
2023-01-21 01:06:19 +00:00
|
|
|
|
}
|
2022-05-15 11:30:15 +00:00
|
|
|
|
|
2023-01-21 01:06:19 +00:00
|
|
|
|
private Dictionary<LocaleKeys, string> LoadJsonLanguage(string languageCode = DefaultLanguageCode)
|
|
|
|
|
{
|
|
|
|
|
var localeStrings = new Dictionary<LocaleKeys, string>();
|
|
|
|
|
string languageJson = EmbeddedResources.ReadAllText($"Ryujinx.Ava/Assets/Locales/{languageCode}.json");
|
2023-03-21 22:41:19 +00:00
|
|
|
|
var strings = JsonHelper.Deserialize(languageJson, CommonJsonContext.Default.StringDictionary);
|
2022-05-15 11:30:15 +00:00
|
|
|
|
|
|
|
|
|
foreach (var item in strings)
|
|
|
|
|
{
|
2023-01-03 18:45:08 +00:00
|
|
|
|
if (Enum.TryParse<LocaleKeys>(item.Key, out var key))
|
|
|
|
|
{
|
2023-01-21 01:06:19 +00:00
|
|
|
|
localeStrings[key] = item.Value;
|
2023-01-03 18:45:08 +00:00
|
|
|
|
}
|
2022-05-15 11:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 01:06:19 +00:00
|
|
|
|
return localeStrings;
|
2022-05-15 11:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|