Godot Engine – Multi-platform 2D and 3D game engine
Add auto-translate mode to SpinBox's format
kobewiee4bb0e7f779
diff --git a/doc/classes/SpinBox.xml b/doc/classes/SpinBox.xmlindex c20f18e9cb7..8ab2c2150fc 100644--- a/doc/classes/SpinBox.xml+++ b/doc/classes/SpinBox.xml@@ -66,6 +66,12 @@- [code]"%d ms"[/code] displays as an integer with milliseconds unit (e.g. [code]420 ms[/code]).- [code]"%0.2f%%"[/code] displays as a percentage, with 2 decimal places of precision (e.g. [code]21.37%[/code]).</member>+ <member name="format_auto_translate_mode" type="int" setter="set_format_auto_translate_mode" getter="get_format_auto_translate_mode" enum="Node.AutoTranslateMode" default="2">+ Auto-translation mode for the [member format]. Useful when the format has a word that can be translated.+ </member>+ <member name="plural_format" type="String" setter="set_plural_format" getter="get_plural_format">+ Plural version of [member format]. Used only if [member format] is not empty and [member format_auto_translate_mode] is not disabled. See also [method Object.tr_n].+ </member><member name="prefix" type="String" setter="set_prefix" getter="get_prefix" default="""" deprecated="Use [member format] instead.">Adds the specified prefix string before the numerical value of the [SpinBox]. This is used only if [member format] is empty.</member>diff --git a/editor/scene/canvas_item_editor_plugin.cpp b/editor/scene/canvas_item_editor_plugin.cppindex c599d71c168..e614d8ab9c3 100644--- a/editor/scene/canvas_item_editor_plugin.cpp+++ b/editor/scene/canvas_item_editor_plugin.cpp@@ -186,7 +186,8 @@ public:primary_grid_step_x->set_step(1);primary_grid_step_x->set_max(SPIN_BOX_GRID_RANGE);primary_grid_step_x->set_allow_greater(true);- primary_grid_step_x->set_format("%s steps");+ primary_grid_step_x->set_format_with_plural("%d step", "%d steps"); // TTRN("%s step", "%s steps")+ primary_grid_step_x->set_format_auto_translate_mode(AUTO_TRANSLATE_MODE_INHERIT);primary_grid_step_x->set_h_size_flags(Control::SIZE_EXPAND_FILL);primary_grid_step_x->set_select_all_on_focus(true);primary_grid_step_x->set_accessibility_name(TTRC("X Primary Step"));@@ -197,7 +198,8 @@ public:primary_grid_step_y->set_step(1);primary_grid_step_y->set_max(SPIN_BOX_GRID_RANGE);primary_grid_step_y->set_allow_greater(true);- primary_grid_step_y->set_format(TTRC("%s steps")); // TODO: Add format auto-translation.+ primary_grid_step_y->set_format_with_plural("%d step", "%d steps"); // TTRN("%s step", "%s steps")+ primary_grid_step_y->set_format_auto_translate_mode(AUTO_TRANSLATE_MODE_INHERIT);primary_grid_step_y->set_h_size_flags(Control::SIZE_EXPAND_FILL);primary_grid_step_y->set_select_all_on_focus(true);primary_grid_step_y->set_accessibility_name(TTRC("Y Primary Step"));diff --git a/editor/translations/packed_scene_translation_parser_plugin.cpp b/editor/translations/packed_scene_translation_parser_plugin.cppindex 47c12f4043a..44fe4902aa0 100644--- a/editor/translations/packed_scene_translation_parser_plugin.cpp+++ b/editor/translations/packed_scene_translation_parser_plugin.cpp@@ -140,6 +140,47 @@ Error PackedSceneEditorTranslationParserPlugin::parse_file(const String &p_path,r_translations->push_back({ tooltip_text });}+ // Handle the `format` property of SpinBox.+ {+ String format_text;+ String plural_format_text;+ bool format_auto_translating = false;+ int found = 0;++ for (int j = 0; j < state->get_node_property_count(i); j++) {+ if (found == 3) {+ break;+ }+ const StringName property = state->get_node_property_name(i, j);+ if (property == SNAME("format")) {+ format_text = state->get_node_property_value(i, j);+ found++;+ continue;+ }+ if (property == SNAME("plural_format")) {+ plural_format_text = state->get_node_property_value(i, j);+ found++;+ continue;+ }+ if (property == SNAME("format_auto_translate_mode")) {+ int mode = state->get_node_property_value(i, j);+ switch (mode) {+ case Node::AUTO_TRANSLATE_MODE_ALWAYS: {+ format_auto_translating = true;+ } break;+ case Node::AUTO_TRANSLATE_MODE_INHERIT: {+ format_auto_translating = auto_translating;+ } break;+ }+ found++;+ continue;+ }+ }+ if (format_auto_translating && !format_text.is_empty()) {+ r_translations->push_back(PackedStringArray{ format_text, String(), plural_format_text });+ }+ }+// 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])) {diff --git a/scene/gui/spin_box.cpp b/scene/gui/spin_box.cppindex bd6966b977c..ed382f4634e 100644--- a/scene/gui/spin_box.cpp+++ b/scene/gui/spin_box.cpp@@ -100,7 +100,7 @@ void SpinBox::_update_text(bool p_only_update_if_value_changed) {if (!line_edit->is_editing() && !format.is_empty() && !use_default_format) {const Variant current_value = get_value();bool error = false;- const String text = format.sprintf(Span(¤t_value, 1), &error);+ const String text = _get_xl_format().sprintf(Span(¤t_value, 1), &error);if (error) {line_edit->set_text_with_selection(RTR("<Invalid>"));return;@@ -124,7 +124,7 @@ void SpinBox::_update_text(bool p_only_update_if_value_changed) {if (!format.is_empty()) {const Variant current_value = value;bool error = false;- value = format.sprintf(Span(¤t_value, 1), &error);+ value = _get_xl_format().sprintf(Span(¤t_value, 1), &error);if (error) {line_edit->set_text_with_selection(RTR("<Invalid>"));return;@@ -212,6 +212,21 @@ void SpinBox::_text_changed(const String &p_string) {}}+String SpinBox::_get_xl_format() const {+ bool translated = false;+ if (format_auto_translate_mode == AUTO_TRANSLATE_MODE_ALWAYS) {+ translated = true;+ } else if (format_auto_translate_mode == AUTO_TRANSLATE_MODE_INHERIT) {+ translated = can_auto_translate();+ }+ if (!translated) {+ return format;+ } else if (plural_format.is_empty()) {+ return tr(format);+ }+ return tr_n(format, plural_format, get_value());+}+LineEdit *SpinBox::get_line_edit() {return line_edit;}@@ -553,6 +568,9 @@ void SpinBox::_notification(int p_what) {} break;case NOTIFICATION_TRANSLATION_CHANGED: {+ if (format_auto_translate_mode != AUTO_TRANSLATE_MODE_DISABLED) {+ _update_text();+ }queue_redraw();} break;@@ -593,6 +611,36 @@ String SpinBox::get_format() const {return format;}+void SpinBox::set_plural_format(const String &p_format) {+ if (plural_format == p_format) {+ return;+ }+ plural_format = p_format;++ _update_text();+ update_configuration_warnings();+}++void SpinBox::set_format_with_plural(const String &p_format, const String &p_plural) {+ if (format == p_format && plural_format == p_format) {+ return;+ }+ format = p_format;+ plural_format = p_plural;++ _update_text();+ update_configuration_warnings();+}++void SpinBox::set_format_auto_translate_mode(AutoTranslateMode p_mode) {+ if (format_auto_translate_mode == p_mode) {+ return;+ }+ format_auto_translate_mode = p_mode;+ _update_text();+ notify_property_list_changed();+}+#ifndef DISABLE_DEPRECATEDvoid SpinBox::set_suffix(const String &p_suffix) {if (suffix == p_suffix) {@@ -692,6 +740,17 @@ PackedStringArray SpinBox::get_configuration_warnings() const {warnings.push_back(vformat(RTR("The format property is not valid: %s."), error_str));}}+ if (!plural_format.is_empty()) {+ bool error = false;+ const Variant test_value = 0.0;+ const String error_str = plural_format.sprintf(Span(&test_value, 1), &error);+ if (error) {+ warnings.push_back(vformat(RTR("The plural_format property is not valid: %s."), error_str));+ }+ if (format.is_empty()) {+ warnings.push_back(RTR("The format property is empty, while plural_format was specified. It will have no effect."));+ }+ }return warnings;}@@ -710,6 +769,8 @@ void SpinBox::_update_buttons_state_for_current_value() {void SpinBox::_validate_property(PropertyInfo &p_property) const {if (p_property.name == "exp_edit") {p_property.usage = PROPERTY_USAGE_NONE;+ } else if (format_auto_translate_mode == AUTO_TRANSLATE_MODE_DISABLED && p_property.name == "plural_format") {+ p_property.usage = PROPERTY_USAGE_NONE;}}@@ -718,6 +779,10 @@ void SpinBox::_bind_methods() {ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &SpinBox::get_horizontal_alignment);ClassDB::bind_method(D_METHOD("set_format", "format"), &SpinBox::set_format);ClassDB::bind_method(D_METHOD("get_format"), &SpinBox::get_format);+ ClassDB::bind_method(D_METHOD("set_plural_format", "format"), &SpinBox::set_plural_format);+ ClassDB::bind_method(D_METHOD("get_plural_format"), &SpinBox::get_plural_format);+ ClassDB::bind_method(D_METHOD("set_format_auto_translate_mode", "mode"), &SpinBox::set_format_auto_translate_mode);+ ClassDB::bind_method(D_METHOD("get_format_auto_translate_mode"), &SpinBox::get_format_auto_translate_mode);#ifndef DISABLE_DEPRECATEDClassDB::bind_method(D_METHOD("set_suffix", "suffix"), &SpinBox::set_suffix);ClassDB::bind_method(D_METHOD("get_suffix"), &SpinBox::get_suffix);@@ -740,14 +805,18 @@ void SpinBox::_bind_methods() {ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_on_text_changed"), "set_update_on_text_changed", "get_update_on_text_changed");+ ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "custom_arrow_step", PROPERTY_HINT_RANGE, "0,10000,0.0001,or_greater"), "set_custom_arrow_step", "get_custom_arrow_step");+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "custom_arrow_round"), "set_custom_arrow_round", "is_custom_arrow_rounding");+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "select_all_on_focus"), "set_select_all_on_focus", "is_select_all_on_focus");++ ADD_GROUP("Format", "");ADD_PROPERTY(PropertyInfo(Variant::STRING, "format", PROPERTY_HINT_PLACEHOLDER_TEXT, "%s"), "set_format", "get_format");+ ADD_PROPERTY(PropertyInfo(Variant::STRING, "plural_format", PROPERTY_HINT_PLACEHOLDER_TEXT, "%s"), "set_plural_format", "get_plural_format");+ ADD_PROPERTY(PropertyInfo(Variant::INT, "format_auto_translate_mode", PROPERTY_HINT_ENUM, "Inherit,Always,Disabled"), "set_format_auto_translate_mode", "get_format_auto_translate_mode");#ifndef DISABLE_DEPRECATEDADD_PROPERTY(PropertyInfo(Variant::STRING, "prefix"), "set_prefix", "get_prefix");ADD_PROPERTY(PropertyInfo(Variant::STRING, "suffix"), "set_suffix", "get_suffix");#endif- ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "custom_arrow_step", PROPERTY_HINT_RANGE, "0,10000,0.0001,or_greater"), "set_custom_arrow_step", "get_custom_arrow_step");- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "custom_arrow_round"), "set_custom_arrow_round", "is_custom_arrow_rounding");- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "select_all_on_focus"), "set_select_all_on_focus", "is_select_all_on_focus");BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SpinBox, buttons_vertical_separation);BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SpinBox, field_and_buttons_separation);diff --git a/scene/gui/spin_box.h b/scene/gui/spin_box.hindex 7b493a09ae9..e4ee32da8bd 100644--- a/scene/gui/spin_box.h+++ b/scene/gui/spin_box.h@@ -77,6 +77,10 @@ class SpinBox : public Range {void _text_changed(const String &p_string);String format;+ String plural_format;+ AutoTranslateMode format_auto_translate_mode = AUTO_TRANSLATE_MODE_DISABLED;+ String _get_xl_format() const;+bool use_default_format = false;#ifndef DISABLE_DEPRECATEDString prefix;@@ -177,6 +181,14 @@ public:void set_format(const String &p_format);String get_format() const;+ void set_plural_format(const String &p_format);+ String get_plural_format() const { return plural_format; }++ void set_format_with_plural(const String &p_format, const String &p_plural);++ void set_format_auto_translate_mode(AutoTranslateMode p_mode);+ AutoTranslateMode get_format_auto_translate_mode() const { return format_auto_translate_mode; }+#ifndef DISABLE_DEPRECATEDvoid set_suffix(const String &p_suffix);String get_suffix() const;