RacesOfValheim/JotunnModStub/ModText.cs
kanym b0d18b0654 feat(text): on-screen messages come from translation files
Every message the mod puts in the middle or corner of the screen now reads
its text through ModText.Get, and messages.json holds 191 texts in English
and Italian. Values computed in code become $1, $2 in the text, so a
translator can put them wherever the sentence needs them. Branching
messages get one key per branch.

A few messages travel through a field before they are shown: thralls'
refusals and the building limits now hold a token or key there. Boss and
form names inside messages use tokens too, the bosses the game's own, so
"The Elder" arrives translated.

ModText.Get now localizes each argument before inserting it. A token
argument left for a later pass ran into the text after it: "$1's power"
became the unknown token "enemy_eikthyr's".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 20:30:03 +02:00

56 lines
2.5 KiB
C#

using System.Linq;
namespace RacesOfValheim
{
/// <summary>
/// Every piece of text the mod shows, read from the translation files instead of the code.
///
/// The files are JSON under Translations/&lt;Language&gt;/, one per kind of text, next to the DLL.
/// Jotunn finds them on its own: it searches the whole plugins folder for any .json whose
/// grandparent folder is called "Translations", takes the language from the folder name
/// (Valheim's own names: English, Italian, German...), and falls back to English for any key a
/// language leaves out. A key missing from English too shows up on screen as [key].
///
/// Keys go in the files without the leading $ and are read here the same way. Values take
/// Valheim's own placeholders, $1, $2..., filled from the arguments in order, so a translator
/// can move a number to wherever their grammar wants it.
/// </summary>
internal static class ModText
{
public static string Get(string key, params object[] args)
{
if (Localization.instance == null) return "[" + key + "]";
// Each argument is localized on its own before it goes in. An argument is often a token
// itself - a creature's m_name, a boss - and left for a later pass it would run into
// whatever follows the $1: "$enemy_eikthyr's power" reads as the token
// "enemy_eikthyr's", because an apostrophe does not end a token.
string[] words = args
.Select(a => Localization.instance.Localize(a?.ToString() ?? ""))
.ToArray();
return Localization.instance.Localize("$" + key, words);
}
/// <summary>
/// A race's name as the player reads it. Only ever for display.
///
/// The race ID ("Skadi", "Trol"...) is a different thing and never goes through here: it is
/// written into the character save and the ZDO, and compared all over the code. Translate
/// that and a character saved under one language stops being recognised under another.
/// </summary>
public static string RaceName(string raceId)
{
return Get(RaceKey(raceId) + "_name");
}
public static string RaceDescription(string raceId, params object[] args)
{
return Get(RaceKey(raceId) + "_desc", args);
}
private static string RaceKey(string raceId)
{
return "rov_race_" + raceId.ToLowerInvariant();
}
}
}