Added run-time translation example

Fixes #647
I added an example of how you can handle translations on runtime.
Small fix
This commit is contained in:
Voylin 2022-04-03 19:08:44 +09:00
parent 3eda0c7fe1
commit a8d797cd46
3 changed files with 36 additions and 0 deletions

View File

@ -1,3 +1,4 @@
KEYS,en,es,ja
KEY_HELLO,Hello!,Hola!,こんにちは
KEY_PUSH,Push Me!,Aprétame!,押す
KEY_TEXT,This is translated!,¡Esto está traducido!,これが翻訳されていました!

1 KEYS en es ja
2 KEY_HELLO Hello! Hola! こんにちは
3 KEY_PUSH Push Me! Aprétame! 押す
4 KEY_TEXT This is translated! ¡Esto está traducido! これが翻訳されていました!

View File

@ -1,5 +1,9 @@
extends Control
func _ready() -> void:
_set_text_in_label()
func _on_english_pressed():
TranslationServer.set_locale("en")
@ -14,3 +18,15 @@ func _on_japanese_pressed():
func _on_play_pressed():
$Audio.play()
func _set_text_in_label():
# Use tr(translation_key) to get the desired string in the correct language.
var message := "This text is being translated through script: \n"
message += tr("KEY_TEXT")
$TextLabel.text = message
func _notification(what):
if what == NOTIFICATION_TRANSLATION_CHANGED:
_set_text_in_label()

View File

@ -97,6 +97,25 @@ texture = ExtResource( "4" )
[node name="Audio" type="AudioStreamPlayer" parent="."]
stream = ExtResource( "5" )
[node name="TextLabel" type="Label" parent="."]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = 21.0
offset_top = 80.0
offset_right = 487.0
offset_bottom = 255.0
grow_horizontal = 2
grow_vertical = 2
theme_override_fonts/font = ExtResource( "2_fnagj" )
text = "This text is being translated through script:
"
__meta__ = {
"_edit_layout_mode": 1,
"_edit_use_custom_anchors": false
}
[connection signal="pressed" from="English" to="." method="_on_english_pressed"]
[connection signal="pressed" from="Spanish" to="." method="_on_spanish_pressed"]
[connection signal="pressed" from="Japanese" to="." method="_on_japanese_pressed"]