Skip to content

Commit 9e96c7d

Browse files
committed
Merge pull request #110378 from timothyqiu/rtl-preview
Make text-related nodes translation domain aware
2 parents 2edad68 + 172c80d commit 9e96c7d

23 files changed

+131
-100
lines changed

core/object/object.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,19 @@ bool Object::_uses_signal_mutex() const {
16881688
return true;
16891689
}
16901690

1691+
String Object::_get_locale() const {
1692+
TranslationServer *ts = TranslationServer::get_singleton();
1693+
const StringName domain_name = get_translation_domain();
1694+
if (ts->has_domain(domain_name)) {
1695+
const Ref<TranslationDomain> domain = ts->get_or_add_domain(domain_name);
1696+
const String &overridden = domain->get_locale_override();
1697+
if (!overridden.is_empty()) {
1698+
return overridden;
1699+
}
1700+
}
1701+
return ts->get_locale();
1702+
}
1703+
16911704
void Object::_set_bind(const StringName &p_set, const Variant &p_value) {
16921705
set(p_set, p_value);
16931706
}

core/object/object.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,9 @@ class Object {
801801

802802
virtual bool _uses_signal_mutex() const;
803803

804+
// Internal helper to get the current locale, taking into account the translation domain.
805+
String _get_locale() const;
806+
804807
#ifdef TOOLS_ENABLED
805808
struct VirtualMethodTracker {
806809
void **method;

editor/editor_node.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,10 @@ void EditorNode::_update_from_settings() {
455455
String current_fallback_locale = GLOBAL_GET("internationalization/locale/fallback");
456456
if (current_fallback_locale != TranslationServer::get_singleton()->get_fallback_locale()) {
457457
TranslationServer::get_singleton()->set_fallback_locale(current_fallback_locale);
458+
Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_main_domain();
459+
if (!domain->is_enabled()) {
460+
domain->set_locale_override(current_fallback_locale);
461+
}
458462
scene_root->propagate_notification(Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED);
459463
}
460464

@@ -4203,8 +4207,15 @@ void EditorNode::set_preview_locale(const String &p_locale) {
42034207
// Texts set in the editor could be identifiers that should never be translated.
42044208
// So we need to disable translation entirely.
42054209
Ref<TranslationDomain> main_domain = TranslationServer::get_singleton()->get_main_domain();
4206-
main_domain->set_enabled(!p_locale.is_empty());
4207-
main_domain->set_locale_override(p_locale);
4210+
if (p_locale.is_empty()) {
4211+
// Disable preview. Use the fallback locale.
4212+
main_domain->set_enabled(false);
4213+
main_domain->set_locale_override(TranslationServer::get_singleton()->get_fallback_locale());
4214+
} else {
4215+
// Preview a specific locale.
4216+
main_domain->set_enabled(true);
4217+
main_domain->set_locale_override(p_locale);
4218+
}
42084219

42094220
_translation_resources_changed();
42104221
}
@@ -7638,7 +7649,10 @@ EditorNode::EditorNode() {
76387649
ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &EditorNode::_update_from_settings));
76397650
GDExtensionManager::get_singleton()->connect("extensions_reloaded", callable_mp(this, &EditorNode::_gdextensions_reloaded));
76407651

7641-
TranslationServer::get_singleton()->get_main_domain()->set_enabled(false);
7652+
Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_main_domain();
7653+
domain->set_enabled(false);
7654+
domain->set_locale_override(TranslationServer::get_singleton()->get_fallback_locale());
7655+
76427656
// Load settings.
76437657
if (!EditorSettings::get_singleton()) {
76447658
EditorSettings::create();

scene/3d/label_3d.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,8 @@ void Label3D::_notification(int p_what) {
215215
window->disconnect("size_changed", callable_mp(this, &Label3D::_font_changed));
216216
} break;
217217
case NOTIFICATION_TRANSLATION_CHANGED: {
218-
String new_text = atr(text);
219-
if (new_text == xl_text) {
220-
return; // Nothing new.
221-
}
222-
xl_text = new_text;
218+
// Language update might change the appearance of some characters.
219+
xl_text = atr(text);
223220
dirty_text = true;
224221
_queue_update();
225222
} break;
@@ -483,8 +480,9 @@ void Label3D::_shape() {
483480
TS->shaped_text_clear(text_rid);
484481
TS->shaped_text_set_direction(text_rid, text_direction);
485482

486-
String txt = (uppercase) ? TS->string_to_upper(xl_text, language) : xl_text;
487-
TS->shaped_text_add_string(text_rid, txt, font->get_rids(), font_size, font->get_opentype_features(), language);
483+
const String &lang = language.is_empty() ? _get_locale() : language;
484+
String txt = uppercase ? TS->string_to_upper(xl_text, lang) : xl_text;
485+
TS->shaped_text_add_string(text_rid, txt, font->get_rids(), font_size, font->get_opentype_features(), lang);
488486

489487
TypedArray<Vector3i> stt;
490488
if (st_parser == TextServer::STRUCTURED_TEXT_CUSTOM) {

scene/gui/button.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,8 @@ void Button::_shape(Ref<TextParagraph> p_paragraph, String p_text) const {
566566
} else {
567567
p_paragraph->set_direction((TextServer::Direction)text_direction);
568568
}
569-
p_paragraph->add_string(p_text, font, font_size, language);
569+
const String &lang = language.is_empty() ? _get_locale() : language;
570+
p_paragraph->add_string(p_text, font, font_size, lang);
570571
p_paragraph->set_text_overrun_behavior(overrun_behavior);
571572
}
572573

scene/gui/code_edit.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,14 @@ void CodeEdit::_notification(int p_what) {
188188
code_completion_line_ofs = CLAMP((code_completion_force_item_center < 0 ? code_completion_current_selected : code_completion_force_item_center) - lines / 2, 0, code_completion_options_count - lines);
189189
RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(code_completion_rect.position.x, code_completion_rect.position.y + (code_completion_current_selected - code_completion_line_ofs) * row_height), Size2(code_completion_rect.size.width, row_height)), theme_cache.code_completion_selected_color);
190190

191+
const String &lang = _get_locale();
191192
for (int i = 0; i < lines; i++) {
192193
int l = code_completion_line_ofs + i;
193194
ERR_CONTINUE(l < 0 || l >= code_completion_options_count);
194195

195196
Ref<TextLine> tl;
196197
tl.instantiate();
197-
tl->add_string(code_completion_options[l].display, theme_cache.font, theme_cache.font_size);
198+
tl->add_string(code_completion_options[l].display, theme_cache.font, theme_cache.font_size, lang);
198199

199200
int yofs = (row_height - tl->get_size().y) / 2;
200201
Point2 title_pos(code_completion_rect.position.x, code_completion_rect.position.y + i * row_height + yofs);
@@ -1521,14 +1522,15 @@ void CodeEdit::_line_number_draw_callback(int p_line, int p_gutter, const Rect2
15211522
if (E) {
15221523
text_rid = E->value;
15231524
} else {
1525+
const String &lang = _get_locale();
15241526
String fc = String::num_int64(p_line + 1).lpad(line_number_digits, line_number_padding);
15251527
if (is_localizing_numeral_system()) {
1526-
fc = TS->format_number(fc);
1528+
fc = TS->format_number(fc, lang);
15271529
}
15281530

15291531
text_rid = TS->create_shaped_text();
15301532
if (theme_cache.font.is_valid()) {
1531-
TS->shaped_text_add_string(text_rid, fc, theme_cache.font->get_rids(), theme_cache.font_size, theme_cache.font->get_opentype_features());
1533+
TS->shaped_text_add_string(text_rid, fc, theme_cache.font->get_rids(), theme_cache.font_size, theme_cache.font->get_opentype_features(), lang);
15321534
}
15331535
line_number_text_cache.insert(p_line, text_rid);
15341536
}

scene/gui/control.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "core/input/input_map.h"
3737
#include "core/os/os.h"
3838
#include "core/string/string_builder.h"
39-
#include "core/string/translation_server.h"
4039
#include "scene/gui/scroll_container.h"
4140
#include "scene/main/canvas_layer.h"
4241
#include "scene/main/window.h"
@@ -3548,12 +3547,9 @@ bool Control::is_layout_rtl() const {
35483547
} else if (proj_root_layout_direction == 2) {
35493548
data.is_rtl = true;
35503549
} else if (proj_root_layout_direction == 3) {
3551-
String locale = OS::get_singleton()->get_locale();
3552-
data.is_rtl = TS->is_locale_right_to_left(locale);
3550+
data.is_rtl = TS->is_locale_right_to_left(OS::get_singleton()->get_locale());
35533551
} else {
3554-
const Ref<Translation> &t = TranslationServer::get_singleton()->get_translation_object(TranslationServer::get_singleton()->get_locale());
3555-
String locale = t.is_valid() ? t->get_locale() : TranslationServer::get_singleton()->get_fallback_locale();
3556-
data.is_rtl = TS->is_locale_right_to_left(locale);
3552+
data.is_rtl = TS->is_locale_right_to_left(_get_locale());
35573553
}
35583554
return data.is_rtl;
35593555
}
@@ -3564,8 +3560,9 @@ bool Control::is_layout_rtl() const {
35643560
return data.is_rtl;
35653561
}
35663562
#endif // TOOLS_ENABLED
3563+
const StringName domain_name = get_translation_domain();
35673564
Node *parent_node = get_parent();
3568-
while (parent_node) {
3565+
while (parent_node && domain_name == parent_node->get_translation_domain()) {
35693566
Control *parent_control = Object::cast_to<Control>(parent_node);
35703567
if (parent_control) {
35713568
data.is_rtl = parent_control->is_layout_rtl();
@@ -3588,22 +3585,19 @@ bool Control::is_layout_rtl() const {
35883585
String locale = OS::get_singleton()->get_locale();
35893586
data.is_rtl = TS->is_locale_right_to_left(locale);
35903587
} else {
3591-
String locale = TranslationServer::get_singleton()->get_tool_locale();
3592-
data.is_rtl = TS->is_locale_right_to_left(locale);
3588+
data.is_rtl = TS->is_locale_right_to_left(_get_locale());
35933589
}
35943590
} else if (data.layout_dir == LAYOUT_DIRECTION_APPLICATION_LOCALE) {
35953591
if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
35963592
data.is_rtl = true;
35973593
} else {
3598-
String locale = TranslationServer::get_singleton()->get_tool_locale();
3599-
data.is_rtl = TS->is_locale_right_to_left(locale);
3594+
data.is_rtl = TS->is_locale_right_to_left(_get_locale());
36003595
}
36013596
} else if (data.layout_dir == LAYOUT_DIRECTION_SYSTEM_LOCALE) {
36023597
if (GLOBAL_GET_CACHED(bool, "internationalization/rendering/force_right_to_left_layout_direction")) {
3603-
const_cast<Control *>(this)->data.is_rtl = true;
3598+
data.is_rtl = true;
36043599
} else {
3605-
String locale = OS::get_singleton()->get_locale();
3606-
const_cast<Control *>(this)->data.is_rtl = TS->is_locale_right_to_left(locale);
3600+
data.is_rtl = TS->is_locale_right_to_left(OS::get_singleton()->get_locale());
36073601
}
36083602
} else {
36093603
data.is_rtl = (data.layout_dir == LAYOUT_DIRECTION_RTL);

scene/gui/foldable_container.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,8 @@ void FoldableContainer::_shape() {
488488
}
489489
text_buf->set_horizontal_alignment(_get_actual_alignment());
490490
text_buf->set_text_overrun_behavior(overrun_behavior);
491-
text_buf->add_string(atr(title), font, font_size, language);
491+
const String &lang = language.is_empty() ? _get_locale() : language;
492+
text_buf->add_string(atr(title), font, font_size, lang);
492493
}
493494

494495
HorizontalAlignment FoldableContainer::_get_actual_alignment() const {

scene/gui/item_list.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ void ItemList::_shape_text(int p_idx) {
4343
} else {
4444
item.text_buf->set_direction((TextServer::Direction)item.text_direction);
4545
}
46-
item.text_buf->add_string(item.xl_text, theme_cache.font, theme_cache.font_size, item.language);
46+
const String &lang = item.language.is_empty() ? _get_locale() : item.language;
47+
item.text_buf->add_string(item.xl_text, theme_cache.font, theme_cache.font_size, lang);
4748
if (icon_mode == ICON_MODE_TOP && max_text_lines > 0) {
4849
item.text_buf->set_break_flags(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES);
4950
} else {

scene/gui/label.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ int Label::get_line_height(int p_line) const {
136136
}
137137

138138
void Label::_shape() const {
139+
const String &lang = language.is_empty() ? _get_locale() : language;
140+
139141
Ref<StyleBox> style = theme_cache.normal_style;
140142
int width = (get_size().width - style->get_minimum_size().width);
141143

@@ -149,7 +151,7 @@ void Label::_shape() const {
149151
}
150152
paragraphs.clear();
151153

152-
String txt = (uppercase) ? TS->string_to_upper(xl_text, language) : xl_text;
154+
String txt = (uppercase) ? TS->string_to_upper(xl_text, lang) : xl_text;
153155
if (visible_chars >= 0 && visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
154156
txt = txt.substr(0, visible_chars);
155157
}
@@ -183,7 +185,7 @@ void Label::_shape() const {
183185
ERR_FAIL_COND(font.is_null());
184186

185187
if (para.dirty) {
186-
TS->shaped_text_add_string(para.text_rid, para.text, font->get_rids(), font_size, font->get_opentype_features(), language);
188+
TS->shaped_text_add_string(para.text_rid, para.text, font->get_rids(), font_size, font->get_opentype_features(), lang);
187189
} else {
188190
int spans = TS->shaped_get_span_count(para.text_rid);
189191
for (int i = 0; i < spans; i++) {
@@ -709,15 +711,14 @@ void Label::_notification(int p_what) {
709711
} break;
710712

711713
case NOTIFICATION_TRANSLATION_CHANGED: {
712-
String new_text = atr(text);
713-
if (new_text == xl_text) {
714-
return; // Nothing new.
715-
}
716-
xl_text = new_text;
717-
if (visible_ratio < 1) {
718-
visible_chars = get_total_character_count() * visible_ratio;
714+
const String new_text = atr(text);
715+
if (new_text != xl_text) {
716+
xl_text = new_text;
717+
if (visible_ratio < 1) {
718+
visible_chars = get_total_character_count() * visible_ratio;
719+
}
719720
}
720-
text_dirty = true;
721+
text_dirty = true; // Language update might change the appearance of some characters.
721722

722723
queue_accessibility_update();
723724
queue_redraw();

0 commit comments

Comments
 (0)