How to change label text language instantly using set_locale function?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By earlroxas
:warning: Old Version Published before Godot 3 was released.

So, i’ve been testing the translation or localization in godot engine. If I type the id from translation in the label, the text displayed will change according to current selected locale / language.

So, what i want to do is change language using script (using TranslationServer.setlocale right?) without reloading the scene, because from what I test, the label text will change after the scene reloaded, not instantly changed when TranslationServer.setlocale called. Can i do this? Please help.

AFAIK, there is no way but reloading scene or reassign all text values again.

volzhs | 2017-03-23 06:19

Well, reassign all text is my last resort.

earlroxas | 2017-03-24 01:54

:bust_in_silhouette: Reply From: mokalux

hello there,

for my projects i do this, in global.gd (for example) or anywhere you prefer.

var g_langue = ""

func _ready()
g_langue = OS.get_locale().left(2)

func getLangue():
	return g_langue

func setLangue(l):
	g_langue = l

Then in any scene i have 2 buttons (1 for french and another for english)

func _input(event):
	if get_node("buttonFr").is_pressed():
		get_node("/root/global").setLangue("fr")
		langues()
	if get_node("buttonEn").is_pressed():
		get_node("/root/global").setLangue("en")
		langues()

func langues():
	mylangue = get_node("/root/global").getLangue()

	if mylangue == "fr":
		get_node("buttonFr").set_self_opacity(1.0)
		get_node("buttonEn").set_self_opacity(0.5)
		get_node("yourNode").set_text("c'est en français")
	else:
		get_node("buttonEn").set_self_opacity(1.0)
		get_node("buttonFr").set_self_opacity(0.5)
		get_node("yourNode").set_text("this is english")

Then on a press of a button (or something else) you can change language.

Voilà.

Thanks for the answer, but it’s just using settext, i know that the simplest way is using settext but doing that to all the label is very time consuming.

earlroxas | 2017-03-24 01:50

You shouldn’t manually localize each label, but prefer using proper localization

timkrief | 2020-01-09 16:16

this is a 2017 post
:slight_smile:

mokalux | 2020-01-30 11:14