How to localize editor plugins?

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

How can I use the translation server to translate strings on an editor plugin? It just doesn’t work as expected.

On my plugin.gd, on _enter_tree(), I do something like this

TranslationServer.add_translation(load("res://addons/myaddon/strings.pt.translation"))
print(tr("hi")) # prints "hi"
TranslationServer.set_locale("pt")
print(tr("hi")) # should print "oi" but prints "hi"

But if I create a normal scene and run the exactly same code on _ready(), it prints exactly what’s expected

TranslationServer.add_translation(load("res://addons/myaddon/strings.pt.translation"))
print(tr("hi")) # prints "hi"
TranslationServer.set_locale("pt")
print(tr("hi")) # prints "oi" exactly as expected.

Maybe it is a bug? Or is this expected behavior and I should do something different when localizing plugins?

I would like to add that those tests are made after reloading the editor one time, that is, after the csv has been properly imported by the editor and the “*.pt.translation” has been generated.

:bust_in_silhouette: Reply From: akoutsoulelos

I just run into the same problem.

It seems that the TranslationServer does not initialize in the Editor, so any translation added, is just dropped.
I came to a solution, where I create a Translation object, load there the desired translation and use the Translation.get_message(String message) function where I want translated texts - it’s very straight forward and simple.

Also, this is how I get the Editor’s locale:

var ePlugin := EditorPlugin.new()
var eInterface : EditorInterface = ePlugin.get_editor_interface()
var locale : String = eInterface.get_editor_settings().get_setting("interface/editor/editor_language")