Skip to content

Commit 818acb4

Browse files
committed
Make editor use translation domains
How editor plugins use this feature: 1. Pick a unique translation domain name. 2. `_enter_tree()`: load translations into that translation domain. 3. Call `set_translation_domain()` for its root UI node. 4. `_exit_tree()`: remove that translation domain. Plugins can also set the translation domain to `godot.editor` for nested nodes that should use editor translations. `EditorFileDialog` automatically does this.
1 parent c5d147b commit 818acb4

File tree

10 files changed

+38
-125
lines changed

10 files changed

+38
-125
lines changed

core/object/object.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,15 +1540,6 @@ String Object::tr(const StringName &p_message, const StringName &p_context) cons
15401540
return p_message;
15411541
}
15421542

1543-
if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) {
1544-
String tr_msg = TranslationServer::get_singleton()->extractable_translate(p_message, p_context);
1545-
if (!tr_msg.is_empty() && tr_msg != p_message) {
1546-
return tr_msg;
1547-
}
1548-
1549-
return TranslationServer::get_singleton()->tool_translate(p_message, p_context);
1550-
}
1551-
15521543
const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain(get_translation_domain());
15531544
return domain->translate(p_message, p_context);
15541545
}
@@ -1562,15 +1553,6 @@ String Object::tr_n(const StringName &p_message, const StringName &p_message_plu
15621553
return p_message_plural;
15631554
}
15641555

1565-
if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) {
1566-
String tr_msg = TranslationServer::get_singleton()->extractable_translate_plural(p_message, p_message_plural, p_n, p_context);
1567-
if (!tr_msg.is_empty() && tr_msg != p_message && tr_msg != p_message_plural) {
1568-
return tr_msg;
1569-
}
1570-
1571-
return TranslationServer::get_singleton()->tool_translate_plural(p_message, p_message_plural, p_n, p_context);
1572-
}
1573-
15741556
const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain(get_translation_domain());
15751557
return domain->translate_plural(p_message, p_message_plural, p_n, p_context);
15761558
}

core/string/translation_server.cpp

Lines changed: 11 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -525,22 +525,14 @@ void TranslationServer::setup() {
525525
#endif
526526
}
527527

528-
void TranslationServer::set_tool_translation(const Ref<Translation> &p_translation) {
529-
tool_translation = p_translation;
530-
}
531-
532-
Ref<Translation> TranslationServer::get_tool_translation() const {
533-
return tool_translation;
534-
}
535-
536528
String TranslationServer::get_tool_locale() {
537529
#ifdef TOOLS_ENABLED
538530
if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) {
539-
if (TranslationServer::get_singleton()->get_tool_translation().is_valid()) {
540-
return tool_translation->get_locale();
541-
} else {
531+
const PackedStringArray &locales = editor_domain->get_loaded_locales();
532+
if (locales.is_empty()) {
542533
return "en";
543534
}
535+
return locales[0];
544536
} else {
545537
#else
546538
{
@@ -555,97 +547,23 @@ String TranslationServer::get_tool_locale() {
555547
}
556548

557549
StringName TranslationServer::tool_translate(const StringName &p_message, const StringName &p_context) const {
558-
if (tool_translation.is_valid()) {
559-
StringName r = tool_translation->get_message(p_message, p_context);
560-
if (r) {
561-
return r;
562-
}
563-
}
564-
return p_message;
550+
return editor_domain->translate(p_message, p_context);
565551
}
566552

567553
StringName TranslationServer::tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
568-
if (tool_translation.is_valid()) {
569-
StringName r = tool_translation->get_plural_message(p_message, p_message_plural, p_n, p_context);
570-
if (r) {
571-
return r;
572-
}
573-
}
574-
575-
if (p_n == 1) {
576-
return p_message;
577-
}
578-
return p_message_plural;
579-
}
580-
581-
void TranslationServer::set_property_translation(const Ref<Translation> &p_translation) {
582-
property_translation = p_translation;
554+
return editor_domain->translate_plural(p_message, p_message_plural, p_n, p_context);
583555
}
584556

585557
StringName TranslationServer::property_translate(const StringName &p_message, const StringName &p_context) const {
586-
if (property_translation.is_valid()) {
587-
StringName r = property_translation->get_message(p_message, p_context);
588-
if (r) {
589-
return r;
590-
}
591-
}
592-
return p_message;
593-
}
594-
595-
void TranslationServer::set_doc_translation(const Ref<Translation> &p_translation) {
596-
doc_translation = p_translation;
558+
return property_domain->translate(p_message, p_context);
597559
}
598560

599561
StringName TranslationServer::doc_translate(const StringName &p_message, const StringName &p_context) const {
600-
if (doc_translation.is_valid()) {
601-
StringName r = doc_translation->get_message(p_message, p_context);
602-
if (r) {
603-
return r;
604-
}
605-
}
606-
return p_message;
562+
return doc_domain->translate(p_message, p_context);
607563
}
608564

609565
StringName TranslationServer::doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
610-
if (doc_translation.is_valid()) {
611-
StringName r = doc_translation->get_plural_message(p_message, p_message_plural, p_n, p_context);
612-
if (r) {
613-
return r;
614-
}
615-
}
616-
617-
if (p_n == 1) {
618-
return p_message;
619-
}
620-
return p_message_plural;
621-
}
622-
623-
void TranslationServer::set_extractable_translation(const Ref<Translation> &p_translation) {
624-
extractable_translation = p_translation;
625-
}
626-
627-
StringName TranslationServer::extractable_translate(const StringName &p_message, const StringName &p_context) const {
628-
if (extractable_translation.is_valid()) {
629-
StringName r = extractable_translation->get_message(p_message, p_context);
630-
if (r) {
631-
return r;
632-
}
633-
}
634-
return p_message;
635-
}
636-
637-
StringName TranslationServer::extractable_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
638-
if (extractable_translation.is_valid()) {
639-
StringName r = extractable_translation->get_plural_message(p_message, p_message_plural, p_n, p_context);
640-
if (r) {
641-
return r;
642-
}
643-
}
644-
645-
if (p_n == 1) {
646-
return p_message;
647-
}
648-
return p_message_plural;
566+
return doc_domain->translate_plural(p_message, p_message_plural, p_n, p_context);
649567
}
650568

651569
bool TranslationServer::is_pseudolocalization_enabled() const {
@@ -890,5 +808,8 @@ void TranslationServer::load_translations() {
890808
TranslationServer::TranslationServer() {
891809
singleton = this;
892810
main_domain.instantiate();
811+
editor_domain = get_or_add_domain("godot.editor");
812+
property_domain = get_or_add_domain("godot.properties");
813+
doc_domain = get_or_add_domain("godot.documentation");
893814
init_locale_info();
894815
}

core/string/translation_server.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,11 @@ class TranslationServer : public Object {
4141
String fallback;
4242

4343
Ref<TranslationDomain> main_domain;
44+
Ref<TranslationDomain> editor_domain;
45+
Ref<TranslationDomain> property_domain;
46+
Ref<TranslationDomain> doc_domain;
4447
HashMap<StringName, Ref<TranslationDomain>> custom_domains;
4548

46-
Ref<Translation> tool_translation;
47-
Ref<Translation> property_translation;
48-
Ref<Translation> doc_translation;
49-
Ref<Translation> extractable_translation;
50-
5149
bool enabled = true;
5250

5351
bool pseudolocalization_enabled = false;
@@ -133,18 +131,11 @@ class TranslationServer : public Object {
133131
int compare_locales(const String &p_locale_a, const String &p_locale_b) const;
134132

135133
String get_tool_locale();
136-
void set_tool_translation(const Ref<Translation> &p_translation);
137-
Ref<Translation> get_tool_translation() const;
138134
StringName tool_translate(const StringName &p_message, const StringName &p_context = "") const;
139135
StringName tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
140-
void set_property_translation(const Ref<Translation> &p_translation);
141136
StringName property_translate(const StringName &p_message, const StringName &p_context = "") const;
142-
void set_doc_translation(const Ref<Translation> &p_translation);
143137
StringName doc_translate(const StringName &p_message, const StringName &p_context = "") const;
144138
StringName doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
145-
void set_extractable_translation(const Ref<Translation> &p_translation);
146-
StringName extractable_translate(const StringName &p_message, const StringName &p_context = "") const;
147-
StringName extractable_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
148139

149140
bool has_domain(const StringName &p_domain) const;
150141
Ref<TranslationDomain> get_or_add_domain(const StringName &p_domain);

doc/classes/TranslationDomain.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
</brief_description>
66
<description>
77
[TranslationDomain] is a self-contained collection of [Translation] resources. Translations can be added to or removed from it.
8+
If you're working with the main translation domain, it is more convenient to use the wrap methods on [TranslationServer].
89
</description>
910
<tutorials>
1011
</tutorials>

doc/classes/TranslationServer.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</brief_description>
66
<description>
77
The translation server is the API backend that manages all language translations.
8-
Translations are stored in [TranslationDomain]s, which can be accessed by name. The most commonly used translation domain is the main translation domain, which always exists and can be accessed using an empty [StringName]. The translation server provides wrapper methods for accessing the master translation domain directly, without having to fetch the main translation domain first.
8+
Translations are stored in [TranslationDomain]s, which can be accessed by name. The most commonly used translation domain is the main translation domain. It always exists and can be accessed using an empty [StringName]. The translation server provides wrapper methods for accessing the main translation domain directly, without having to fetch the translation domain first. Custom translation domains are mainly for advanced usages like editor plugins. Names starting with [code]godot.[/code] are reserved for engine internals.
99
</description>
1010
<tutorials>
1111
<link title="Internationalizing games">$DOCS_URL/tutorials/i18n/internationalizing_games.html</link>

editor/editor_node.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6557,6 +6557,8 @@ EditorNode::EditorNode() {
65576557
DEV_ASSERT(!singleton);
65586558
singleton = this;
65596559

6560+
set_translation_domain("godot.editor");
6561+
65606562
Resource::_get_local_scene_func = _resource_get_edited_scene;
65616563

65626564
{

editor/editor_settings.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,8 @@ void EditorSettings::create() {
12101210

12111211
void EditorSettings::setup_language() {
12121212
String lang = get("interface/editor/editor_language");
1213+
TranslationServer::get_singleton()->set_locale(lang);
1214+
12131215
if (lang == "en") {
12141216
return; // Default, nothing to do.
12151217
}

editor/editor_translation.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Vector<String> get_editor_locales() {
5454
}
5555

5656
void load_editor_translations(const String &p_locale) {
57+
const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain("godot.editor");
58+
5759
EditorTranslationList *etl = _editor_translations;
5860
while (etl->data) {
5961
if (etl->lang == p_locale) {
@@ -70,7 +72,7 @@ void load_editor_translations(const String &p_locale) {
7072

7173
if (tr.is_valid()) {
7274
tr->set_locale(etl->lang);
73-
TranslationServer::get_singleton()->set_tool_translation(tr);
75+
domain->add_translation(tr);
7476
break;
7577
}
7678
}
@@ -80,6 +82,8 @@ void load_editor_translations(const String &p_locale) {
8082
}
8183

8284
void load_property_translations(const String &p_locale) {
85+
const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain("godot.properties");
86+
8387
PropertyTranslationList *etl = _property_translations;
8488
while (etl->data) {
8589
if (etl->lang == p_locale) {
@@ -96,7 +100,7 @@ void load_property_translations(const String &p_locale) {
96100

97101
if (tr.is_valid()) {
98102
tr->set_locale(etl->lang);
99-
TranslationServer::get_singleton()->set_property_translation(tr);
103+
domain->add_translation(tr);
100104
break;
101105
}
102106
}
@@ -106,6 +110,8 @@ void load_property_translations(const String &p_locale) {
106110
}
107111

108112
void load_doc_translations(const String &p_locale) {
113+
const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain("godot.documentation");
114+
109115
DocTranslationList *dtl = _doc_translations;
110116
while (dtl->data) {
111117
if (dtl->lang == p_locale) {
@@ -122,7 +128,7 @@ void load_doc_translations(const String &p_locale) {
122128

123129
if (tr.is_valid()) {
124130
tr->set_locale(dtl->lang);
125-
TranslationServer::get_singleton()->set_doc_translation(tr);
131+
domain->add_translation(tr);
126132
break;
127133
}
128134
}
@@ -132,6 +138,8 @@ void load_doc_translations(const String &p_locale) {
132138
}
133139

134140
void load_extractable_translations(const String &p_locale) {
141+
const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain("godot.editor");
142+
135143
ExtractableTranslationList *etl = _extractable_translations;
136144
while (etl->data) {
137145
if (etl->lang == p_locale) {
@@ -148,7 +156,7 @@ void load_extractable_translations(const String &p_locale) {
148156

149157
if (tr.is_valid()) {
150158
tr->set_locale(etl->lang);
151-
TranslationServer::get_singleton()->set_extractable_translation(tr);
159+
domain->add_translation(tr);
152160
break;
153161
}
154162
}

editor/gui/editor_file_dialog.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ void EditorFileDialog::_update_theme_item_cache() {
207207

208208
void EditorFileDialog::_notification(int p_what) {
209209
switch (p_what) {
210+
case NOTIFICATION_POSTINITIALIZE: {
211+
set_translation_domain(SNAME("godot.editor"));
212+
} break;
213+
210214
case NOTIFICATION_THEME_CHANGED:
211215
case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
212216
case NOTIFICATION_TRANSLATION_CHANGED: {

editor/project_manager.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,8 @@ void ProjectManager::_titlebar_resized() {
10811081
ProjectManager::ProjectManager() {
10821082
singleton = this;
10831083

1084+
set_translation_domain("godot.editor");
1085+
10841086
// Turn off some servers we aren't going to be using in the Project Manager.
10851087
NavigationServer3D::get_singleton()->set_active(false);
10861088
PhysicsServer3D::get_singleton()->set_active(false);

0 commit comments

Comments
 (0)