2022-12-29 15:24:05 +01:00
|
|
|
|
using Ryujinx.Ava.UI.ViewModels;
|
2022-05-15 13:30:15 +02:00
|
|
|
|
using Ryujinx.Common;
|
2022-11-23 18:55:26 +01:00
|
|
|
|
using Ryujinx.Common.Utilities;
|
2022-05-15 13:30:15 +02:00
|
|
|
|
using Ryujinx.Ui.Common.Configuration;
|
2023-01-03 19:45:08 +01:00
|
|
|
|
using System;
|
2022-05-15 13:30:15 +02: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-03 19:45:08 +01:00
|
|
|
|
private Dictionary<LocaleKeys, string> _localeStrings;
|
|
|
|
|
private ConcurrentDictionary<LocaleKeys, object[]> _dynamicValues;
|
2022-05-15 13:30:15 +02:00
|
|
|
|
|
|
|
|
|
public static LocaleManager Instance { get; } = new LocaleManager();
|
2023-01-03 19:45:08 +01:00
|
|
|
|
public Dictionary<LocaleKeys, string> LocaleStrings { get => _localeStrings; set => _localeStrings = value; }
|
2022-05-15 13:30:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public LocaleManager()
|
|
|
|
|
{
|
2023-01-03 19:45:08 +01:00
|
|
|
|
_localeStrings = new Dictionary<LocaleKeys, string>();
|
|
|
|
|
_dynamicValues = new ConcurrentDictionary<LocaleKeys, object[]>();
|
2022-05-15 13:30:15 +02:00
|
|
|
|
|
|
|
|
|
Load();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Load()
|
|
|
|
|
{
|
|
|
|
|
string localeLanguageCode = CultureInfo.CurrentCulture.Name.Replace('-', '_');
|
|
|
|
|
|
|
|
|
|
if (Program.PreviewerDetached)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(ConfigurationState.Instance.Ui.LanguageCode.Value))
|
|
|
|
|
{
|
|
|
|
|
localeLanguageCode = ConfigurationState.Instance.Ui.LanguageCode.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load english first, if the target language translation is incomplete, we default to english.
|
2022-07-05 20:06:31 +02:00
|
|
|
|
LoadDefaultLanguage();
|
2022-05-15 13:30:15 +02:00
|
|
|
|
|
|
|
|
|
if (localeLanguageCode != DefaultLanguageCode)
|
|
|
|
|
{
|
|
|
|
|
LoadLanguage(localeLanguageCode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-03 19:45:08 +01:00
|
|
|
|
public string this[LocaleKeys key]
|
2022-05-15 13:30:15 +02:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_localeStrings.TryGetValue(key, out string value))
|
|
|
|
|
{
|
|
|
|
|
if (_dynamicValues.TryGetValue(key, out var dynamicValue))
|
|
|
|
|
{
|
|
|
|
|
return string.Format(value, dynamicValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-03 19:45:08 +01:00
|
|
|
|
return key.ToString();
|
2022-05-15 13:30:15 +02:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_localeStrings[key] = value;
|
|
|
|
|
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-03 19:45:08 +01:00
|
|
|
|
public void UpdateDynamicValue(LocaleKeys key, params object[] values)
|
2022-05-15 13:30:15 +02:00
|
|
|
|
{
|
|
|
|
|
_dynamicValues[key] = values;
|
|
|
|
|
|
|
|
|
|
OnPropertyChanged("Item");
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 20:06:31 +02:00
|
|
|
|
public void LoadDefaultLanguage()
|
|
|
|
|
{
|
|
|
|
|
LoadLanguage(DefaultLanguageCode);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-15 13:30:15 +02:00
|
|
|
|
public void LoadLanguage(string languageCode)
|
|
|
|
|
{
|
|
|
|
|
string languageJson = EmbeddedResources.ReadAllText($"Ryujinx.Ava/Assets/Locales/{languageCode}.json");
|
|
|
|
|
|
|
|
|
|
if (languageJson == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-23 18:55:26 +01:00
|
|
|
|
var strings = JsonHelper.Deserialize<Dictionary<string, string>>(languageJson);
|
2022-05-15 13:30:15 +02:00
|
|
|
|
|
|
|
|
|
foreach (var item in strings)
|
|
|
|
|
{
|
2023-01-03 19:45:08 +01:00
|
|
|
|
if (Enum.TryParse<LocaleKeys>(item.Key, out var key))
|
|
|
|
|
{
|
|
|
|
|
this[key] = item.Value;
|
|
|
|
|
}
|
2022-05-15 13:30:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Program.PreviewerDetached)
|
|
|
|
|
{
|
|
|
|
|
ConfigurationState.Instance.Ui.LanguageCode.Value = languageCode;
|
|
|
|
|
ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|