+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?

asked Feb 22, 2016 in Engine by Calinou (6,560 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.

answered Feb 22, 2016 by Calinou (6,560 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.