Skip to content

Commit

Permalink
Avoid using get_tool_locale() when possible
Browse files Browse the repository at this point in the history
`TextServer` has a bunch of APIs that uses `get_tool_locale()` when an
empty language parameter is passed. This behavior can't be changed, so
the best I can do is to avoid passing empty strings for these
parameters.
  • Loading branch information
timothyqiu committed Nov 8, 2024
1 parent 5af9efc commit d0cf20c
Show file tree
Hide file tree
Showing 28 changed files with 167 additions and 99 deletions.
8 changes: 5 additions & 3 deletions editor/animation_bezier_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "animation_bezier_editor.h"

#include "core/string/translation_server.h"
#include "editor/editor_node.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
Expand Down Expand Up @@ -439,6 +440,7 @@ void AnimationBezierTrackEdit::_notification(int p_what) {
}

const Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();

// Guides.
{
Expand Down Expand Up @@ -469,7 +471,7 @@ void AnimationBezierTrackEdit::_notification(int p_what) {
draw_line(Point2(limit, i), Point2(right_limit, i), lc, Math::round(EDSCALE));
Color c = color;
c.a *= 0.5;
draw_string(font, Point2(limit + 8, i - 2), TS->format_number(rtos(Math::snapped((iv + 1) * scale, step))), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, c);
draw_string(font, Point2(limit + 8, i - 2), TS->format_number(rtos(Math::snapped((iv + 1) * scale, step)), lang), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, c);
}

first = false;
Expand Down Expand Up @@ -556,8 +558,8 @@ void AnimationBezierTrackEdit::_notification(int p_what) {
ep.point_rect.size = bezier_icon->get_size();
if (selection.has(IntPair(i, j))) {
draw_texture(selected_icon, ep.point_rect.position);
draw_string(font, ep.point_rect.position + Vector2(8, -font->get_height(font_size) - 8), TTR("Time:") + " " + TS->format_number(rtos(Math::snapped(offset, 0.0001))), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, accent);
draw_string(font, ep.point_rect.position + Vector2(8, -8), TTR("Value:") + " " + TS->format_number(rtos(Math::snapped(value, 0.001))), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, accent);
draw_string(font, ep.point_rect.position + Vector2(8, -font->get_height(font_size) - 8), TTR("Time:") + " " + TS->format_number(rtos(Math::snapped(offset, 0.0001)), lang), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, accent);
draw_string(font, ep.point_rect.position + Vector2(8, -8), TTR("Value:") + " " + TS->format_number(rtos(Math::snapped(value, 0.001)), lang), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, accent);
} else {
Color track_color = Color(1, 1, 1, 1);
if (i != selected_track) {
Expand Down
7 changes: 5 additions & 2 deletions editor/animation_track_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "animation_track_editor_plugins.h"
#include "core/error/error_macros.h"
#include "core/input/input.h"
#include "core/string/translation_server.h"
#include "editor/animation_bezier_editor.h"
#include "editor/editor_node.h"
#include "editor/editor_settings.h"
Expand Down Expand Up @@ -2787,7 +2788,8 @@ String AnimationTrackEdit::get_tooltip(const Point2 &p_pos) const {
}

if (key_idx != -1) {
String text = TTR("Time (s):") + " " + TS->format_number(rtos(Math::snapped(animation->track_get_key_time(track, key_idx), SECOND_DECIMAL))) + "\n";
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();
String text = TTR("Time (s):") + " " + TS->format_number(rtos(Math::snapped(animation->track_get_key_time(track, key_idx), SECOND_DECIMAL)), lang) + "\n";
switch (animation->track_get_type(track)) {
case Animation::TYPE_POSITION_3D: {
Vector3 t = animation->track_get_key_value(track, key_idx);
Expand Down Expand Up @@ -8562,7 +8564,8 @@ String AnimationMarkerEdit::get_tooltip(const Point2 &p_pos) const {

if (key_idx != -1) {
String name = names[key_idx];
String text = TTR("Time (s):") + " " + TS->format_number(rtos(Math::snapped(animation->get_marker_time(name), 0.0001))) + "\n";
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();
String text = TTR("Time (s):") + " " + TS->format_number(rtos(Math::snapped(animation->get_marker_time(name), 0.0001)), lang) + "\n";
text += TTR("Marker:") + " " + name + "\n";
return text;
}
Expand Down
6 changes: 4 additions & 2 deletions editor/debugger/editor_performance_profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "editor_performance_profiler.h"

#include "core/string/translation_server.h"
#include "editor/editor_property_name_processor.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
Expand Down Expand Up @@ -80,15 +81,16 @@ void EditorPerformanceProfiler::Monitor::reset() {
}

String EditorPerformanceProfiler::_create_label(float p_value, Performance::MonitorType p_type) {
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();
switch (p_type) {
case Performance::MONITOR_TYPE_MEMORY: {
return String::humanize_size(p_value);
}
case Performance::MONITOR_TYPE_TIME: {
return TS->format_number(rtos(p_value * 1000).pad_decimals(2)) + " " + TTR("ms");
return TS->format_number(rtos(p_value * 1000).pad_decimals(2), lang) + " " + TTR("ms");
}
default: {
return TS->format_number(rtos(p_value));
return TS->format_number(rtos(p_value), lang);
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions editor/debugger/editor_profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "core/io/image.h"
#include "core/os/os.h"
#include "core/string/translation_server.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
#include "editor/themes/editor_scale.h"
Expand Down Expand Up @@ -121,19 +122,21 @@ static String _get_percent_txt(float p_value, float p_total) {
p_total = 0.00001;
}

return TS->format_number(String::num((p_value / p_total) * 100, 1)) + TS->percent_sign();
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();
return TS->format_number(String::num((p_value / p_total) * 100, 1), lang) + TS->percent_sign(lang);
}

String EditorProfiler::_get_time_as_text(const Metric &m, float p_time, int p_calls) {
const int dmode = display_mode->get_selected();
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();

if (dmode == DISPLAY_FRAME_TIME) {
return TS->format_number(rtos(p_time * 1000).pad_decimals(2)) + " " + TTR("ms");
return TS->format_number(rtos(p_time * 1000).pad_decimals(2), lang) + " " + TTR("ms");
} else if (dmode == DISPLAY_AVERAGE_TIME) {
if (p_calls == 0) {
return TS->format_number("0.00") + " " + TTR("ms");
return TS->format_number("0.00", lang) + " " + TTR("ms");
} else {
return TS->format_number(rtos((p_time / p_calls) * 1000).pad_decimals(2)) + " " + TTR("ms");
return TS->format_number(rtos((p_time / p_calls) * 1000).pad_decimals(2), lang) + " " + TTR("ms");
}
} else if (dmode == DISPLAY_FRAME_PERCENT) {
return _get_percent_txt(p_time, m.frame_time);
Expand Down
6 changes: 4 additions & 2 deletions editor/debugger/editor_visual_profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "core/io/image.h"
#include "core/os/os.h"
#include "core/string/translation_server.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
#include "editor/themes/editor_scale.h"
Expand Down Expand Up @@ -117,11 +118,12 @@ void EditorVisualProfiler::clear() {

String EditorVisualProfiler::_get_time_as_text(float p_time) {
int dmode = display_mode->get_selected();
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();

if (dmode == DISPLAY_FRAME_TIME) {
return TS->format_number(String::num(p_time, 2)) + " " + TTR("ms");
return TS->format_number(String::num(p_time, 2), lang) + " " + TTR("ms");
} else if (dmode == DISPLAY_FRAME_PERCENT) {
return TS->format_number(String::num(p_time * 100 / graph_limit, 2)) + " " + TS->percent_sign();
return TS->format_number(String::num(p_time * 100 / graph_limit, 2), lang) + " " + TS->percent_sign(lang);
}

return "err";
Expand Down
5 changes: 3 additions & 2 deletions editor/debugger/script_editor_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "core/config/project_settings.h"
#include "core/debugger/debugger_marshalls.h"
#include "core/debugger/remote_debugger.h"
#include "core/io/marshalls.h"
#include "core/string/translation_server.h"
#include "core/string/ustring.h"
#include "core/version.h"
#include "editor/debugger/debug_adapter/debug_adapter_protocol.h"
Expand Down Expand Up @@ -849,7 +849,8 @@ void ScriptEditorDebugger::_set_reason_text(const String &p_reason, MessageType
}
reason->set_text(p_reason);

const PackedInt32Array boundaries = TS->string_get_word_breaks(p_reason, "", 80);
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();
const PackedInt32Array boundaries = TS->string_get_word_breaks(p_reason, lang, 80);
PackedStringArray lines;
for (int i = 0; i < boundaries.size(); i += 2) {
const int start = boundaries[i];
Expand Down
7 changes: 5 additions & 2 deletions editor/editor_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "editor_properties.h"

#include "core/config/project_settings.h"
#include "core/string/translation_server.h"
#include "editor/create_dialog.h"
#include "editor/editor_node.h"
#include "editor/editor_properties_array_dict.h"
Expand Down Expand Up @@ -1564,7 +1565,8 @@ void EditorPropertyEasing::_draw_easing() {
} else {
decimals = 1;
}
f->draw_string(ci, Point2(10, 10 + f->get_ascent(font_size)), TS->format_number(rtos(exp).pad_decimals(decimals)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color);
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();
f->draw_string(ci, Point2(10, 10 + f->get_ascent(font_size)), TS->format_number(rtos(exp).pad_decimals(decimals), lang), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color);
}

void EditorPropertyEasing::update_property() {
Expand All @@ -1579,8 +1581,9 @@ void EditorPropertyEasing::_set_preset(int p_preset) {
}

void EditorPropertyEasing::_setup_spin() {
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();
spin->setup_and_show();
spin->get_line_edit()->set_text(TS->format_number(rtos(get_edited_property_value())));
spin->get_line_edit()->set_text(TS->format_number(rtos(get_edited_property_value()), lang));
spin->show();
}

Expand Down
18 changes: 12 additions & 6 deletions editor/gui/editor_spin_slider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,23 @@
#include "core/input/input.h"
#include "core/math/expression.h"
#include "core/os/keyboard.h"
#include "core/string/translation_server.h"
#include "editor/editor_settings.h"
#include "editor/themes/editor_scale.h"
#include "scene/theme/theme_db.h"

String EditorSpinSlider::get_tooltip(const Point2 &p_pos) const {
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();
if (!read_only && grabber->is_visible()) {
Key key = (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) ? Key::META : Key::CTRL;
return TS->format_number(rtos(get_value())) + "\n\n" + vformat(TTR("Hold %s to round to integers.\nHold Shift for more precise changes."), find_keycode_name(key));
return TS->format_number(rtos(get_value()), lang) + "\n\n" + vformat(TTR("Hold %s to round to integers.\nHold Shift for more precise changes."), find_keycode_name(key));
}
return TS->format_number(rtos(get_value()));
return TS->format_number(rtos(get_value()), lang);
}

String EditorSpinSlider::get_text_value() const {
return TS->format_number(String::num(get_value(), Math::range_step_decimals(get_step())));
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();
return TS->format_number(String::num(get_value(), Math::range_step_decimals(get_step())), lang);
}

void EditorSpinSlider::gui_input(const Ref<InputEvent> &p_event) {
Expand Down Expand Up @@ -350,7 +353,8 @@ void EditorSpinSlider::_draw_spin_slider() {

int suffix_start = numstr.length();
RID num_rid = TS->create_shaped_text();
TS->shaped_text_add_string(num_rid, numstr + U"\u2009" + suffix, font->get_rids(), font_size, font->get_opentype_features());
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();
TS->shaped_text_add_string(num_rid, numstr + U"\u2009" + suffix, font->get_rids(), font_size, font->get_opentype_features(), lang);

float text_start = rtl ? Math::round(sb->get_offset().x) : Math::round(sb->get_offset().x + label_width + sep);
Vector2 text_ofs = rtl ? Vector2(text_start + (number_width - TS->shaped_text_get_width(num_rid)), vofs) : Vector2(text_start, vofs);
Expand Down Expand Up @@ -548,19 +552,21 @@ String EditorSpinSlider::get_suffix() const {
}

void EditorSpinSlider::_evaluate_input_text() {
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();

Ref<Expression> expr;
expr.instantiate();

// Convert commas ',' to dots '.' for French/German etc. keyboard layouts.
String text = value_input->get_text().replace(",", ".");
text = text.replace(";", ",");
text = TS->parse_number(text);
text = TS->parse_number(text, lang);

Error err = expr->parse(text);
if (err != OK) {
// If the expression failed try without converting commas to dots - they might have been for parameter separation.
text = value_input->get_text();
text = TS->parse_number(text);
text = TS->parse_number(text, lang);

err = expr->parse(text);
if (err != OK) {
Expand Down
8 changes: 5 additions & 3 deletions editor/gui/editor_zoom_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,24 @@
#include "editor_zoom_widget.h"

#include "core/os/keyboard.h"
#include "core/string/translation_server.h"
#include "editor/editor_settings.h"
#include "editor/themes/editor_scale.h"

void EditorZoomWidget::_update_zoom_label() {
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();
String zoom_text;
// The zoom level displayed is relative to the editor scale
// (like in most image editors). Its lower bound is clamped to 1 as some people
// lower the editor scale to increase the available real estate,
// even if their display doesn't have a particularly low DPI.
if (zoom >= 10) {
zoom_text = TS->format_number(rtos(Math::round((zoom / MAX(1, EDSCALE)) * 100)));
zoom_text = TS->format_number(rtos(Math::round((zoom / MAX(1, EDSCALE)) * 100)), lang);
} else {
// 2 decimal places if the zoom is below 10%, 1 decimal place if it's below 1000%.
zoom_text = TS->format_number(rtos(Math::snapped((zoom / MAX(1, EDSCALE)) * 100, (zoom >= 0.1) ? 0.1 : 0.01)));
zoom_text = TS->format_number(rtos(Math::snapped((zoom / MAX(1, EDSCALE)) * 100, (zoom >= 0.1) ? 0.1 : 0.01)), lang);
}
zoom_text += " " + TS->percent_sign();
zoom_text += " " + TS->percent_sign(lang);
zoom_reset->set_text(zoom_text);
}

Expand Down
7 changes: 5 additions & 2 deletions editor/gui/scene_tree_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "core/config/project_settings.h"
#include "core/object/script_language.h"
#include "core/string/translation_server.h"
#include "editor/editor_dock_manager.h"
#include "editor/editor_file_system.h"
#include "editor/editor_node.h"
Expand Down Expand Up @@ -144,7 +145,8 @@ void SceneTreeEditor::_cell_button_pressed(Object *p_item, int p_column, int p_i

// Limit the line width while keeping some padding.
// It is not efficient, but it does not have to be.
const PackedInt32Array boundaries = TS->string_get_word_breaks(all_warnings, "", 80);
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();
const PackedInt32Array boundaries = TS->string_get_word_breaks(all_warnings, lang, 80);
PackedStringArray lines;
for (int i = 0; i < boundaries.size(); i += 2) {
const int start = boundaries[i];
Expand Down Expand Up @@ -531,7 +533,8 @@ void SceneTreeEditor::_update_node_tooltip(Node *p_node, TreeItem *p_item) {
tooltip += "\n" + TTR("Type:") + " " + (custom_type != StringName() ? String(custom_type) : p_node->get_class());

if (!p_node->get_editor_description().is_empty()) {
const PackedInt32Array boundaries = TS->string_get_word_breaks(p_node->get_editor_description(), "", 80);
const String &lang = TranslationServer::get_singleton()->get_editor_domain()->get_locale();
const PackedInt32Array boundaries = TS->string_get_word_breaks(p_node->get_editor_description(), lang, 80);
tooltip += "\n";

for (int i = 0; i < boundaries.size(); i += 2) {
Expand Down
Loading

0 comments on commit d0cf20c

Please sign in to comment.