From 73b67cd6ce547057e070a666d6f261018584ee62 Mon Sep 17 00:00:00 2001 From: kanym Date: Sat, 26 Sep 2026 19:43:48 +0200 Subject: [PATCH] feat(text): the race menu reads its texts from translation files First step of moving every piece of text out of the code so modders can translate the mod. The texts live in Translations//*.json, one file per kind of text, which Jotunn finds on its own and falls back to English for. ModText reads them; values take Valheim's $1, $2 placeholders. This step covers the race selection menu: title, buttons, every race's name and description, in English and Italian. The race ID and the name on screen are now separate strings. The ID is what the save and the ZDO hold, so it must never be translated. The files are deployed to plugins\RacesOfValheim\Translations and packed into both release zips. Co-Authored-By: Claude Opus 5.5 --- JotunnModStub/ModText.cs | 50 +++++++++++++++++++ JotunnModStub/RacesOfValheim.cs | 48 ++++++++++-------- JotunnModStub/RacesOfValheim.csproj | 17 ++++++- JotunnModStub/Translations/English/races.json | 38 ++++++++++++++ JotunnModStub/Translations/Italian/races.json | 38 ++++++++++++++ 5 files changed, 169 insertions(+), 22 deletions(-) create mode 100644 JotunnModStub/ModText.cs create mode 100644 JotunnModStub/Translations/English/races.json create mode 100644 JotunnModStub/Translations/Italian/races.json diff --git a/JotunnModStub/ModText.cs b/JotunnModStub/ModText.cs new file mode 100644 index 0000000..4e43258 --- /dev/null +++ b/JotunnModStub/ModText.cs @@ -0,0 +1,50 @@ +using System.Linq; + +namespace RacesOfValheim +{ + /// + /// 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. + /// + internal static class ModText + { + public static string Get(string key, params object[] args) + { + if (Localization.instance == null) return "[" + key + "]"; + + string[] words = args.Select(a => a?.ToString() ?? "").ToArray(); + return Localization.instance.Localize("$" + key, words); + } + + /// + /// 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. + /// + 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(); + } + } +} diff --git a/JotunnModStub/RacesOfValheim.cs b/JotunnModStub/RacesOfValheim.cs index 9b7a5c3..7778632 100644 --- a/JotunnModStub/RacesOfValheim.cs +++ b/JotunnModStub/RacesOfValheim.cs @@ -303,7 +303,7 @@ namespace RacesOfValheim titleObj.transform.SetParent(selectionPanel.transform, false); Text titleText = titleObj.AddComponent(); titleText.font = GUIManager.Instance.AveriaSerifBold; - titleText.text = "Choose your Race"; + titleText.text = ModText.Get("rov_menu_choose_race"); titleText.fontSize = 50; titleText.alignment = TextAnchor.MiddleCenter; titleText.color = Color.white; @@ -346,7 +346,7 @@ namespace RacesOfValheim btnOkTxtObj.transform.SetParent(btnOkObj.transform, false); Text btnOkTxt = btnOkTxtObj.AddComponent(); btnOkTxt.font = GUIManager.Instance.AveriaSerifBold; - btnOkTxt.text = "OK"; + btnOkTxt.text = ModText.Get("rov_menu_ok"); btnOkTxt.fontSize = 24; btnOkTxt.alignment = TextAnchor.MiddleCenter; btnOkTxt.color = Color.white; @@ -371,7 +371,7 @@ namespace RacesOfValheim btnBackTxtObj.transform.SetParent(btnBackObj.transform, false); Text btnBackTxt = btnBackTxtObj.AddComponent(); btnBackTxt.font = GUIManager.Instance.AveriaSerifBold; - btnBackTxt.text = "Back"; + btnBackTxt.text = ModText.Get("rov_menu_back"); btnBackTxt.fontSize = 24; btnBackTxt.alignment = TextAnchor.MiddleCenter; btnBackTxt.color = Color.white; @@ -379,34 +379,40 @@ namespace RacesOfValheim btnBackTxtRect.anchorMin = Vector2.zero; btnBackTxtRect.anchorMax = Vector2.one; btnBackTxtRect.offsetMin = Vector2.zero; btnBackTxtRect.offsetMax = Vector2.zero; - void CreateRaceButtonWithConfirm(string label, Vector2 position, Sprite iconSprite, string description, UnityEngine.Events.UnityAction onConfirm) + // The race ID picks the texts and is what gets saved; the button, the title and the + // description only ever show the translated name. See ModText.RaceName for why the two + // must never be the same string. + void CreateRaceButtonWithConfirm(string raceId, Vector2 position, Sprite iconSprite, UnityEngine.Events.UnityAction onConfirm, params object[] descArgs) { - CreateRaceButton(label, position, iconSprite, selectionPanel.transform, () => { + string name = ModText.RaceName(raceId); + CreateRaceButton(raceId, name, position, iconSprite, selectionPanel.transform, () => { selectionPanel.SetActive(false); confirmationPanel.SetActive(true); - confTitleText.text = label; - confDescText.text = description; + confTitleText.text = name; + confDescText.text = ModText.RaceDescription(raceId, descArgs); btnOk.onClick.RemoveAllListeners(); btnOk.onClick.AddListener(onConfirm); }); } // Row 1 (y = 100) - CreateRaceButtonWithConfirm("Human", new Vector2(-450, 100), GetHumanIcon(), "The versatile founders of Midgard. Balanced stats with no specific weaknesses.", SelectHuman); - CreateRaceButtonWithConfirm("Fenrir", new Vector2(-150, 100), GetFenrirIcon(), "Cursed by the blood of the wolf. Fast and deadly, but vulnerable to silver.", SelectFenrir); - CreateRaceButtonWithConfirm("Skadi", new Vector2(150, 100), GetSkadiIcon(), "Children of the mountains. Resistant to frost, at home in the freezing cold.", SelectSkadi); - CreateRaceButtonWithConfirm("Skeleton", new Vector2(450, 100), GetSkeletonIcon(), "Undead warriors. Immune to poison, but brittle against blunt trauma.", SelectSkeleton); - + CreateRaceButtonWithConfirm("Human", new Vector2(-450, 100), GetHumanIcon(), SelectHuman); + CreateRaceButtonWithConfirm("Fenrir", new Vector2(-150, 100), GetFenrirIcon(), SelectFenrir); + CreateRaceButtonWithConfirm("Skadi", new Vector2(150, 100), GetSkadiIcon(), SelectSkadi); + CreateRaceButtonWithConfirm(SkeletonVisuals.RaceId, new Vector2(450, 100), GetSkeletonIcon(), SelectSkeleton); + // Row 2 (y = -20) - CreateRaceButtonWithConfirm("Aswid", new Vector2(-450, -20), GetAswidIcon(), "Fierce night stalker. Strong and resilient, transforms into a bat.", SelectAswid); - CreateRaceButtonWithConfirm("Furling", new Vector2(-150, -20), GetFurlingIcon(), "Cunning goblin-like creatures. Agile and resourceful.", SelectFurling); - CreateRaceButtonWithConfirm("Trol", new Vector2(150, -20), GetTrolIcon(), $"Massive brute strength. Towering over others, at peace with the Black Forest - but fire burns a Trol {(TrolConfig.FireWeaknessMultiplier.Value - 1f) * 100f:0}% harder than anyone else.", SelectTrol); - CreateRaceButtonWithConfirm("Draugh", new Vector2(450, -20), GetDraughIcon(), "A decaying undead warrior fueled by dark magic and extreme metal.", SelectDraugh); + CreateRaceButtonWithConfirm("Aswid", new Vector2(-450, -20), GetAswidIcon(), SelectAswid); + CreateRaceButtonWithConfirm("Furling", new Vector2(-150, -20), GetFurlingIcon(), SelectFurling); + // $1 in the Trol's description is how much harder fire burns it, read from the config. + CreateRaceButtonWithConfirm("Trol", new Vector2(150, -20), GetTrolIcon(), SelectTrol, + ((TrolConfig.FireWeaknessMultiplier.Value - 1f) * 100f).ToString("0")); + CreateRaceButtonWithConfirm(DraughVisuals.RaceId, new Vector2(450, -20), GetDraughIcon(), SelectDraugh); // Row 3 (y = -140) - CreateRaceButtonWithConfirm("Dvergar", new Vector2(-450, -140), GetDvergarIcon(), "Small, stubborn smiths of the deep. Frail of frame, but masters of metal, mine and market.", SelectDvergar); - CreateRaceButtonWithConfirm("Trenta", new Vector2(-150, -140), GetTrentaIcon(), "Tree-born folk, kin to root and leaf. Patient and hard to fell.", SelectTrenta); - CreateRaceButtonWithConfirm("Sverm", new Vector2(150, -140), GetSvermIcon(), "Chitin-born swarm kin. Armoured from birth and never slowed, venomous, at peace with every crawling thing - but blunt trauma and fire find the seams.", SelectSverm); + CreateRaceButtonWithConfirm(DvergarConfig.RaceId, new Vector2(-450, -140), GetDvergarIcon(), SelectDvergar); + CreateRaceButtonWithConfirm(TrentaConfig.RaceId, new Vector2(-150, -140), GetTrentaIcon(), SelectTrenta); + CreateRaceButtonWithConfirm(SvermConfig.RaceId, new Vector2(150, -140), GetSvermIcon(), SelectSverm); GUIManager.BlockInput(true); } @@ -481,9 +487,9 @@ namespace RacesOfValheim return null; } - private void CreateRaceButton(string label, Vector2 position, Sprite iconSprite, Transform parentTransform, UnityEngine.Events.UnityAction onClick) + private void CreateRaceButton(string raceId, string label, Vector2 position, Sprite iconSprite, Transform parentTransform, UnityEngine.Events.UnityAction onClick) { - GameObject btnObj = new GameObject(label + "Button"); + GameObject btnObj = new GameObject(raceId + "Button"); btnObj.transform.SetParent(parentTransform, false); RectTransform btnRect = btnObj.AddComponent(); btnRect.sizeDelta = new Vector2(250, 80); diff --git a/JotunnModStub/RacesOfValheim.csproj b/JotunnModStub/RacesOfValheim.csproj index 58ed010..188493c 100644 --- a/JotunnModStub/RacesOfValheim.csproj +++ b/JotunnModStub/RacesOfValheim.csproj @@ -138,18 +138,33 @@ + + + + + + $(REPO_ROOT)dist\ + + + $(IntermediateOutputPath)nexus\ + + + - + diff --git a/JotunnModStub/Translations/English/races.json b/JotunnModStub/Translations/English/races.json new file mode 100644 index 0000000..8d930c3 --- /dev/null +++ b/JotunnModStub/Translations/English/races.json @@ -0,0 +1,38 @@ +{ + "rov_menu_choose_race": "Choose your Race", + "rov_menu_ok": "OK", + "rov_menu_back": "Back", + + "rov_race_human_name": "Human", + "rov_race_human_desc": "The versatile founders of Midgard. Balanced stats with no specific weaknesses.", + + "rov_race_fenrir_name": "Fenrir", + "rov_race_fenrir_desc": "Cursed by the blood of the wolf. Fast and deadly, but vulnerable to silver.", + + "rov_race_skadi_name": "Skadi", + "rov_race_skadi_desc": "Children of the mountains. Resistant to frost, at home in the freezing cold.", + + "rov_race_skeleton_name": "Skeleton", + "rov_race_skeleton_desc": "Undead warriors. Immune to poison, but brittle against blunt trauma.", + + "rov_race_aswid_name": "Aswid", + "rov_race_aswid_desc": "Fierce night stalker. Strong and resilient, transforms into a bat.", + + "rov_race_furling_name": "Furling", + "rov_race_furling_desc": "Cunning goblin-like creatures. Agile and resourceful.", + + "rov_race_trol_name": "Trol", + "rov_race_trol_desc": "Massive brute strength. Towering over others, at peace with the Black Forest - but fire burns a Trol $1% harder than anyone else.", + + "rov_race_draugh_name": "Draugh", + "rov_race_draugh_desc": "A decaying undead warrior fueled by dark magic and extreme metal.", + + "rov_race_dvergar_name": "Dvergar", + "rov_race_dvergar_desc": "Small, stubborn smiths of the deep. Frail of frame, but masters of metal, mine and market.", + + "rov_race_trenta_name": "Trenta", + "rov_race_trenta_desc": "Tree-born folk, kin to root and leaf. Patient and hard to fell.", + + "rov_race_sverm_name": "Sverm", + "rov_race_sverm_desc": "Chitin-born swarm kin. Armoured from birth and never slowed, venomous, at peace with every crawling thing - but blunt trauma and fire find the seams." +} diff --git a/JotunnModStub/Translations/Italian/races.json b/JotunnModStub/Translations/Italian/races.json new file mode 100644 index 0000000..2e29263 --- /dev/null +++ b/JotunnModStub/Translations/Italian/races.json @@ -0,0 +1,38 @@ +{ + "rov_menu_choose_race": "Scegli la tua razza", + "rov_menu_ok": "OK", + "rov_menu_back": "Indietro", + + "rov_race_human_name": "Umano", + "rov_race_human_desc": "I versatili fondatori di Midgard. Statistiche equilibrate, senza debolezze particolari.", + + "rov_race_fenrir_name": "Fenrir", + "rov_race_fenrir_desc": "Maledetti dal sangue del lupo. Veloci e letali, ma vulnerabili all'argento.", + + "rov_race_skadi_name": "Skadi", + "rov_race_skadi_desc": "Figli delle montagne. Resistenti al gelo, a casa loro nel freddo più rigido.", + + "rov_race_skeleton_name": "Scheletro", + "rov_race_skeleton_desc": "Guerrieri non morti. Immuni al veleno, ma fragili contro i colpi contundenti.", + + "rov_race_aswid_name": "Aswid", + "rov_race_aswid_desc": "Feroce cacciatore notturno. Forte e resistente, si trasforma in pipistrello.", + + "rov_race_furling_name": "Furling", + "rov_race_furling_desc": "Astute creature simili a goblin. Agili e piene di risorse.", + + "rov_race_trol_name": "Trol", + "rov_race_trol_desc": "Forza bruta smisurata. Sovrasta tutti gli altri ed è in pace con la Foresta Nera, ma il fuoco brucia un Trol il $1% in più di chiunque altro.", + + "rov_race_draugh_name": "Draugh", + "rov_race_draugh_desc": "Un guerriero non morto in decomposizione, alimentato da magia oscura e metallo estremo.", + + "rov_race_dvergar_name": "Dvergar", + "rov_race_dvergar_desc": "Piccoli e testardi fabbri del profondo. Fragili nel fisico, ma maestri di metallo, miniera e mercato.", + + "rov_race_trenta_name": "Trenta", + "rov_race_trenta_desc": "Popolo nato dagli alberi, parente di radici e foglie. Paziente e difficile da abbattere.", + + "rov_race_sverm_name": "Sverm", + "rov_race_sverm_desc": "Stirpe dello sciame nata dalla chitina. Corazzata dalla nascita e mai rallentata, velenosa, in pace con ogni cosa che striscia, ma i colpi contundenti e il fuoco trovano le giunture." +}