+2 votes

I know I can set a button's text to a translation key, and it gets automatically translated this way. But what if I set the text by code using GDScript?

in Engine by (9,692 points)

1 Answer

+4 votes
Best answer

Use the tr(text) method, like this:

func _ready():

This will automatically look up the key HELLO when the project starts, and translate it to the user's language (if found in the localization table).

If you need to perform replacements in the translated text, you can do something like this:

func _ready():
    print(tr("HELLO_NAME").replace("%1", player_name))

For this to work, your translation text will have to contain a text that matches the %1. Note that %1 is just an example, you could use anything instead, it should probably be unique in each string though.

by (9,692 points)
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 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 webmaster@godotengine.org with your username.