From 5d7e003b29cf9b57168e459469e13468a49c3da6 Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Tue, 27 Dec 2022 10:58:01 +0800 Subject: [PATCH] Prepare for moving editor and classref translations to godot-editor-l10n repo - Separate editor interface and property translations. - Add property translation in TranslationServer. - The split and merge of the POT/PO/Makefiles and extract scripts is done directly in godot-editor-l10n, the files will be removed in the next commit. - Remove the hardcoded "to_include" lists from the SCsub, we'll only commit the files which are ready to inclue. --- core/string/translation.cpp | 14 ++++++++++++ core/string/translation.h | 3 +++ editor/SCsub | 17 +++++++++----- editor/editor_builders.py | 4 ++++ editor/editor_inspector.cpp | 4 ++-- editor/editor_property_name_processor.cpp | 19 +++++++++++++--- editor/editor_property_name_processor.h | 3 +++ editor/editor_settings.cpp | 1 + editor/editor_translation.cpp | 27 +++++++++++++++++++++++ editor/editor_translation.h | 1 + 10 files changed, 82 insertions(+), 11 deletions(-) diff --git a/core/string/translation.cpp b/core/string/translation.cpp index 60dca8ebc6d4..b9d5d3b538e3 100644 --- a/core/string/translation.cpp +++ b/core/string/translation.cpp @@ -768,6 +768,20 @@ StringName TranslationServer::doc_translate_plural(const StringName &p_message, return p_message_plural; } +void TranslationServer::set_property_translation(const Ref &p_translation) { + property_translation = p_translation; +} + +StringName TranslationServer::property_translate(const StringName &p_message) const { + if (property_translation.is_valid()) { + StringName r = property_translation->get_message(p_message); + if (r) { + return r; + } + } + return p_message; +} + bool TranslationServer::is_pseudolocalization_enabled() const { return pseudolocalization_enabled; } diff --git a/core/string/translation.h b/core/string/translation.h index 8646635eb83c..01d239f81cef 100644 --- a/core/string/translation.h +++ b/core/string/translation.h @@ -78,6 +78,7 @@ class TranslationServer : public Object { HashSet> translations; Ref tool_translation; Ref doc_translation; + Ref property_translation; bool enabled = true; @@ -174,6 +175,8 @@ public: void set_doc_translation(const Ref &p_translation); StringName doc_translate(const StringName &p_message, const StringName &p_context = "") const; StringName doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const; + void set_property_translation(const Ref &p_translation); + StringName property_translate(const StringName &p_message) const; void setup(); diff --git a/editor/SCsub b/editor/SCsub index 32ad88add59b..dddec55ed654 100644 --- a/editor/SCsub +++ b/editor/SCsub @@ -75,10 +75,7 @@ if env.editor_build: # Generated with `make include-list` for each resource. # Editor translations - to_include = ( - "ar,bg,ca,cs,de,el,eo,es_AR,es,fi,fr,gl,he,hu,id,it,ja,ko,lv,ms,nb,nl,pl,pt_BR,pt,ro,ru,sk,sv,th,tr,uk,vi,zh_CN,zh_TW" - ).split(",") - tlist = [env.Dir("#editor/translations").abspath + "/" + f + ".po" for f in to_include] + tlist = glob.glob(env.Dir("#editor/translations/editor").abspath + "/*.po") env.Depends("#editor/editor_translations.gen.h", tlist) env.CommandNoCache( "#editor/editor_translations.gen.h", @@ -86,9 +83,17 @@ if env.editor_build: env.Run(editor_builders.make_editor_translations_header, "Generating editor translations header."), ) + # Property translations + tlist = glob.glob(env.Dir("#editor/translations/properties").abspath + "/*.po") + env.Depends("#editor/property_translations.gen.h", tlist) + env.CommandNoCache( + "#editor/property_translations.gen.h", + tlist, + env.Run(editor_builders.make_property_translations_header, "Generating property translations header."), + ) + # Documentation translations - to_include = "de,es,fr,ja,zh_CN".split(",") - tlist = [env.Dir("#doc/translations").abspath + "/" + f + ".po" for f in to_include] + tlist = glob.glob(env.Dir("#doc/translations").abspath + "/*.po") env.Depends("#editor/doc_translations.gen.h", tlist) env.CommandNoCache( "#editor/doc_translations.gen.h", diff --git a/editor/editor_builders.py b/editor/editor_builders.py index 696e3b64ec90..2a4f79548ff9 100644 --- a/editor/editor_builders.py +++ b/editor/editor_builders.py @@ -161,6 +161,10 @@ def make_editor_translations_header(target, source, env): make_translations_header(target, source, env, "editor") +def make_property_translations_header(target, source, env): + make_translations_header(target, source, env, "property") + + def make_doc_translations_header(target, source, env): make_translations_header(target, source, env, "doc") diff --git a/editor/editor_inspector.cpp b/editor/editor_inspector.cpp index 42d8f48ea0ed..275e47e37020 100644 --- a/editor/editor_inspector.cpp +++ b/editor/editor_inspector.cpp @@ -2974,11 +2974,11 @@ void EditorInspector::update_tree() { // Only process group label if this is not the group or subgroup. if ((i == 0 && component == group) || (i == 1 && component == subgroup)) { if (section_name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED) { - label = TTRGET(component); + label = EditorPropertyNameProcessor::get_singleton()->translate_group_name(component); tooltip = component; } else { label = component; - tooltip = TTRGET(component); + tooltip = EditorPropertyNameProcessor::get_singleton()->translate_group_name(component); } } else { label = EditorPropertyNameProcessor::get_singleton()->process_name(component, section_name_style); diff --git a/editor/editor_property_name_processor.cpp b/editor/editor_property_name_processor.cpp index 5380fddde29b..a183e6261859 100644 --- a/editor/editor_property_name_processor.cpp +++ b/editor/editor_property_name_processor.cpp @@ -30,6 +30,7 @@ #include "editor_property_name_processor.h" +#include "core/string/translation.h" #include "editor_settings.h" EditorPropertyNameProcessor *EditorPropertyNameProcessor::singleton = nullptr; @@ -92,18 +93,30 @@ String EditorPropertyNameProcessor::process_name(const String &p_name, Style p_s } break; case STYLE_LOCALIZED: { - return TTRGET(_capitalize_name(p_name)); + const String capitalized = _capitalize_name(p_name); + if (TranslationServer::get_singleton()) { + return TranslationServer::get_singleton()->property_translate(capitalized); + } + return capitalized; } break; } ERR_FAIL_V_MSG(p_name, "Unexpected property name style."); } +String EditorPropertyNameProcessor::translate_group_name(const String &p_name) const { + if (TranslationServer::get_singleton()) { + return TranslationServer::get_singleton()->property_translate(p_name); + } + return p_name; +} + EditorPropertyNameProcessor::EditorPropertyNameProcessor() { ERR_FAIL_COND(singleton != nullptr); singleton = this; - // The following initialization is parsed in `editor/translations/extract.py` with a regex. + // The following initialization is parsed by the l10n extraction script with a regex. // The map name and value definition format should be kept synced with the regex. + // https://github.com/godotengine/godot-editor-l10n/blob/main/scripts/common.py capitalize_string_remaps["2d"] = "2D"; capitalize_string_remaps["3d"] = "3D"; capitalize_string_remaps["aa"] = "AA"; @@ -263,7 +276,7 @@ EditorPropertyNameProcessor::EditorPropertyNameProcessor() { capitalize_string_remaps["yz"] = "YZ"; // Articles, conjunctions, prepositions. - // The following initialization is parsed in `editor/translations/extract.py` with a regex. + // The following initialization is parsed in `editor/translations/scripts/common.py` with a regex. // The word definition format should be kept synced with the regex. stop_words = LocalVector({ "a", diff --git a/editor/editor_property_name_processor.h b/editor/editor_property_name_processor.h index f32cb9f43a92..8e3cecb45b82 100644 --- a/editor/editor_property_name_processor.h +++ b/editor/editor_property_name_processor.h @@ -64,6 +64,9 @@ public: // Turns property path segment into the given style. String process_name(const String &p_name, Style p_style) const; + // Translate plain text group names. + String translate_group_name(const String &p_name) const; + EditorPropertyNameProcessor(); ~EditorPropertyNameProcessor(); }; diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp index 1c988840acb7..8eb255184336 100644 --- a/editor/editor_settings.cpp +++ b/editor/editor_settings.cpp @@ -922,6 +922,7 @@ void EditorSettings::setup_language() { } // Load editor translation for configured/detected locale. load_editor_translations(lang); + load_property_translations(lang); // Load class reference translation. load_doc_translations(lang); diff --git a/editor/editor_translation.cpp b/editor/editor_translation.cpp index 426daad82355..54a9d2f0684d 100644 --- a/editor/editor_translation.cpp +++ b/editor/editor_translation.cpp @@ -35,6 +35,7 @@ #include "core/io/translation_loader_po.h" #include "editor/doc_translations.gen.h" #include "editor/editor_translations.gen.h" +#include "editor/property_translations.gen.h" Vector get_editor_locales() { Vector locales; @@ -101,3 +102,29 @@ void load_doc_translations(const String &p_locale) { dtl++; } } + +void load_property_translations(const String &p_locale) { + PropertyTranslationList *etl = _property_translations; + while (etl->data) { + if (etl->lang == p_locale) { + Vector data; + data.resize(etl->uncomp_size); + int ret = Compression::decompress(data.ptrw(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE); + ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt."); + + Ref fa; + fa.instantiate(); + fa->open_custom(data.ptr(), data.size()); + + Ref tr = TranslationLoaderPO::load_translation(fa); + + if (tr.is_valid()) { + tr->set_locale(etl->lang); + TranslationServer::get_singleton()->set_property_translation(tr); + break; + } + } + + etl++; + } +} diff --git a/editor/editor_translation.h b/editor/editor_translation.h index 3717d0cbf500..bd6db32536af 100644 --- a/editor/editor_translation.h +++ b/editor/editor_translation.h @@ -37,5 +37,6 @@ Vector get_editor_locales(); void load_editor_translations(const String &p_locale); void load_doc_translations(const String &p_locale); +void load_property_translations(const String &p_locale); #endif // EDITOR_TRANSLATION_H