Skip to content

Commit 82a0290

Browse files
committed
Add auto translate mode for items in PopupMenu and OptionButton
1 parent cae3d72 commit 82a0290

File tree

13 files changed

+156
-21
lines changed

13 files changed

+156
-21
lines changed

doc/classes/OptionButton.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
Clears all the items in the [OptionButton].
4343
</description>
4444
</method>
45+
<method name="get_item_auto_translate_mode" qualifiers="const">
46+
<return type="int" enum="Node.AutoTranslateMode" />
47+
<param index="0" name="idx" type="int" />
48+
<description>
49+
Returns the auto translate mode of the item at index [param idx].
50+
</description>
51+
</method>
4552
<method name="get_item_icon" qualifiers="const">
4653
<return type="Texture2D" />
4754
<param index="0" name="idx" type="int" />
@@ -153,6 +160,15 @@
153160
If [code]true[/code], shortcuts are disabled and cannot be used to trigger the button.
154161
</description>
155162
</method>
163+
<method name="set_item_auto_translate_mode">
164+
<return type="void" />
165+
<param index="0" name="idx" type="int" />
166+
<param index="1" name="mode" type="int" enum="Node.AutoTranslateMode" />
167+
<description>
168+
Sets the auto translate mode of the item at index [param idx].
169+
Items use [constant Node.AUTO_TRANSLATE_MODE_INHERIT] by default, which uses the same auto translate mode as the [OptionButton] itself.
170+
</description>
171+
</method>
156172
<method name="set_item_disabled">
157173
<return type="void" />
158174
<param index="0" name="idx" type="int" />

doc/classes/PopupMenu.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@
237237
Returns the accelerator of the item at the given [param index]. An accelerator is a keyboard shortcut that can be pressed to trigger the menu button even if it's not currently open. The return value is an integer which is generally a combination of [enum KeyModifierMask]s and [enum Key]s using bitwise OR such as [code]KEY_MASK_CTRL | KEY_A[/code] ([kbd]Ctrl + A[/kbd]). If no accelerator is defined for the specified [param index], [method get_item_accelerator] returns [code]0[/code] (corresponding to [constant @GlobalScope.KEY_NONE]).
238238
</description>
239239
</method>
240+
<method name="get_item_auto_translate_mode" qualifiers="const">
241+
<return type="int" enum="Node.AutoTranslateMode" />
242+
<param index="0" name="index" type="int" />
243+
<description>
244+
Returns the auto translate mode of the item at the given [param index].
245+
</description>
246+
</method>
240247
<method name="get_item_icon" qualifiers="const">
241248
<return type="Texture2D" />
242249
<param index="0" name="index" type="int" />
@@ -462,6 +469,15 @@
462469
Mark the item at the given [param index] as a separator, which means that it would be displayed as a line. If [code]false[/code], sets the type of the item to plain text.
463470
</description>
464471
</method>
472+
<method name="set_item_auto_translate_mode">
473+
<return type="void" />
474+
<param index="0" name="index" type="int" />
475+
<param index="1" name="mode" type="int" enum="Node.AutoTranslateMode" />
476+
<description>
477+
Sets the auto translate mode of the item at the given [param index].
478+
Items use [constant Node.AUTO_TRANSLATE_MODE_INHERIT] by default, which uses the same auto translate mode as the [PopupMenu] itself.
479+
</description>
480+
</method>
465481
<method name="set_item_checked">
466482
<return type="void" />
467483
<param index="0" name="index" type="int" />

editor/editor_node.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4676,6 +4676,7 @@ void EditorNode::_update_recent_scenes() {
46764676

46774677
recent_scenes->add_separator();
46784678
recent_scenes->add_shortcut(ED_SHORTCUT("editor/clear_recent", TTRC("Clear Recent Scenes")));
4679+
recent_scenes->set_item_auto_translate_mode(-1, AUTO_TRANSLATE_MODE_ALWAYS);
46794680
recent_scenes->reset_size();
46804681
}
46814682

@@ -5625,6 +5626,7 @@ void EditorNode::_update_layouts_menu() {
56255626
}
56265627

56275628
editor_layouts->add_item(layout);
5629+
editor_layouts->set_item_auto_translate_mode(-1, AUTO_TRANSLATE_MODE_DISABLED);
56285630
}
56295631
}
56305632

@@ -7443,6 +7445,7 @@ EditorNode::EditorNode() {
74437445
file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/reopen_closed_scene", TTRC("Reopen Closed Scene"), KeyModifierMask::CMD_OR_CTRL + KeyModifierMask::SHIFT + Key::T), FILE_OPEN_PREV);
74447446

74457447
recent_scenes = memnew(PopupMenu);
7448+
recent_scenes->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
74467449
file_menu->add_submenu_node_item(TTR("Open Recent"), recent_scenes, FILE_OPEN_RECENT);
74477450
recent_scenes->connect(SceneStringName(id_pressed), callable_mp(this, &EditorNode::_open_recent_scene));
74487451

@@ -7572,7 +7575,6 @@ EditorNode::EditorNode() {
75727575
settings_menu->add_submenu_node_item(TTR("Editor Docks"), editor_dock_manager->get_docks_menu());
75737576

75747577
editor_layouts = memnew(PopupMenu);
7575-
editor_layouts->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
75767578
settings_menu->add_submenu_node_item(TTR("Editor Layout"), editor_layouts);
75777579
editor_layouts->connect(SceneStringName(id_pressed), callable_mp(this, &EditorNode::_layout_menu_option));
75787580
settings_menu->add_separator();

editor/plugins/script_editor_plugin.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,8 @@ void ScriptEditor::_update_recent_scripts() {
824824

825825
recent_scripts->add_separator();
826826
recent_scripts->add_shortcut(ED_GET_SHORTCUT("script_editor/clear_recent"));
827-
recent_scripts->set_item_disabled(recent_scripts->get_item_id(recent_scripts->get_item_count() - 1), rc.is_empty());
827+
recent_scripts->set_item_auto_translate_mode(-1, AUTO_TRANSLATE_MODE_ALWAYS);
828+
recent_scripts->set_item_disabled(-1, rc.is_empty());
828829

829830
recent_scripts->reset_size();
830831
}
@@ -4288,6 +4289,7 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
42884289
file_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_editor/reopen_closed_script"), FILE_REOPEN_CLOSED);
42894290

42904291
recent_scripts = memnew(PopupMenu);
4292+
recent_scripts->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
42914293
file_menu->get_popup()->add_submenu_node_item(TTR("Open Recent"), recent_scripts, FILE_OPEN_RECENT);
42924294
recent_scripts->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptEditor::_open_recent_script));
42934295

editor/scene_create_dialog.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ SceneCreateDialog::SceneCreateDialog() {
261261
ResourceSaver::get_recognized_extensions(sd, &extensions);
262262

263263
scene_extension_picker = memnew(OptionButton);
264+
scene_extension_picker->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
264265
hb->add_child(scene_extension_picker);
265266
for (const String &E : extensions) {
266267
scene_extension_picker->add_item("." + E);

editor/script_create_dialog.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ void ScriptCreateDialog::_update_template_menu() {
562562
if (!separator) {
563563
template_menu->add_separator();
564564
template_menu->set_item_text(-1, display_name);
565+
template_menu->set_item_auto_translate_mode(-1, AUTO_TRANSLATE_MODE_ALWAYS);
565566
separator = true;
566567
}
567568
for (ScriptLanguage::ScriptTemplate &t : templates_found) {
@@ -666,7 +667,7 @@ void ScriptCreateDialog::_update_dialog() {
666667
validation_panel->set_message(MSG_ID_PATH, TTR("Built-in script (into scene file)."), EditorValidationPanel::MSG_OK);
667668
}
668669
} else {
669-
template_inactive_message = TTR("Using existing script file.");
670+
template_inactive_message = TTRC("Using existing script file.");
670671
if (load_enabled) {
671672
if (is_path_valid) {
672673
validation_panel->set_message(MSG_ID_PATH, TTR("Will load an existing script file."), EditorValidationPanel::MSG_OK);
@@ -680,16 +681,17 @@ void ScriptCreateDialog::_update_dialog() {
680681
if (is_using_templates) {
681682
// Check if at least one suitable template has been found.
682683
if (template_menu->get_item_count() == 0 && template_inactive_message.is_empty()) {
683-
template_inactive_message = TTR("No suitable template.");
684+
template_inactive_message = TTRC("No suitable template.");
684685
}
685686
} else {
686-
template_inactive_message = TTR("Empty");
687+
template_inactive_message = TTRC("Empty");
687688
}
688689

689690
if (!template_inactive_message.is_empty()) {
690691
template_menu->set_disabled(true);
691692
template_menu->clear();
692693
template_menu->add_item(template_inactive_message);
694+
template_menu->set_item_auto_translate_mode(-1, AUTO_TRANSLATE_MODE_ALWAYS);
693695
validation_panel->set_message(MSG_ID_TEMPLATE, "", EditorValidationPanel::MSG_INFO);
694696
}
695697
}
@@ -817,11 +819,11 @@ ScriptLanguage::ScriptTemplate ScriptCreateDialog::_parse_template(const ScriptL
817819
String ScriptCreateDialog::_get_script_origin_label(const ScriptLanguage::TemplateLocation &p_origin) const {
818820
switch (p_origin) {
819821
case ScriptLanguage::TEMPLATE_BUILT_IN:
820-
return TTR("Built-in");
822+
return TTRC("Built-in");
821823
case ScriptLanguage::TEMPLATE_EDITOR:
822-
return TTR("Editor");
824+
return TTRC("Editor");
823825
case ScriptLanguage::TEMPLATE_PROJECT:
824-
return TTR("Project");
826+
return TTRC("Project");
825827
}
826828
return "";
827829
}
@@ -864,6 +866,7 @@ ScriptCreateDialog::ScriptCreateDialog() {
864866
/* Language */
865867

866868
language_menu = memnew(OptionButton);
869+
language_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
867870
language_menu->set_custom_minimum_size(Size2(350, 0) * EDSCALE);
868871
language_menu->set_expand_icon(true);
869872
language_menu->set_h_size_flags(Control::SIZE_EXPAND_FILL);
@@ -917,6 +920,7 @@ ScriptCreateDialog::ScriptCreateDialog() {
917920
template_inactive_message = "";
918921

919922
template_menu = memnew(OptionButton);
923+
template_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
920924
template_menu->set_h_size_flags(Control::SIZE_EXPAND_FILL);
921925
template_menu->connect(SceneStringName(item_selected), callable_mp(this, &ScriptCreateDialog::_template_changed));
922926
template_hb->add_child(template_menu);

editor/shader_create_dialog.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,13 @@ void ShaderCreateDialog::_type_changed(int p_language) {
312312
if (shader_type_data.use_templates) {
313313
int last_template = EditorSettings::get_singleton()->get_project_metadata("shader_setup", "last_selected_template", 0);
314314

315-
template_menu->add_item(TTR("Default"));
316-
template_menu->add_item(TTR("Empty"));
315+
template_menu->add_item(TTRC("Default"));
316+
template_menu->add_item(TTRC("Empty"));
317317

318318
template_menu->select(last_template);
319319
current_template = last_template;
320320
} else {
321-
template_menu->add_item(TTR("N/A"));
321+
template_menu->add_item(TTRC("N/A"));
322322
}
323323

324324
EditorSettings::get_singleton()->set_project_metadata("shader_setup", "last_selected_language", type_menu->get_item_text(type_menu->get_selected()));
@@ -572,6 +572,7 @@ ShaderCreateDialog::ShaderCreateDialog() {
572572
// Type.
573573

574574
type_menu = memnew(OptionButton);
575+
type_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
575576
type_menu->set_custom_minimum_size(Size2(250, 0) * EDSCALE);
576577
type_menu->set_h_size_flags(Control::SIZE_EXPAND_FILL);
577578
gc->add_child(memnew(Label(TTR("Type:"))));
@@ -612,6 +613,7 @@ ShaderCreateDialog::ShaderCreateDialog() {
612613
// Modes.
613614

614615
mode_menu = memnew(OptionButton);
616+
mode_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
615617
for (const String &type_name : ShaderTypes::get_singleton()->get_types_list()) {
616618
mode_menu->add_item(type_name.capitalize());
617619
}

scene/gui/button.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ void Button::_set_internal_margin(Side p_side, float p_value) {
4848
void Button::_queue_update_size_cache() {
4949
}
5050

51+
String Button::_get_translated_text(const String &p_text) const {
52+
return atr(p_text);
53+
}
54+
5155
void Button::_update_theme_item_cache() {
5256
Control::_update_theme_item_cache();
5357

@@ -186,7 +190,7 @@ void Button::_notification(int p_what) {
186190
} break;
187191

188192
case NOTIFICATION_TRANSLATION_CHANGED: {
189-
xl_text = atr(text);
193+
xl_text = _get_translated_text(text);
190194
_shape();
191195

192196
update_minimum_size();
@@ -598,14 +602,16 @@ TextServer::OverrunBehavior Button::get_text_overrun_behavior() const {
598602
}
599603

600604
void Button::set_text(const String &p_text) {
601-
if (text != p_text) {
602-
text = p_text;
603-
xl_text = atr(text);
604-
_shape();
605-
606-
queue_redraw();
607-
update_minimum_size();
605+
const String translated_text = _get_translated_text(p_text);
606+
if (text == p_text && xl_text == translated_text) {
607+
return;
608608
}
609+
text = p_text;
610+
xl_text = translated_text;
611+
_shape();
612+
613+
queue_redraw();
614+
update_minimum_size();
609615
}
610616

611617
String Button::get_text() const {

scene/gui/button.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class Button : public BaseButton {
110110

111111
void _set_internal_margin(Side p_side, float p_value);
112112
virtual void _queue_update_size_cache();
113+
virtual String _get_translated_text(const String &p_text) const;
113114

114115
Size2 _fit_icon_size(const Size2 &p_size) const;
115116
Ref<StyleBox> _get_current_stylebox() const;

scene/gui/option_button.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,21 @@ void OptionButton::set_item_tooltip(int p_idx, const String &p_tooltip) {
240240
popup->set_item_tooltip(p_idx, p_tooltip);
241241
}
242242

243+
void OptionButton::set_item_auto_translate_mode(int p_idx, AutoTranslateMode p_mode) {
244+
if (p_idx < 0) {
245+
p_idx += get_item_count();
246+
}
247+
if (popup->get_item_auto_translate_mode(p_idx) == p_mode) {
248+
return;
249+
}
250+
popup->set_item_auto_translate_mode(p_idx, p_mode);
251+
252+
if (current == p_idx) {
253+
set_text(popup->get_item_text(p_idx));
254+
}
255+
_queue_update_size_cache();
256+
}
257+
243258
void OptionButton::set_item_disabled(int p_idx, bool p_disabled) {
244259
popup->set_item_disabled(p_idx, p_disabled);
245260
}
@@ -272,6 +287,10 @@ String OptionButton::get_item_tooltip(int p_idx) const {
272287
return popup->get_item_tooltip(p_idx);
273288
}
274289

290+
Node::AutoTranslateMode OptionButton::get_item_auto_translate_mode(int p_idx) const {
291+
return popup->get_item_auto_translate_mode(p_idx);
292+
}
293+
275294
bool OptionButton::is_item_disabled(int p_idx) const {
276295
return popup->is_item_disabled(p_idx);
277296
}
@@ -431,6 +450,25 @@ void OptionButton::_queue_update_size_cache() {
431450
callable_mp(this, &OptionButton::_refresh_size_cache).call_deferred();
432451
}
433452

453+
String OptionButton::_get_translated_text(const String &p_text) const {
454+
if (0 <= current && current < popup->get_item_count()) {
455+
AutoTranslateMode mode = popup->get_item_auto_translate_mode(current);
456+
switch (mode) {
457+
case AUTO_TRANSLATE_MODE_INHERIT: {
458+
return atr(p_text);
459+
} break;
460+
case AUTO_TRANSLATE_MODE_ALWAYS: {
461+
return tr(p_text);
462+
} break;
463+
case AUTO_TRANSLATE_MODE_DISABLED: {
464+
return p_text;
465+
} break;
466+
}
467+
ERR_FAIL_V_MSG(atr(p_text), "Unexpected auto translate mode: " + itos(mode));
468+
}
469+
return atr(p_text);
470+
}
471+
434472
void OptionButton::select(int p_idx) {
435473
_select(p_idx, false);
436474
}
@@ -510,12 +548,14 @@ void OptionButton::_bind_methods() {
510548
ClassDB::bind_method(D_METHOD("set_item_id", "idx", "id"), &OptionButton::set_item_id);
511549
ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &OptionButton::set_item_metadata);
512550
ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &OptionButton::set_item_tooltip);
551+
ClassDB::bind_method(D_METHOD("set_item_auto_translate_mode", "idx", "mode"), &OptionButton::set_item_auto_translate_mode);
513552
ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &OptionButton::get_item_text);
514553
ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &OptionButton::get_item_icon);
515554
ClassDB::bind_method(D_METHOD("get_item_id", "idx"), &OptionButton::get_item_id);
516555
ClassDB::bind_method(D_METHOD("get_item_index", "id"), &OptionButton::get_item_index);
517556
ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &OptionButton::get_item_metadata);
518557
ClassDB::bind_method(D_METHOD("get_item_tooltip", "idx"), &OptionButton::get_item_tooltip);
558+
ClassDB::bind_method(D_METHOD("get_item_auto_translate_mode", "idx"), &OptionButton::get_item_auto_translate_mode);
519559
ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &OptionButton::is_item_disabled);
520560
ClassDB::bind_method(D_METHOD("is_item_separator", "idx"), &OptionButton::is_item_separator);
521561
ClassDB::bind_method(D_METHOD("add_separator", "text"), &OptionButton::add_separator, DEFVAL(String()));

0 commit comments

Comments
 (0)