Formatting in c#

Godot Version

4.2

Question

how can i achieve this in c#:

message.text = tr("{character} picked up the {weapon}").format({character = "Ogre", weapon = "Sword"})

my problem is the .format cuz there is Tr() but not the format method.
what i have now

SubLabel.Text = Tr("oldman19")

oldman 19 is the tag for something like you are {name}

tr in gdscript is really is self.tr(...), method of Object - base class of all objects and returning String
.format is method of String
I’m not c# user and have not C# RN, but godot must use embed c# String and it have format method too, check what Tr return

I tested solution with {0}{1} placeholders, not named placeholders. Placeholders required only in translated text not to the key.
my translation.csv:

keys	en	pl
Ogre	Ogre	Ogr
Sword	Sword	Miecz
Pick1	{0} picked up the {1}	{0} podniósł {1}

obraz

         GD.Print(string.Format(Tr("Pick1"), Tr("Ogre"), Tr("Sword")));

obraz

remember to set Test if you don’t change translation in code
obraz

for named placeholders

keys	en	pl
Ogre	Ogre	Ogr
Sword	Sword	Miecz
Pick1	{0} picked up the {1}	{0} podniósł {1}
Pick2	{character} picked up the {weapon}	{character} podniósł {weapon}

obraz

        GD.Print(Tr("Pick2").Replace("{character}", Tr("Ogre")).Replace("{weapon}", Tr("Sword")));
1 Like

thank you very much the replace method was what i needed.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.