Browse Source

Merge pull request #716 from voylin/Added_run-time_translation_example_to_Translation_Demo

Added run-time translation example for Translation Demo 4.0-dev
Aaron Franke 3 years ago
parent
commit
353dc4d2d6

+ 1 - 0
gui/translation/text.csv

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

+ 16 - 0
gui/translation/translation_demo.gd

@@ -1,5 +1,9 @@
 extends Control
 extends Control
 
 
+func _ready() -> void:
+	_set_text_in_label()
+
+
 func _on_english_pressed():
 func _on_english_pressed():
 	TranslationServer.set_locale("en")
 	TranslationServer.set_locale("en")
 
 
@@ -14,3 +18,15 @@ func _on_japanese_pressed():
 
 
 func _on_play_pressed():
 func _on_play_pressed():
 	$Audio.play()
 	$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()

+ 19 - 0
gui/translation/translation_demo.tscn

@@ -97,6 +97,25 @@ texture = ExtResource( "4" )
 [node name="Audio" type="AudioStreamPlayer" parent="."]
 [node name="Audio" type="AudioStreamPlayer" parent="."]
 stream = ExtResource( "5" )
 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="English" to="." method="_on_english_pressed"]
 [connection signal="pressed" from="Spanish" to="." method="_on_spanish_pressed"]
 [connection signal="pressed" from="Spanish" to="." method="_on_spanish_pressed"]
 [connection signal="pressed" from="Japanese" to="." method="_on_japanese_pressed"]
 [connection signal="pressed" from="Japanese" to="." method="_on_japanese_pressed"]