Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[proof-of-concept] Add translation_context to Control nodes #4

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion editor/plugins/packed_scene_translation_parser_plugin.cpp
Original file line number Diff line number Diff line change
@@ -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.
@@ -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])) {
@@ -163,9 +176,10 @@ 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()) {
r_translations->push_back({ str_value });
r_translations->push_back({ str_value, translation_context });
}
}
}
22 changes: 22 additions & 0 deletions scene/gui/control.cpp
Original file line number Diff line number Diff line change
@@ -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;
@@ -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()));
@@ -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");
@@ -3862,3 +3877,10 @@ Control::~Control() {
data.theme_color_override.clear();
data.theme_constant_override.clear();
}

String Control::_get_translation_context_with_override(const StringName &p_context) const {
if (p_context == "") {
return this->data.translation_context;
}
return p_context;
}
6 changes: 6 additions & 0 deletions scene/gui/control.h
Original file line number Diff line number Diff line change
@@ -267,6 +267,7 @@ class Control : public CanvasItem {

String tooltip;
AutoTranslateMode tooltip_auto_translate_mode = AUTO_TRANSLATE_MODE_INHERIT;
String translation_context;

} data;

@@ -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.
@@ -340,6 +342,9 @@ class Control : public CanvasItem {
bool _property_can_revert(const StringName &p_name) const;
bool _property_get_revert(const StringName &p_name, Variant &r_property) const;

// Localization.
String _get_translation_context_with_override(const StringName &p_context) const override;

// Theming.

virtual void _update_theme_item_cache();
@@ -649,6 +654,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;

7 changes: 5 additions & 2 deletions scene/main/node.h
Original file line number Diff line number Diff line change
@@ -378,6 +378,9 @@ class Node : public Object {

void _validate_property(PropertyInfo &p_property) const;

// Localization.
virtual String _get_translation_context_with_override(const StringName &p_context) const { return p_context; }

protected:
virtual void input(const Ref<InputEvent> &p_event);
virtual void shortcut_input(const Ref<InputEvent> &p_key_event);
@@ -753,10 +756,10 @@ class Node : public Object {
virtual void set_translation_domain(const StringName &p_domain) override;
void set_translation_domain_inherited();

_FORCE_INLINE_ String atr(const String &p_message, const StringName &p_context = "") const { return can_auto_translate() ? tr(p_message, p_context) : p_message; }
_FORCE_INLINE_ String atr(const String &p_message, const StringName &p_context = "") const { return can_auto_translate() ? tr(p_message, _get_translation_context_with_override(p_context)) : p_message; }
_FORCE_INLINE_ String atr_n(const String &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const {
if (can_auto_translate()) {
return tr_n(p_message, p_message_plural, p_n, p_context);
return tr_n(p_message, p_message_plural, p_n, _get_translation_context_with_override(p_context));
}
return p_n == 1 ? p_message : String(p_message_plural);
}