Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions MiraAPI/Roles/CustomRoleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ internal static void RegisterRoleTypes(List<Type> 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;
Expand Down
16 changes: 16 additions & 0 deletions MiraAPI/Translation/MiraLocaleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static class MiraLocaleManager
/// </summary>
public static readonly Dictionary<string, StringNames> RegisteredStringNames = [];
internal static readonly Dictionary<StringNames, string> StringNamesLookup = [];
internal static readonly Dictionary<StringNames, Func<string>> StringNamesFallbacks = [];

/// <summary>
/// Gets a dictionary mapping <see cref="MiraLanguage"/> values to their standard language codes.
Expand Down Expand Up @@ -278,6 +279,21 @@ public static StringNames GetOrCreateLocaleString(string name)
return newString;
}

/// <summary>
/// Retrieves the <see cref="StringNames"/> value for a locale key, shown as <paramref name="fallback"/>'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.
/// </summary>
/// <param name="name">The name of the locale string.</param>
/// <param name="fallback">Supplies the text to show when <paramref name="name"/> has no translation.</param>
/// <returns>The corresponding <see cref="StringNames"/> value.</returns>
public static StringNames GetOrCreateLocaleString(string name, Func<string> fallback)
{
var stringName = GetOrCreateLocaleString(name);
StringNamesFallbacks[stringName] = fallback;
return stringName;
}

/// <summary>
/// Loads external locale XML files by checking predefined directories, including the plugin path, BepInEx root, game root, and registered mod directories.
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion MiraAPI/Translation/MiraLocalizationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Loading