RichTextEffect get the ability to modify text as a feature?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By digitalcyclops

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 char_fx via tr(char_fx). 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:

草藥
雷峰塔

Any help or comment on this??

digitalcyclops | 2020-03-18 02:43

:bust_in_silhouette: Reply From: JimArtificer

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.