Skip to content

Commit

Permalink
Add translation_context to Control nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
jkirsteins committed Feb 20, 2025
1 parent 36d90c7 commit 5d05874
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
20 changes: 19 additions & 1 deletion editor/plugins/packed_scene_translation_parser_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path,
// Find the `auto_translate_mode` property.
bool auto_translating = true;
bool auto_translate_mode_found = false;
String translation_context = "";

for (int j = 0; j < state->get_node_property_count(i); j++) {
String property = state->get_node_property_name(i, j);

// If an old scene wasn't saved in the new version, the `auto_translate` property won't be converted into `auto_translate_mode`,
// so the deprecated property still needs to be checked as well.
// TODO: Remove the check for "auto_translate" once the property if fully removed.
Expand Down Expand Up @@ -113,6 +116,16 @@ Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path,
}
}

// TODO: only loop once over the properties (between this and auto_translate_mode)
for (int j = 0; j < state->get_node_property_count(i); j++) {
String property = state->get_node_property_name(i, j);

if (property == "translation_context") {
translation_context = String(state->get_node_property_value(i, j));
break;
}
}

// Parse the names of children of `TabContainer`s, as they are used for tab titles.
if (!tabcontainer_paths.is_empty()) {
if (!parent_path.begins_with(tabcontainer_paths[tabcontainer_paths.size() - 1])) {
Expand Down Expand Up @@ -168,9 +181,14 @@ Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path,
}
} else if (property_value.get_type() == Variant::STRING) {
String str_value = String(property_value);
Variant _ctx_value = state->get_node_property_value(i, j);
// Prevent reading text containing only spaces.
if (!str_value.strip_edges().is_empty()) {
parsed_strings.push_back(str_value);
Vector<String> tmp_id_ctx_plural;
tmp_id_ctx_plural.push_back(str_value);
tmp_id_ctx_plural.push_back(translation_context);
tmp_id_ctx_plural.push_back("");
r_ids_ctx_plural->append(tmp_id_ctx_plural);
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions scene/gui/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3254,6 +3254,17 @@ String Control::get_tooltip_text() const {
return data.tooltip;
}

void Control::set_translation_context(const String &p_translation_context) {
ERR_MAIN_THREAD_GUARD;
data.translation_context = p_translation_context;
update_configuration_warnings();
}

String Control::get_translation_context() const {
ERR_READ_THREAD_GUARD_V(String());
return data.translation_context;
}

String Control::get_tooltip(const Point2 &p_pos) const {
ERR_READ_THREAD_GUARD_V(String());
String ret;
Expand Down Expand Up @@ -3596,6 +3607,9 @@ void Control::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_tooltip_text"), &Control::get_tooltip_text);
ClassDB::bind_method(D_METHOD("get_tooltip", "at_position"), &Control::get_tooltip, DEFVAL(Point2()));

ClassDB::bind_method(D_METHOD("set_translation_context", "hint"), &Control::set_translation_context);
ClassDB::bind_method(D_METHOD("get_translation_context"), &Control::get_translation_context);

ClassDB::bind_method(D_METHOD("set_default_cursor_shape", "shape"), &Control::set_default_cursor_shape);
ClassDB::bind_method(D_METHOD("get_default_cursor_shape"), &Control::get_default_cursor_shape);
ClassDB::bind_method(D_METHOD("get_cursor_shape", "position"), &Control::get_cursor_shape, DEFVAL(Point2()));
Expand Down Expand Up @@ -3692,6 +3706,7 @@ void Control::_bind_methods() {

ADD_GROUP("Localization", "");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "localize_numeral_system"), "set_localize_numeral_system", "is_localizing_numeral_system");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "translation_context", PROPERTY_HINT_MULTILINE_TEXT), "set_translation_context", "get_translation_context");

#ifndef DISABLE_DEPRECATED
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_translate", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_auto_translate", "is_auto_translating");
Expand Down
3 changes: 3 additions & 0 deletions scene/gui/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ class Control : public CanvasItem {

String tooltip;
AutoTranslateMode tooltip_auto_translate_mode = AUTO_TRANSLATE_MODE_INHERIT;
String translation_context;

} data;

Expand Down Expand Up @@ -328,6 +329,7 @@ class Control : public CanvasItem {
static int root_layout_direction;

String get_tooltip_text() const;
String get_translation_context() const;

protected:
// Dynamic properties.
Expand Down Expand Up @@ -649,6 +651,7 @@ class Control : public CanvasItem {
// Extra properties.

void set_tooltip_text(const String &text);
void set_translation_context(const String &text);
virtual String get_tooltip(const Point2 &p_pos) const;
virtual Control *make_custom_tooltip(const String &p_text) const;

Expand Down

0 comments on commit 5d05874

Please sign in to comment.