From d0da51c66916364c3e6ddcc3d56f75824bdfe7c7 Mon Sep 17 00:00:00 2001 From: XaineDev <125938059+XaineDev@users.noreply.github.com> Date: Thu, 1 Oct 2026 16:42:42 +0100 Subject: [PATCH] fix(translation): show a role's own text when its locale key has no translation --- MiraAPI/Roles/CustomRoleManager.cs | 8 ++++---- MiraAPI/Translation/MiraLocaleManager.cs | 16 ++++++++++++++++ MiraAPI/Translation/MiraLocalizationProvider.cs | 4 +++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/MiraAPI/Roles/CustomRoleManager.cs b/MiraAPI/Roles/CustomRoleManager.cs index 060b1a5..1dd6a76 100644 --- a/MiraAPI/Roles/CustomRoleManager.cs +++ b/MiraAPI/Roles/CustomRoleManager.cs @@ -131,10 +131,10 @@ internal static void RegisterRoleTypes(List roles, MiraPluginInfo pluginIn roleBehaviour.Role = (RoleTypes)roleId; roleBehaviour.TeamType = customRole.Team == ModdedRoleTeams.Custom ? RoleTeamTypes.Crewmate : (RoleTeamTypes)customRole.Team; roleBehaviour.NameColor = customRole.RoleColor; - roleBehaviour.StringName = MiraLocaleManager.GetOrCreateLocaleString(customRole.RoleNameLocale); - roleBehaviour.BlurbName = MiraLocaleManager.GetOrCreateLocaleString(customRole.RoleDescriptionLocale); - roleBehaviour.BlurbNameMed = MiraLocaleManager.GetOrCreateLocaleString(customRole.RoleMedDescriptionLocale); - roleBehaviour.BlurbNameLong = MiraLocaleManager.GetOrCreateLocaleString(customRole.RoleLongDescriptionLocale); + roleBehaviour.StringName = MiraLocaleManager.GetOrCreateLocaleString(customRole.RoleNameLocale, () => customRole.RoleName); + roleBehaviour.BlurbName = MiraLocaleManager.GetOrCreateLocaleString(customRole.RoleDescriptionLocale, () => customRole.RoleDescription); + roleBehaviour.BlurbNameMed = MiraLocaleManager.GetOrCreateLocaleString(customRole.RoleMedDescriptionLocale, () => customRole.RoleMedDescription); + roleBehaviour.BlurbNameLong = MiraLocaleManager.GetOrCreateLocaleString(customRole.RoleLongDescriptionLocale, () => customRole.RoleLongDescription); roleBehaviour.AffectedByLightAffectors = customRole.Configuration.AffectedByLightOnAirship; roleBehaviour.CanBeKilled = customRole.Configuration.CanGetKilled; roleBehaviour.CanUseKillButton = customRole.Configuration.UseVanillaKillButton; diff --git a/MiraAPI/Translation/MiraLocaleManager.cs b/MiraAPI/Translation/MiraLocaleManager.cs index d26f422..c323f4b 100644 --- a/MiraAPI/Translation/MiraLocaleManager.cs +++ b/MiraAPI/Translation/MiraLocaleManager.cs @@ -35,6 +35,7 @@ public static class MiraLocaleManager /// public static readonly Dictionary RegisteredStringNames = []; internal static readonly Dictionary StringNamesLookup = []; + internal static readonly Dictionary> StringNamesFallbacks = []; /// /// Gets a dictionary mapping values to their standard language codes. @@ -278,6 +279,21 @@ public static StringNames GetOrCreateLocaleString(string name) return newString; } + /// + /// Retrieves the value for a locale key, shown as 's text while the + /// key has no translation. Mods built before keys existed set their text directly, so without a fallback the key itself + /// would be shown. + /// + /// The name of the locale string. + /// Supplies the text to show when has no translation. + /// The corresponding value. + public static StringNames GetOrCreateLocaleString(string name, Func fallback) + { + var stringName = GetOrCreateLocaleString(name); + StringNamesFallbacks[stringName] = fallback; + return stringName; + } + /// /// Loads external locale XML files by checking predefined directories, including the plugin path, BepInEx root, game root, and registered mod directories. /// diff --git a/MiraAPI/Translation/MiraLocalizationProvider.cs b/MiraAPI/Translation/MiraLocalizationProvider.cs index 4e757a0..d12b550 100644 --- a/MiraAPI/Translation/MiraLocalizationProvider.cs +++ b/MiraAPI/Translation/MiraLocalizationProvider.cs @@ -22,7 +22,9 @@ public override bool TryGetText(StringNames stringName, out string? result) { if (MiraLocaleManager.StringNamesLookup.TryGetValue(stringName, out var key)) { - result = MiraLocaleManager.Get(key); + result = MiraLocaleManager.StringNamesFallbacks.TryGetValue(stringName, out var fallback) + ? MiraLocaleManager.Get(key, fallback()) + : MiraLocaleManager.Get(key); return true; }