From 8f8178bda6d74e09283df85d4cb34a52843e1892 Mon Sep 17 00:00:00 2001 From: kobewi Date: Fri, 17 Mar 2023 01:58:30 +0100 Subject: [PATCH] Fix auto-translations in editor --- core/object/object.cpp | 14 ++++++++++++-- scene/gui/control.cpp | 7 +++++++ scene/gui/menu_bar.cpp | 4 +++- scene/main/node.cpp | 5 +++++ scene/main/node.h | 1 + scene/main/timer.cpp | 2 +- scene/main/window.cpp | 9 ++++++++- 7 files changed, 37 insertions(+), 5 deletions(-) diff --git a/core/object/object.cpp b/core/object/object.cpp index 57aa1339ece0..c324eab9bbd2 100644 --- a/core/object/object.cpp +++ b/core/object/object.cpp @@ -1378,7 +1378,12 @@ String Object::tr(const StringName &p_message, const StringName &p_context) cons if (!_can_translate || !TranslationServer::get_singleton()) { return p_message; } - return TranslationServer::get_singleton()->translate(p_message, p_context); + + if (Engine::get_singleton()->is_editor_hint()) { + return TranslationServer::get_singleton()->tool_translate(p_message, p_context); + } else { + return TranslationServer::get_singleton()->translate(p_message, p_context); + } } String Object::tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const { @@ -1389,7 +1394,12 @@ String Object::tr_n(const StringName &p_message, const StringName &p_message_plu } return p_message_plural; } - return TranslationServer::get_singleton()->translate_plural(p_message, p_message_plural, p_n, p_context); + + if (Engine::get_singleton()->is_editor_hint()) { + return TranslationServer::get_singleton()->tool_translate_plural(p_message, p_message_plural, p_n, p_context); + } else { + return TranslationServer::get_singleton()->translate_plural(p_message, p_message_plural, p_n, p_context); + } } void Object::_clear_internal_resource_paths(const Variant &p_var) { diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index 10f9529ca96f..f78afed2c343 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -2912,6 +2912,13 @@ void Control::_notification(int p_notification) { } break; case NOTIFICATION_ENTER_TREE: { +#ifdef TOOLS_ENABLED + if (is_part_of_edited_scene()) { + // Don't translate Controls on scene when inside editor. + set_message_translation(false); + notification(NOTIFICATION_TRANSLATION_CHANGED); + } +#endif notification(NOTIFICATION_THEME_CHANGED); } break; diff --git a/scene/gui/menu_bar.cpp b/scene/gui/menu_bar.cpp index 0c12b98ad765..38fd61689240 100644 --- a/scene/gui/menu_bar.cpp +++ b/scene/gui/menu_bar.cpp @@ -249,9 +249,11 @@ void MenuBar::_update_submenu(const String &p_menu_name, PopupMenu *p_child) { } bool MenuBar::is_native_menu() const { - if (Engine::get_singleton()->is_editor_hint() && is_inside_tree() && get_tree()->get_edited_scene_root() && (get_tree()->get_edited_scene_root()->is_ancestor_of(this) || get_tree()->get_edited_scene_root() == this)) { +#ifdef TOOLS_ENABLED + if (is_part_of_edited_scene()) { return false; } +#endif return (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_GLOBAL_MENU) && is_native); } diff --git a/scene/main/node.cpp b/scene/main/node.cpp index 62ce887c930f..22bcfc947bd4 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -2073,6 +2073,11 @@ bool Node::is_property_pinned(const StringName &p_property) const { StringName Node::get_property_store_alias(const StringName &p_property) const { return p_property; } + +bool Node::is_part_of_edited_scene() const { + return Engine::get_singleton()->is_editor_hint() && is_inside_tree() && get_tree()->get_edited_scene_root() && + (get_tree()->get_edited_scene_root() == this || get_tree()->get_edited_scene_root()->is_ancestor_of(this)); +} #endif void Node::get_storable_properties(HashSet &r_storable_properties) const { diff --git a/scene/main/node.h b/scene/main/node.h index 493578bc5be0..8ce42d04bdb5 100644 --- a/scene/main/node.h +++ b/scene/main/node.h @@ -382,6 +382,7 @@ public: void set_property_pinned(const String &p_property, bool p_pinned); bool is_property_pinned(const StringName &p_property) const; virtual StringName get_property_store_alias(const StringName &p_property) const; + bool is_part_of_edited_scene() const; #endif void get_storable_properties(HashSet &r_storable_properties) const; diff --git a/scene/main/timer.cpp b/scene/main/timer.cpp index 4fd66312b7a2..0f4f18b495ea 100644 --- a/scene/main/timer.cpp +++ b/scene/main/timer.cpp @@ -35,7 +35,7 @@ void Timer::_notification(int p_what) { case NOTIFICATION_READY: { if (autostart) { #ifdef TOOLS_ENABLED - if (Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && (get_tree()->get_edited_scene_root() == this || get_tree()->get_edited_scene_root()->is_ancestor_of(this))) { + if (is_part_of_edited_scene()) { break; } #endif diff --git a/scene/main/window.cpp b/scene/main/window.cpp index 5d3173a2a545..1ac682715847 100644 --- a/scene/main/window.cpp +++ b/scene/main/window.cpp @@ -490,7 +490,7 @@ bool Window::is_embedded() const { bool Window::is_in_edited_scene_root() const { #ifdef TOOLS_ENABLED - return (Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && (get_tree()->get_edited_scene_root()->is_ancestor_of(this) || get_tree()->get_edited_scene_root() == this)); + return is_part_of_edited_scene(); #else return false; #endif @@ -1138,6 +1138,13 @@ void Window::_notification(int p_what) { RS::get_singleton()->viewport_set_active(get_viewport_rid(), true); } +#ifdef TOOLS_ENABLED + if (is_part_of_edited_scene()) { + // Don't translate Windows on scene when inside editor. + set_message_translation(false); + notification(NOTIFICATION_TRANSLATION_CHANGED); + } +#endif notification(NOTIFICATION_THEME_CHANGED); } break;