From 38ee19e99673c7b604dddadf50245a6cea0fa36a Mon Sep 17 00:00:00 2001 From: Az <56699741+AlchlcDvl@users.noreply.github.com> Date: Wed, 18 Nov 2026 14:33:14 -0500 Subject: [PATCH] allow referring to vanilla strings --- MiraAPI/Translation/MiraLocaleManager.cs | 67 ++++++++++++++++++++---- 1 file changed, 56 insertions(+), 11 deletions(-) diff --git a/MiraAPI/Translation/MiraLocaleManager.cs b/MiraAPI/Translation/MiraLocaleManager.cs index f3f276fe..6a1c8a39 100644 --- a/MiraAPI/Translation/MiraLocaleManager.cs +++ b/MiraAPI/Translation/MiraLocaleManager.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Xml; using AmongUs.Data; +using BepInEx; using MiraAPI.Utilities; using Reactor.Localization.Utilities; using UnityEngine; @@ -16,10 +18,14 @@ namespace MiraAPI.Translation; /// public static class MiraLocaleManager { + private const string StringNamesPrefix = "StringNames"; private const string LangDirectory = "mira_languages"; + private const string VanillaPrefix = "@vanilla:"; private const string RefPrefix = "@ref:"; private const int MaxDepth = 10; + private static readonly Dictionary VanillaKeyMap = []; + // Language, XML Name, then Value /// @@ -147,6 +153,9 @@ public static class MiraLocaleManager /// A based on the key provided. public static string Get(MiraLanguage language, string key, string fallback = "") { + if (VanillaKeyMap.TryGetValue(key, out var stringName) && TranslationController.InstanceExists) + return TranslationController.Instance.GetString(stringName); + var text = Locale.GetValueOrDefault(language)?.GetValueOrDefault(key); if (language != MiraLanguage.English) @@ -173,14 +182,7 @@ public static class MiraLocaleManager return GetParsed(CurrentLanguage, key, parseList, string.IsNullOrEmpty(fallback) ? key : fallback); } - /// - /// Translates a key into the current language. - /// - /// The specific language to prioritize. - /// The string id to find. - /// List of keys to change into other text. - /// Fallback string to use if no translation is found. - /// A based on the key provided. + /// public static string GetParsed( MiraLanguage language, string key, @@ -221,6 +223,32 @@ public static class MiraLocaleManager return text; } + /// + public static string GetParsed( + MiraLanguage language, + string key, + string fallback, + params object[] parseList) + { + var text = Get(language, key, fallback); + + if (parseList == null || parseList.Length == 0) + { + return text; + } + + return string.Format(CultureInfo.InvariantCulture, text, parseList); + } + + /// + public static string GetParsed( + MiraLanguage language, + string key, + params object[] parseList) + { + return GetParsed(language, key, string.Empty, parseList); + } + /// /// Builds a formatted translation ID string using a mod ID, a specific ID part, and a suffix. /// @@ -267,9 +295,9 @@ public static class MiraLocaleManager /// public static void LoadExternalLocale() { - CheckExternalDirectory("mira.api", BepInEx.Paths.PluginPath); - CheckExternalDirectory("mira.api", BepInEx.Paths.BepInExRootPath); - CheckExternalDirectory("mira.api", BepInEx.Paths.GameRootPath); + CheckExternalDirectory("mira.api", Paths.PluginPath); + CheckExternalDirectory("mira.api", Paths.BepInExRootPath); + CheckExternalDirectory("mira.api", Paths.GameRootPath); foreach (var mod in RegisteredModIds) { CheckExternalDirectory(mod, GetModLangDir(mod)); @@ -400,6 +428,23 @@ public static class MiraLocaleManager { pendingRefs[name] = value[RefPrefix.Length..]; } + else if (value.StartsWith(VanillaPrefix, StringComparison.Ordinal)) + { + var enumString = value[VanillaPrefix.Length..]; + + if (enumString.StartsWith(StringNamesPrefix, StringComparison.Ordinal)) + enumString = enumString[StringNamesPrefix.Length..]; + + if (Enum.TryParse(enumString, true, out var stringName)) + { + VanillaKeyMap[name] = stringName; + total++; + } + else + { + Error($"Failed to parse vanilla StringNames enum: {enumString}"); + } + } else { value = value.Replace('[', '<')