Fractured Locales Support (ryubing/ryujinx!238)

See merge request ryubing/ryujinx!238
This commit is contained in:
LotP
2025-12-27 14:07:56 -06:00
parent 9ebf444644
commit 45193dcc8d
9 changed files with 272 additions and 175 deletions

View File

@@ -8,6 +8,8 @@ using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.Json.Serialization;
namespace Ryujinx.Ava.Common.Locale
@@ -158,52 +160,86 @@ namespace Ryujinx.Ava.Common.Locale
LocaleChanged?.Invoke();
}
private static LocalesJson? _localeData;
private static LocalesData? _localeData;
private static Dictionary<LocaleKeys, string> LoadJsonLanguage(string languageCode)
{
Dictionary<LocaleKeys, string> localeStrings = new();
_localeData ??= EmbeddedResources.ReadAllText("Ryujinx/Assets/Locale.json")
.Into(it => JsonHelper.Deserialize(it, LocalesJsonContext.Default.LocalesJson));
foreach (LocalesEntry locale in _localeData.Value.Locales)
if (_localeData is null)
{
if (locale.Translations.Count < _localeData.Value.Languages.Count)
Dictionary<string, LocalesJson> locales = [];
foreach (string uri in EmbeddedResources.GetAllAvailableResources("Ryujinx/Assets/Locales", ".json"))
{
throw new Exception(
$"Locale key {{{locale.ID}}} is missing languages! Has {locale.Translations.Count} translations, expected {_localeData.Value.Languages.Count}!");
string path = uri[..^".json".Length];
path = path.Replace('.', '/');
path = path.Append(".json");
locales.TryAdd(Path.GetFileName(path), EmbeddedResources.ReadAllText(path)
.Into(it => JsonHelper.Deserialize(it, LocalesJsonContext.Default.LocalesJson)));
}
if (locale.Translations.Count > _localeData.Value.Languages.Count)
_localeData = new LocalesData
{
throw new Exception(
$"Locale key {{{locale.ID}}} has too many languages! Has {locale.Translations.Count} translations, expected {_localeData.Value.Languages.Count}!");
}
Languages = EmbeddedResources.ReadAllText("Ryujinx/Assets/Languages.json")
.Into(it => JsonHelper.Deserialize(it, LanguagesJsonContext.Default.LanguagesJson)).Languages.Keys.ToList(),
LocalesFiles = locales
};
if (!Enum.TryParse<LocaleKeys>(locale.ID, out LocaleKeys localeKey))
continue;
}
string str = locale.Translations.TryGetValue(languageCode, out string val) && !string.IsNullOrEmpty(val)
? val
: locale.Translations[DefaultLanguageCode];
if (string.IsNullOrEmpty(str))
foreach (LocalesJson file in _localeData.Value.LocalesFiles.Values)
{
foreach (LocalesEntry locale in file.Locales)
{
throw new Exception(
$"Locale key '{locale.ID}' has no valid translations for desired language {languageCode}! {DefaultLanguageCode} is an empty string or null");
}
if (locale.Translations.Count < _localeData.Value.Languages.Count)
{
throw new Exception(
$"Locale key {{{locale.ID}}} is missing languages! Has {locale.Translations.Count} translations, expected {_localeData.Value.Languages.Count}!");
}
localeStrings[localeKey] = str;
if (locale.Translations.Count > _localeData.Value.Languages.Count)
{
throw new Exception(
$"Locale key {{{locale.ID}}} has too many languages! Has {locale.Translations.Count} translations, expected {_localeData.Value.Languages.Count}!");
}
if (!Enum.TryParse<LocaleKeys>(locale.ID, out LocaleKeys localeKey))
continue;
string str = locale.Translations.TryGetValue(languageCode, out string val) && !string.IsNullOrEmpty(val)
? val
: locale.Translations[DefaultLanguageCode];
if (string.IsNullOrEmpty(str))
{
throw new Exception(
$"Locale key '{locale.ID}' has no valid translations for desired language {languageCode}! {DefaultLanguageCode} is an empty string or null");
}
localeStrings[localeKey] = str;
}
}
return localeStrings;
}
}
public struct LocalesJson
public struct LocalesData
{
public List<string> Languages { get; set; }
public Dictionary<string, LocalesJson> LocalesFiles { get; set; }
}
public struct LanguagesJson
{
public Dictionary<string, string> Languages { get; set; }
}
public struct LocalesJson
{
public List<LocalesEntry> Locales { get; set; }
}
@@ -216,4 +252,8 @@ namespace Ryujinx.Ava.Common.Locale
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(LocalesJson))]
internal partial class LocalesJsonContext : JsonSerializerContext;
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(LanguagesJson))]
internal partial class LanguagesJsonContext : JsonSerializerContext;
}

View File

@@ -134,7 +134,7 @@
</ItemGroup>
<ItemGroup>
<None Remove="Assets\locales.json" />
<None Remove="Assets\**\*.json" />
<None Remove="Assets\Styles\Styles.xaml" />
<None Remove="Assets\Styles\Themes.xaml" />
<None Remove="Assets\Icons\Controller_JoyConLeft.svg" />
@@ -156,8 +156,8 @@
<EmbeddedResource Include="..\..\docs\compatibility.csv" LogicalName="RyujinxGameCompatibilityList">
<Link>Assets\RyujinxGameCompatibility.csv</Link>
</EmbeddedResource>
<EmbeddedResource Include="..\..\assets\locales.json">
<Link>Assets\Locale.json</Link>
<EmbeddedResource Include="..\..\assets\**\*.json">
<LinkBase>Assets</LinkBase>
</EmbeddedResource>
<EmbeddedResource Include="Assets\Styles\Styles.xaml" />
<EmbeddedResource Include="Assets\Icons\Controller_JoyConLeft.svg" />
@@ -178,6 +178,6 @@
<EmbeddedResource Include="Assets\UIImages\Logo_Ryujinx_AntiAlias.png" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="..\..\assets\locales.json" />
<AdditionalFiles Include="..\..\assets\Locales\*.json" />
</ItemGroup>
</Project>

View File

@@ -85,29 +85,16 @@ namespace Ryujinx.Ava.UI.Views.Main
private static IEnumerable<MenuItem> GenerateLanguageMenuItems()
{
const string LocalePath = "Ryujinx/Assets/Locale.json";
const string LanguagesPath = "Ryujinx/Assets/Languages.json";
string languageJson = EmbeddedResources.ReadAllText(LocalePath);
string languageJson = EmbeddedResources.ReadAllText(LanguagesPath);
string currentLanguageCode = LocaleManager.Instance.CurrentLanguageCode;
LocalesJson locales = JsonHelper.Deserialize(languageJson, LocalesJsonContext.Default.LocalesJson);
LanguagesJson languages = JsonHelper.Deserialize(languageJson, LanguagesJsonContext.Default.LanguagesJson);
foreach (string language in locales.Languages)
foreach ((string code, string language) in languages.Languages)
{
int index = locales.Locales.FindIndex(x => x.ID == "Language");
string languageName;
if (index == -1)
{
languageName = language;
}
else
{
string tr = locales.Locales[index].Translations[language];
languageName = string.IsNullOrEmpty(tr)
? language
: tr;
}
string languageName = string.IsNullOrEmpty(language) ? code : language;
MenuItem menuItem = new()
{