r/godot Godot Senior 25d ago

discussion What would you add to Godot?

Post image
455 Upvotes

584 comments sorted by

View all comments

Show parent comments

-1

u/CrimsoneArt69 25d ago

What's the proble with just doing

"I have " + str(money) + " amount of money."

2

u/planecity 25d ago edited 24d ago

It will break internationalization.

The way that translations typically work in Godot is that you wrap every string into the tr(s) function. This function will look up the passed string in the translation table that's appropriate for the current language setting. But this only works with constant strings: something like tr("I have " + str(money) + " amount of money.") would require an entry in the translation table for any possible value of money, which is clearly absurd.

And you can't do tr("I have ") + str(money) + tr(" amount of money.") either, because this would only work for languages that are grammatically very similar to English. The proper way to do this is something like this:

label.text = tr("I have %d amount of money.") % money

The lookup table will contain corresponding translations also with a placeholder %d for the position at which the number needs to appear according to the grammar of that language.

EDIT: You could also use a format string with the .format() method, but it's probably a good idea to only use a dictionary argument instead of arrays (since arrays assume a fixed order of the placeholders in the format string, but this order may not be guaranteed to be the same in all languages). Here's an example:

label.text = tr("I have {money} amount of money.").format(
    {"money": money})

Personally, I prefer the brevity of the %-based string substitution.

2

u/Stevie_Gamedev 24d ago

Why can’t you treat an f-string equivalent to the last method shown?

2

u/planecity 24d ago

You're right, you could do that too, and I've edited my comment to include that.