Add automatic locales re-/construction

This commit is contained in:
LotP1 2024-12-16 19:42:19 +01:00
parent 1d21260b0a
commit 611a11ab56

View file

@ -6,7 +6,11 @@ using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Json; using System.Text.Json;
using System.Text.Unicode;
namespace Ryujinx.Ava.Common.Locale namespace Ryujinx.Ava.Common.Locale
{ {
@ -146,6 +150,8 @@ namespace Ryujinx.Ava.Common.Locale
var localeStrings = new Dictionary<LocaleKeys, string>(); var localeStrings = new Dictionary<LocaleKeys, string>();
string fileData = EmbeddedResources.ReadAllText($"Ryujinx/Assets/locales.json"); string fileData = EmbeddedResources.ReadAllText($"Ryujinx/Assets/locales.json");
bool invalidLocalesJson = false; //does the locales.json contain all the languages in all the locales?
if (fileData == null) if (fileData == null)
{ {
// We were unable to find file for that language code. // We were unable to find file for that language code.
@ -159,7 +165,12 @@ namespace Ryujinx.Ava.Common.Locale
if (locale.Translations.Count != json.Languages.Count) if (locale.Translations.Count != json.Languages.Count)
{ {
Logger.Error?.Print(LogClass.UI, $"Locale key {{{locale.ID}}} is missing languages!"); Logger.Error?.Print(LogClass.UI, $"Locale key {{{locale.ID}}} is missing languages!");
invalidLocalesJson = true;
#if DEBUG
break;
#else
throw new Exception("Missing locale data!"); throw new Exception("Missing locale data!");
#endif
} }
if (Enum.TryParse<LocaleKeys>(locale.ID, out var localeKey)) if (Enum.TryParse<LocaleKeys>(locale.ID, out var localeKey))
@ -176,6 +187,42 @@ namespace Ryujinx.Ava.Common.Locale
} }
} }
if (invalidLocalesJson)
{
for (int i = 0; i < json.Locales.Count; i++)
{
LocalesEntry locale = json.Locales[i];
foreach (string language in json.Languages)
{
if (!locale.Translations.ContainsKey(language))
{
locale.Translations.Add(language, "");
Logger.Warning?.Print(LogClass.UI, $"Added {{{language}}} to Locale {{{locale.ID}}}");
}
}
locale.Translations = locale.Translations.OrderBy(pair => pair.Key).ToDictionary(pair => pair.Key, pair => pair.Value);
json.Locales[i] = locale;
}
string location = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.GetDirectories("Assets")[0].GetFiles("locales.json")[0].FullName;
JsonSerializerOptions options = new JsonSerializerOptions
{
WriteIndented = true,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};
string jsonString = JsonSerializer.Serialize(json, options);//GetSubEventString(rootEvent, 0);
using (StreamWriter sw = new StreamWriter(location))
{
sw.Write(jsonString);
}
throw new Exception("Missing locale data! (missing locale data has been added, please rebuild the project)");
}
return localeStrings; return localeStrings;
} }
} }