diff --git a/src/Ryujinx/Common/LocaleManager.cs b/src/Ryujinx/Common/LocaleManager.cs index c57c447ff..54a11c109 100644 --- a/src/Ryujinx/Common/LocaleManager.cs +++ b/src/Ryujinx/Common/LocaleManager.cs @@ -6,7 +6,11 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Globalization; +using System.IO; +using System.Linq; +using System.Text.Encodings.Web; using System.Text.Json; +using System.Text.Unicode; namespace Ryujinx.Ava.Common.Locale { @@ -146,6 +150,8 @@ namespace Ryujinx.Ava.Common.Locale var localeStrings = new Dictionary(); 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) { // 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) { Logger.Error?.Print(LogClass.UI, $"Locale key {{{locale.ID}}} is missing languages!"); + invalidLocalesJson = true; +#if DEBUG + break; +#else throw new Exception("Missing locale data!"); +#endif } if (Enum.TryParse(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; } }