Skip to content

Commit 2043023

Browse files
committed
Merge pull request #107999 from timothyqiu/translation-cleanup
Clean up editor translation related methods
2 parents 49219de + 3f03260 commit 2043023

File tree

12 files changed

+77
-163
lines changed

12 files changed

+77
-163
lines changed

core/string/translation_domain.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ StringName TranslationDomain::get_message_from_translations(const String &p_loca
202202
int best_score = 0;
203203

204204
for (const Ref<Translation> &E : translations) {
205-
ERR_CONTINUE(E.is_null());
206205
int score = TranslationServer::get_singleton()->compare_locales(p_locale, E->get_locale());
207206
if (score > 0 && score >= best_score) {
208207
const StringName r = E->get_message(p_message, p_context);
@@ -225,7 +224,6 @@ StringName TranslationDomain::get_message_from_translations(const String &p_loca
225224
int best_score = 0;
226225

227226
for (const Ref<Translation> &E : translations) {
228-
ERR_CONTINUE(E.is_null());
229227
int score = TranslationServer::get_singleton()->compare_locales(p_locale, E->get_locale());
230228
if (score > 0 && score >= best_score) {
231229
const StringName r = E->get_plural_message(p_message, p_message_plural, p_n, p_context);
@@ -246,7 +244,6 @@ StringName TranslationDomain::get_message_from_translations(const String &p_loca
246244
PackedStringArray TranslationDomain::get_loaded_locales() const {
247245
PackedStringArray locales;
248246
for (const Ref<Translation> &E : translations) {
249-
ERR_CONTINUE(E.is_null());
250247
const String &locale = E->get_locale();
251248
if (!locales.has(locale)) {
252249
locales.push_back(locale);
@@ -255,13 +252,20 @@ PackedStringArray TranslationDomain::get_loaded_locales() const {
255252
return locales;
256253
}
257254

255+
bool TranslationDomain::has_translation_for_locale(const String &p_locale) const {
256+
for (const Ref<Translation> &E : translations) {
257+
if (E->get_locale() == p_locale) {
258+
return true;
259+
}
260+
}
261+
return false;
262+
}
263+
258264
// Translation objects that could potentially be used for the given locale.
259265
HashSet<Ref<Translation>> TranslationDomain::get_potential_translations(const String &p_locale) const {
260266
HashSet<Ref<Translation>> res;
261267

262268
for (const Ref<Translation> &E : translations) {
263-
ERR_CONTINUE(E.is_null());
264-
265269
if (TranslationServer::get_singleton()->compare_locales(p_locale, E->get_locale()) > 0) {
266270
res.insert(E);
267271
}
@@ -274,8 +278,6 @@ Ref<Translation> TranslationDomain::get_translation_object(const String &p_local
274278
int best_score = 0;
275279

276280
for (const Ref<Translation> &E : translations) {
277-
ERR_CONTINUE(E.is_null());
278-
279281
int score = TranslationServer::get_singleton()->compare_locales(p_locale, E->get_locale());
280282
if (score > 0 && score >= best_score) {
281283
res = E;
@@ -289,6 +291,7 @@ Ref<Translation> TranslationDomain::get_translation_object(const String &p_local
289291
}
290292

291293
void TranslationDomain::add_translation(const Ref<Translation> &p_translation) {
294+
ERR_FAIL_COND_MSG(p_translation.is_null(), "Invalid translation provided.");
292295
translations.insert(p_translation);
293296
}
294297

core/string/translation_domain.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class TranslationDomain : public RefCounted {
7171
StringName get_message_from_translations(const String &p_locale, const StringName &p_message, const StringName &p_context) const;
7272
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;
7373
PackedStringArray get_loaded_locales() const;
74+
bool has_translation_for_locale(const String &p_locale) const;
7475
HashSet<Ref<Translation>> get_potential_translations(const String &p_locale) const;
7576

7677
public:

core/string/translation_server.cpp

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,7 @@ void TranslationServer::setup() {
494494
String TranslationServer::get_tool_locale() {
495495
#ifdef TOOLS_ENABLED
496496
if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) {
497-
const PackedStringArray &locales = editor_domain->get_loaded_locales();
498-
if (locales.has(locale)) {
497+
if (editor_domain->has_translation_for_locale(locale)) {
499498
return locale;
500499
}
501500
return "en";
@@ -512,26 +511,6 @@ String TranslationServer::get_tool_locale() {
512511
}
513512
}
514513

515-
StringName TranslationServer::tool_translate(const StringName &p_message, const StringName &p_context) const {
516-
return editor_domain->translate(p_message, p_context);
517-
}
518-
519-
StringName TranslationServer::tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
520-
return editor_domain->translate_plural(p_message, p_message_plural, p_n, p_context);
521-
}
522-
523-
StringName TranslationServer::property_translate(const StringName &p_message, const StringName &p_context) const {
524-
return property_domain->translate(p_message, p_context);
525-
}
526-
527-
StringName TranslationServer::doc_translate(const StringName &p_message, const StringName &p_context) const {
528-
return doc_domain->translate(p_message, p_context);
529-
}
530-
531-
StringName TranslationServer::doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
532-
return doc_domain->translate_plural(p_message, p_message_plural, p_n, p_context);
533-
}
534-
535514
bool TranslationServer::is_pseudolocalization_enabled() const {
536515
return main_domain->is_pseudolocalization_enabled();
537516
}
@@ -648,9 +627,14 @@ void TranslationServer::load_translations() {
648627

649628
TranslationServer::TranslationServer() {
650629
singleton = this;
630+
651631
main_domain.instantiate();
632+
633+
#ifdef TOOLS_ENABLED
652634
editor_domain = get_or_add_domain("godot.editor");
653635
property_domain = get_or_add_domain("godot.properties");
654636
doc_domain = get_or_add_domain("godot.documentation");
637+
#endif // TOOLS_ENABLED
638+
655639
init_locale_info();
656640
}

core/string/translation_server.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ class TranslationServer : public Object {
4040
String fallback;
4141

4242
Ref<TranslationDomain> main_domain;
43+
#ifdef TOOLS_ENABLED
4344
Ref<TranslationDomain> editor_domain;
4445
Ref<TranslationDomain> property_domain;
4546
Ref<TranslationDomain> doc_domain;
47+
#endif // TOOLS_ENABLED
4648
HashMap<StringName, Ref<TranslationDomain>> custom_domains;
4749

4850
mutable HashMap<String, int> locale_compare_cache;
@@ -95,8 +97,13 @@ class TranslationServer : public Object {
9597
public:
9698
_FORCE_INLINE_ static TranslationServer *get_singleton() { return singleton; }
9799

100+
// Built-in domain accessors. For engine code only, user code should use `get_or_add_domain()` instead.
98101
Ref<TranslationDomain> get_main_domain() const { return main_domain; }
102+
#ifdef TOOLS_ENABLED
99103
Ref<TranslationDomain> get_editor_domain() const { return editor_domain; }
104+
Ref<TranslationDomain> get_property_domain() const { return property_domain; }
105+
Ref<TranslationDomain> get_doc_domain() const { return doc_domain; }
106+
#endif // TOOLS_ENABLED
100107

101108
void set_locale(const String &p_locale);
102109
String get_locale() const;
@@ -135,11 +142,6 @@ class TranslationServer : public Object {
135142
int compare_locales(const String &p_locale_a, const String &p_locale_b) const;
136143

137144
String get_tool_locale();
138-
StringName tool_translate(const StringName &p_message, const StringName &p_context = "") const;
139-
StringName tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
140-
StringName property_translate(const StringName &p_message, const StringName &p_context = "") const;
141-
StringName doc_translate(const StringName &p_message, const StringName &p_context = "") const;
142-
StringName doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
143145

144146
bool has_domain(const StringName &p_domain) const;
145147
Ref<TranslationDomain> get_or_add_domain(const StringName &p_domain);

core/string/ustring.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5854,7 +5854,7 @@ Vector<uint8_t> String::to_multibyte_char_buffer(const String &p_encoding) const
58545854
*/
58555855
String TTR(const String &p_text, const String &p_context) {
58565856
if (TranslationServer::get_singleton()) {
5857-
return TranslationServer::get_singleton()->tool_translate(p_text, p_context);
5857+
return TranslationServer::get_singleton()->get_editor_domain()->translate(p_text, p_context);
58585858
}
58595859

58605860
return p_text;
@@ -5874,7 +5874,7 @@ String TTR(const String &p_text, const String &p_context) {
58745874
*/
58755875
String TTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context) {
58765876
if (TranslationServer::get_singleton()) {
5877-
return TranslationServer::get_singleton()->tool_translate_plural(p_text, p_text_plural, p_n, p_context);
5877+
return TranslationServer::get_singleton()->get_editor_domain()->translate_plural(p_text, p_text_plural, p_n, p_context);
58785878
}
58795879

58805880
// Return message based on English plural rule if translation is not possible.
@@ -5895,7 +5895,7 @@ String DTR(const String &p_text, const String &p_context) {
58955895
const String text = p_text.dedent().strip_edges();
58965896

58975897
if (TranslationServer::get_singleton()) {
5898-
return String(TranslationServer::get_singleton()->doc_translate(text, p_context)).replace("$DOCS_URL", GODOT_VERSION_DOCS_URL);
5898+
return String(TranslationServer::get_singleton()->get_doc_domain()->translate(text, p_context)).replace("$DOCS_URL", GODOT_VERSION_DOCS_URL);
58995899
}
59005900

59015901
return text.replace("$DOCS_URL", GODOT_VERSION_DOCS_URL);
@@ -5912,7 +5912,7 @@ String DTRN(const String &p_text, const String &p_text_plural, int p_n, const St
59125912
const String text_plural = p_text_plural.dedent().strip_edges();
59135913

59145914
if (TranslationServer::get_singleton()) {
5915-
return String(TranslationServer::get_singleton()->doc_translate_plural(text, text_plural, p_n, p_context)).replace("$DOCS_URL", GODOT_VERSION_DOCS_URL);
5915+
return String(TranslationServer::get_singleton()->get_doc_domain()->translate_plural(text, text_plural, p_n, p_context)).replace("$DOCS_URL", GODOT_VERSION_DOCS_URL);
59165916
}
59175917

59185918
// Return message based on English plural rule if translation is not possible.
@@ -5936,11 +5936,13 @@ String DTRN(const String &p_text, const String &p_text_plural, int p_n, const St
59365936
*/
59375937
String RTR(const String &p_text, const String &p_context) {
59385938
if (TranslationServer::get_singleton()) {
5939-
String rtr = TranslationServer::get_singleton()->tool_translate(p_text, p_context);
5940-
if (rtr.is_empty() || rtr == p_text) {
5941-
return TranslationServer::get_singleton()->translate(p_text, p_context);
5939+
#ifdef TOOLS_ENABLED
5940+
String rtr = TranslationServer::get_singleton()->get_editor_domain()->translate(p_text, p_context);
5941+
if (!rtr.is_empty() && rtr != p_text) {
5942+
return rtr;
59425943
}
5943-
return rtr;
5944+
#endif // TOOLS_ENABLED
5945+
return TranslationServer::get_singleton()->translate(p_text, p_context);
59445946
}
59455947

59465948
return p_text;
@@ -5959,11 +5961,13 @@ String RTR(const String &p_text, const String &p_context) {
59595961
*/
59605962
String RTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context) {
59615963
if (TranslationServer::get_singleton()) {
5962-
String rtr = TranslationServer::get_singleton()->tool_translate_plural(p_text, p_text_plural, p_n, p_context);
5963-
if (rtr.is_empty() || rtr == p_text || rtr == p_text_plural) {
5964-
return TranslationServer::get_singleton()->translate_plural(p_text, p_text_plural, p_n, p_context);
5964+
#ifdef TOOLS_ENABLED
5965+
String rtr = TranslationServer::get_singleton()->get_editor_domain()->translate_plural(p_text, p_text_plural, p_n, p_context);
5966+
if (!rtr.is_empty() && rtr != p_text && rtr != p_text_plural) {
5967+
return rtr;
59655968
}
5966-
return rtr;
5969+
#endif // TOOLS_ENABLED
5970+
return TranslationServer::get_singleton()->translate_plural(p_text, p_text_plural, p_n, p_context);
59675971
}
59685972

59695973
// Return message based on English plural rule if translation is not possible.

editor/doc/doc_tools.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static String _get_indent(const String &p_text) {
7171
static String _translate_doc_string(const String &p_text) {
7272
const String indent = _get_indent(p_text);
7373
const String message = p_text.dedent().strip_edges();
74-
const String translated = TranslationServer::get_singleton()->doc_translate(message, "");
74+
const String translated = TranslationServer::get_singleton()->get_doc_domain()->translate(message, StringName());
7575
// No need to restore stripped edges because they'll be stripped again later.
7676
return translated.indent(indent);
7777
}

editor/editor_builders.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def make_translations(target, source, env):
124124
file.write(f"""\
125125
#include "{target_h}"
126126
127-
const {category.capitalize()}TranslationList _{category}_translations[] = {{
127+
const EditorTranslationList _{category}_translations[] = {{
128128
""")
129129

130130
for x in xl_names:
@@ -137,12 +137,18 @@ def make_translations(target, source, env):
137137

138138
with methods.generated_wrapper(target_h) as file:
139139
file.write(f"""\
140-
struct {category.capitalize()}TranslationList {{
140+
141+
#ifndef EDITOR_TRANSLATION_LIST
142+
#define EDITOR_TRANSLATION_LIST
143+
144+
struct EditorTranslationList {{
141145
const char* lang;
142146
int comp_size;
143147
int uncomp_size;
144148
const unsigned char* data;
145149
}};
146150
147-
extern const {category.capitalize()}TranslationList _{category}_translations[];
151+
#endif // EDITOR_TRANSLATION_LIST
152+
153+
extern const EditorTranslationList _{category}_translations[];
148154
""")

editor/editor_node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ void EditorNode::_update_translations() {
581581
if (main->is_enabled()) {
582582
// Check for the exact locale.
583583
// `get_potential_translations("zh_CN")` could return translations for "zh".
584-
if (main->get_loaded_locales().has(main->get_locale_override())) {
584+
if (main->has_translation_for_locale(main->get_locale_override())) {
585585
// The set of translation resources for the current locale changed.
586586
const HashSet<Ref<Translation>> translations = main->get_potential_translations(main->get_locale_override());
587587
if (translations != tracked_translations) {

editor/inspector/editor_property_name_processor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ String EditorPropertyNameProcessor::process_name(const String &p_name, Style p_s
120120
case STYLE_LOCALIZED: {
121121
const String capitalized = _capitalize_name(p_name);
122122
if (TranslationServer::get_singleton()) {
123-
return TranslationServer::get_singleton()->property_translate(capitalized, _get_context(p_name, p_property, p_class));
123+
return TranslationServer::get_singleton()->get_property_domain()->translate(capitalized, _get_context(p_name, p_property, p_class));
124124
}
125125
return capitalized;
126126
} break;
@@ -130,7 +130,7 @@ String EditorPropertyNameProcessor::process_name(const String &p_name, Style p_s
130130

131131
String EditorPropertyNameProcessor::translate_group_name(const String &p_name) const {
132132
if (TranslationServer::get_singleton()) {
133-
return TranslationServer::get_singleton()->property_translate(p_name);
133+
return TranslationServer::get_singleton()->get_property_domain()->translate(p_name, StringName());
134134
}
135135
return p_name;
136136
}

editor/settings/editor_settings.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,16 +1337,10 @@ void EditorSettings::setup_language(bool p_initial_setup) {
13371337
TranslationServer::get_singleton()->set_locale(lang);
13381338
return; // Default, nothing to do.
13391339
}
1340-
// Load editor translation for configured/detected locale.
1341-
load_editor_translations(lang);
1342-
load_property_translations(lang);
13431340

1344-
// Load class reference translation.
1341+
load_editor_translations(lang);
13451342
load_doc_translations(lang);
13461343

1347-
// Load extractable translation for projects.
1348-
load_extractable_translations(lang);
1349-
13501344
TranslationServer::get_singleton()->set_locale(lang);
13511345
}
13521346

0 commit comments

Comments
 (0)