From e861ea590392fedeeede3996b9cd3ce575198494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Giet?= Date: Tue, 28 Feb 2023 16:48:49 +0100 Subject: [PATCH] Add translation support to RichTextLabel BUG: 34050 add translation support to RichTextLabel, and respect its Auto Translate setting --- scene/gui/rich_text_label.cpp | 26 ++++++++++++-------------- scene/gui/rich_text_label.h | 1 + 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/scene/gui/rich_text_label.cpp b/scene/gui/rich_text_label.cpp index 4e24c9ba06f..2c0e732fce1 100644 --- a/scene/gui/rich_text_label.cpp +++ b/scene/gui/rich_text_label.cpp @@ -34,6 +34,7 @@ #include "core/math/math_defs.h" #include "core/os/keyboard.h" #include "core/os/os.h" +#include "core/string/translation.h" #include "label.h" #include "scene/scene_string_names.h" #include "servers/display_server.h" @@ -1790,8 +1791,7 @@ void RichTextLabel::_notification(int p_what) { case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: case NOTIFICATION_TRANSLATION_CHANGED: { - _stop_thread(); - main->first_invalid_line.store(0); //invalidate ALL + _apply_translation(); queue_redraw(); } break; @@ -5114,13 +5114,17 @@ void RichTextLabel::set_text(const String &p_bbcode) { if (text == p_bbcode) { return; } - text = p_bbcode; + _apply_translation(); +} + +void RichTextLabel::_apply_translation() { + String xl_text = atr(text); if (use_bbcode) { - parse_bbcode(p_bbcode); + parse_bbcode(xl_text); } else { // raw text clear(); - add_text(p_bbcode); + add_text(xl_text); } } @@ -5135,13 +5139,7 @@ void RichTextLabel::set_use_bbcode(bool p_enable) { use_bbcode = p_enable; notify_property_list_changed(); - const String current_text = text; - if (use_bbcode) { - parse_bbcode(current_text); - } else { // raw text - clear(); - add_text(current_text); - } + _apply_translation(); } bool RichTextLabel::is_using_bbcode() const { @@ -5276,7 +5274,7 @@ float RichTextLabel::get_visible_ratio() const { void RichTextLabel::set_effects(Array p_effects) { custom_effects = p_effects; if ((!text.is_empty()) && use_bbcode) { - parse_bbcode(text); + parse_bbcode(atr(text)); } } @@ -5291,7 +5289,7 @@ void RichTextLabel::install_effect(const Variant effect) { ERR_FAIL_COND_MSG(rteffect.is_null(), "Invalid RichTextEffect resource."); custom_effects.push_back(effect); if ((!text.is_empty()) && use_bbcode) { - parse_bbcode(text); + parse_bbcode(atr(text)); } } diff --git a/scene/gui/rich_text_label.h b/scene/gui/rich_text_label.h index e3f1e11e711..fef34f72603 100644 --- a/scene/gui/rich_text_label.h +++ b/scene/gui/rich_text_label.h @@ -527,6 +527,7 @@ private: #endif bool use_bbcode = false; String text; + void _apply_translation(); bool fit_content = false;