What the stations, thralls, the ward and the hive show under the crosshair now reads its text through ModText.Get, and hover.json holds 50 texts in English and Italian. The Aswid thralls and the Race Menu Ward wrote the use key as a literal E; they now use $KEY_Use, which shows the key the player actually bound. For that to work, ModText.Get runs a second localization pass when a $ is left in the result: Valheim resolves tokens once and never looks inside what it inserted, and hover texts go to the HUD without another pass. The Sverm hive stage names stay English for now: the compendium and the hive's goods show them too, and those come with the merchants step. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
63 lines
3 KiB
C#
63 lines
3 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/<Language>/, 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();
|
|
string text = Localization.instance.Localize("$" + key, words);
|
|
|
|
// Valheim resolves tokens in one pass and never looks inside what it put in, so a
|
|
// "$KEY_Use" written in a translation came out as it was. Places that localize the
|
|
// result again (MessageHud, the stations' own Localize) hid that; hover texts returned
|
|
// straight to the HUD did not. A second pass costs nothing when there is no $ left.
|
|
if (text.IndexOf('$') >= 0) text = Localization.instance.Localize(text);
|
|
return text;
|
|
}
|
|
|
|
/// <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();
|
|
}
|
|
}
|
|
}
|