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

Make is_layout_rtl() translation domain aware #97918

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
14 changes: 10 additions & 4 deletions core/string/translation_domain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,7 @@ void TranslationDomain::clear() {
}

StringName TranslationDomain::translate(const StringName &p_message, const StringName &p_context) const {
const String &locale = TranslationServer::get_singleton()->get_locale();
StringName res = get_message_from_translations(locale, p_message, p_context);
StringName res = get_message_from_translations(get_locale(), p_message, p_context);

const String &fallback = TranslationServer::get_singleton()->get_fallback_locale();
if (!res && fallback.length() >= 2) {
Expand All @@ -302,8 +301,7 @@ StringName TranslationDomain::translate(const StringName &p_message, const Strin
}

StringName TranslationDomain::translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
const String &locale = TranslationServer::get_singleton()->get_locale();
StringName res = get_message_from_translations(locale, p_message, p_message_plural, p_n, p_context);
StringName res = get_message_from_translations(get_locale(), p_message, p_message_plural, p_n, p_context);

const String &fallback = TranslationServer::get_singleton()->get_fallback_locale();
if (!res && fallback.length() >= 2) {
Expand All @@ -319,6 +317,14 @@ StringName TranslationDomain::translate_plural(const StringName &p_message, cons
return res;
}

String TranslationDomain::get_locale() const {
return locale_override.is_empty() ? TranslationServer::get_singleton()->get_locale() : locale_override;
}

void TranslationDomain::set_locale_override(const String &p_locale) {
locale_override = p_locale.is_empty() ? p_locale : TranslationServer::get_singleton()->standardize_locale(p_locale);
}

bool TranslationDomain::is_pseudolocalization_enabled() const {
return pseudolocalization.enabled;
}
Expand Down
4 changes: 4 additions & 0 deletions core/string/translation_domain.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class TranslationDomain : public RefCounted {
String suffix = "]";
};

String locale_override;
HashSet<Ref<Translation>> translations;
PseudolocalizationConfig pseudolocalization;

Expand All @@ -70,6 +71,9 @@ class TranslationDomain : public RefCounted {
StringName get_message_from_translations(const String &p_locale, const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const;
PackedStringArray get_loaded_locales() const;

String get_locale() const;
void set_locale_override(const String &p_locale);

public:
Ref<Translation> get_translation_object(const String &p_locale) const;

Expand Down
2 changes: 2 additions & 0 deletions core/string/translation_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ class TranslationServer : public Object {
public:
_FORCE_INLINE_ static TranslationServer *get_singleton() { return singleton; }

// These helpers are meant to be used by engine implementation only.
Ref<TranslationDomain> get_editor_domain() const { return editor_domain; }
Ref<TranslationDomain> get_main_domain() const { return main_domain; }

void set_enabled(bool p_enabled) { enabled = p_enabled; }
_FORCE_INLINE_ bool is_enabled() const { return enabled; }
Expand Down
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 @@ -440,6 +441,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 @@ -470,7 +472,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 @@ -557,8 +559,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 @@ -2804,7 +2805,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 @@ -8643,7 +8645,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,6 +81,7 @@ 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_QUANTITY: {
return TS->format_number(itos(p_value));
Expand All @@ -88,10 +90,10 @@ String EditorPerformanceProfiler::_create_label(float p_value, Performance::Moni
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 @@ -31,6 +31,7 @@
#include "editor_profiler.h"

#include "core/io/image.h"
#include "core/string/translation_server.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
#include "editor/gui/editor_run_bar.h"
Expand Down Expand Up @@ -120,19 +121,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 @@ -31,6 +31,7 @@
#include "editor_visual_profiler.h"

#include "core/io/image.h"
#include "core/string/translation_server.h"
#include "editor/editor_settings.h"
#include "editor/editor_string_names.h"
#include "editor/gui/editor_run_bar.h"
Expand Down Expand Up @@ -123,11 +124,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 @@ -850,7 +850,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
2 changes: 2 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6795,6 +6795,7 @@ EditorNode::EditorNode() {
DEV_ASSERT(!singleton);
singleton = this;

TranslationServer::get_singleton()->get_main_domain()->set_locale_override(TranslationServer::get_singleton()->get_fallback_locale());
set_translation_domain("godot.editor");

Resource::_get_local_scene_func = _resource_get_edited_scene;
Expand Down Expand Up @@ -7230,6 +7231,7 @@ EditorNode::EditorNode() {
editor_main_screen->set_v_size_flags(Control::SIZE_EXPAND_FILL);

scene_root = memnew(SubViewport);
scene_root->set_translation_domain(StringName());
scene_root->set_embedding_subwindows(true);
scene_root->set_disable_3d(true);
scene_root->set_disable_input(true);
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 @@ -1590,7 +1591,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 @@ -1605,8 +1607,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 @@ -351,7 +354,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 @@ -549,19 +553,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 @@ -166,7 +167,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 @@ -670,7 +672,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