The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+2 votes

So I have this string in the translation table (csv file):
goal_timed,Clear stage under [x] minutes,Selesaikan stage dalam [x] menit,[x]分にステージをクリアする

so I want to replace [x] with some number, how can i do that?
and also the goal is to print the string to Label and it must respond to TranslationServer.set_locale()

in Engine by (81 points)

1 Answer

+2 votes

You can change [x] to be an index for the string format function: {0}

So your localized strings would be like "Clear the stage in under {0} minutes".

And in gdscript, you'd do: tr("goal_timed").format(your_time_variable)

by (44 points)

Note that you can also use named placeholders, so you can swap their order in translated strings:

# "goal_timed" translates to "The goal {name} was scored {amount} times!".
tr("goal_timed").format({name="Some name", amount=20})

You can swap the order up with the indexes as well, can't you? Like "This {0} is {1}".format(["balloon", "red"]) vs "This is a {1} {0}".format(["balloon", "red"]). The names can be a convenience thing, but seem like a bit of unnecessary extra string parsing.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.