classes/class_richtextlabel #343
Replies: 1 comment
-
Here's a simple script for a localized RichTextLabel: ## A RichTextLabel that localizes its own text, even when using bbcode.
class_name LocalizedRichTextLabel
extends RichTextLabel
enum _From {
ON_FINISHED = 0,
NOTIFICATION,
SIZE,
}
var _text_key: String = ""
var _original_text: String = ""
var _previous_text: String = ""
var _needs_update: bool = true
var _needs_update_from_notification: bool = true
func _notification(what: int) -> void:
if what == NOTIFICATION_TRANSLATION_CHANGED:
if text.is_empty() or not _needs_update_from_notification:
return
if _text_key.is_empty(): # as this node enters the tree
_text_key = get_parsed_text()
_original_text = text
_update_locale(_From.NOTIFICATION)
func _enter_tree() -> void:
if not finished.is_connected(_on_finished):
finished.connect(_on_finished)
func _on_finished() -> void:
if not _needs_update:
_needs_update = true
return
_update_locale(_From.ON_FINISHED)
func _update_locale(from: _From) -> void:
match from:
_From.ON_FINISHED: # text changed
var new_key: String = get_parsed_text()
var _translated_text := TranslationServer.translate(new_key)
if _translated_text == TranslationServer.translate(text): # there was no bbcode to parse, so the engine will translate it
_needs_update_from_notification = false
_needs_update = true
return
_needs_update_from_notification = true
_needs_update = false
_text_key = new_key
_original_text = text
_previous_text = text
text = _original_text.replace(_text_key, _translated_text)
_From.NOTIFICATION: # locale changed
_needs_update = false
var _translated_text := TranslationServer.translate(_text_key)
_previous_text = text
text = _original_text.replace(_text_key, _translated_text) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
classes/class_richtextlabel
Inherits: Control< CanvasItem< Node< Object A control for displaying text that can contain different font styles, images, and basic formatting. Description: A control for displaying text that can c...
https://docs.godotengine.org/en/stable/classes/class_richtextlabel.html
Beta Was this translation helpful? Give feedback.
All reactions