0 votes

I'm not sure if this is already possible. But I am trying to find a easy way to translate parts of a dialog which is a block of bbcode in a RichTextLabel. It would be very nice if I can do things such as "[tr]translate this[/tr]" and have the RichTextEffect modify charfx via tr(charfx). Also, regex is not working with tr():

var test : String = "[item][tr]Herb[/tr][/item] is nice. [place][tr]Thunder Tower[/tr][/place] is far.";
var regx : RegEx = RegEx.new();
regx.compile("\\[tr\\]([\\w|\\s]+)\\[\\/tr\\]?");
print(TranslationServer.get_locale() + " " + regx.sub(test, tr("$1"), true));

Prints:

  zh [item]Herb[/item] is nice. [place]Thunder Tower[/place] is far.

The code below works:

print(tr("Herb"));
print(tr("Thunder Tower"));

Prints:

草藥
雷峰塔
asked Mar 16 in Engine by digitalcyclops (14 points)
edited Mar 17 by digitalcyclops

Any help or comment on this??

1 Answer

0 votes

Generally speaking, when it comes to internationalization you don't mix partial translations in a line of text. You cannot make assumptions about sentence structure or how pluralization is handled between languages.

The standard way of handling translations with Godot is to have your translation strings defined in a CSV file. The engine will process it and create .translation files that you add to your project settings.

In your particular case, it looks like you want English text in more than one translation. That means some of your translation strings will look very similar to each other. Nothing wrong with that, though you may want to translate the entire string.

Example CSV file:

keys,en,zh
message1,"The herb is nice", "The 草藥 is nice"
message2,"Thunder Tower is far", "雷峰塔 is far"

You could certainly also add bbcode to the translation strings and feed them into the bbcode text field of a RichTextLabel if you wanted to. Or add variables to them, as described in the answer to this question.

The main takeaway here is to be feeding simple ids like message1 into the tr() function. Once you have the right string you can manipulate it for display purposes.

answered Jun 15 by JimArtificer (852 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.