1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Merge pull request #107999 from timothyqiu/translation-cleanup

Clean up editor translation related methods
This commit is contained in:
Thaddeus Crews
2025-10-15 16:31:09 -05:00
12 changed files with 77 additions and 163 deletions

View File

@@ -202,7 +202,6 @@ StringName TranslationDomain::get_message_from_translations(const String &p_loca
int best_score = 0;
for (const Ref<Translation> &E : translations) {
ERR_CONTINUE(E.is_null());
int score = TranslationServer::get_singleton()->compare_locales(p_locale, E->get_locale());
if (score > 0 && score >= best_score) {
const StringName r = E->get_message(p_message, p_context);
@@ -225,7 +224,6 @@ StringName TranslationDomain::get_message_from_translations(const String &p_loca
int best_score = 0;
for (const Ref<Translation> &E : translations) {
ERR_CONTINUE(E.is_null());
int score = TranslationServer::get_singleton()->compare_locales(p_locale, E->get_locale());
if (score > 0 && score >= best_score) {
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
PackedStringArray TranslationDomain::get_loaded_locales() const {
PackedStringArray locales;
for (const Ref<Translation> &E : translations) {
ERR_CONTINUE(E.is_null());
const String &locale = E->get_locale();
if (!locales.has(locale)) {
locales.push_back(locale);
@@ -255,13 +252,20 @@ PackedStringArray TranslationDomain::get_loaded_locales() const {
return locales;
}
bool TranslationDomain::has_translation_for_locale(const String &p_locale) const {
for (const Ref<Translation> &E : translations) {
if (E->get_locale() == p_locale) {
return true;
}
}
return false;
}
// Translation objects that could potentially be used for the given locale.
HashSet<Ref<Translation>> TranslationDomain::get_potential_translations(const String &p_locale) const {
HashSet<Ref<Translation>> res;
for (const Ref<Translation> &E : translations) {
ERR_CONTINUE(E.is_null());
if (TranslationServer::get_singleton()->compare_locales(p_locale, E->get_locale()) > 0) {
res.insert(E);
}
@@ -274,8 +278,6 @@ Ref<Translation> TranslationDomain::get_translation_object(const String &p_local
int best_score = 0;
for (const Ref<Translation> &E : translations) {
ERR_CONTINUE(E.is_null());
int score = TranslationServer::get_singleton()->compare_locales(p_locale, E->get_locale());
if (score > 0 && score >= best_score) {
res = E;
@@ -289,6 +291,7 @@ Ref<Translation> TranslationDomain::get_translation_object(const String &p_local
}
void TranslationDomain::add_translation(const Ref<Translation> &p_translation) {
ERR_FAIL_COND_MSG(p_translation.is_null(), "Invalid translation provided.");
translations.insert(p_translation);
}

View File

@@ -71,6 +71,7 @@ public:
StringName get_message_from_translations(const String &p_locale, const StringName &p_message, const StringName &p_context) const;
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;
bool has_translation_for_locale(const String &p_locale) const;
HashSet<Ref<Translation>> get_potential_translations(const String &p_locale) const;
public:

View File

@@ -494,8 +494,7 @@ void TranslationServer::setup() {
String TranslationServer::get_tool_locale() {
#ifdef TOOLS_ENABLED
if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) {
const PackedStringArray &locales = editor_domain->get_loaded_locales();
if (locales.has(locale)) {
if (editor_domain->has_translation_for_locale(locale)) {
return locale;
}
return "en";
@@ -512,26 +511,6 @@ String TranslationServer::get_tool_locale() {
}
}
StringName TranslationServer::tool_translate(const StringName &p_message, const StringName &p_context) const {
return editor_domain->translate(p_message, p_context);
}
StringName TranslationServer::tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
return editor_domain->translate_plural(p_message, p_message_plural, p_n, p_context);
}
StringName TranslationServer::property_translate(const StringName &p_message, const StringName &p_context) const {
return property_domain->translate(p_message, p_context);
}
StringName TranslationServer::doc_translate(const StringName &p_message, const StringName &p_context) const {
return doc_domain->translate(p_message, p_context);
}
StringName TranslationServer::doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
return doc_domain->translate_plural(p_message, p_message_plural, p_n, p_context);
}
bool TranslationServer::is_pseudolocalization_enabled() const {
return main_domain->is_pseudolocalization_enabled();
}
@@ -648,9 +627,14 @@ void TranslationServer::load_translations() {
TranslationServer::TranslationServer() {
singleton = this;
main_domain.instantiate();
#ifdef TOOLS_ENABLED
editor_domain = get_or_add_domain("godot.editor");
property_domain = get_or_add_domain("godot.properties");
doc_domain = get_or_add_domain("godot.documentation");
#endif // TOOLS_ENABLED
init_locale_info();
}

View File

@@ -40,9 +40,11 @@ class TranslationServer : public Object {
String fallback;
Ref<TranslationDomain> main_domain;
#ifdef TOOLS_ENABLED
Ref<TranslationDomain> editor_domain;
Ref<TranslationDomain> property_domain;
Ref<TranslationDomain> doc_domain;
#endif // TOOLS_ENABLED
HashMap<StringName, Ref<TranslationDomain>> custom_domains;
mutable HashMap<String, int> locale_compare_cache;
@@ -95,8 +97,13 @@ class TranslationServer : public Object {
public:
_FORCE_INLINE_ static TranslationServer *get_singleton() { return singleton; }
// Built-in domain accessors. For engine code only, user code should use `get_or_add_domain()` instead.
Ref<TranslationDomain> get_main_domain() const { return main_domain; }
#ifdef TOOLS_ENABLED
Ref<TranslationDomain> get_editor_domain() const { return editor_domain; }
Ref<TranslationDomain> get_property_domain() const { return property_domain; }
Ref<TranslationDomain> get_doc_domain() const { return doc_domain; }
#endif // TOOLS_ENABLED
void set_locale(const String &p_locale);
String get_locale() const;
@@ -135,11 +142,6 @@ public:
int compare_locales(const String &p_locale_a, const String &p_locale_b) const;
String get_tool_locale();
StringName tool_translate(const StringName &p_message, const StringName &p_context = "") const;
StringName tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context = "") const;
StringName property_translate(const StringName &p_message, const StringName &p_context = "") const;
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;
bool has_domain(const StringName &p_domain) const;
Ref<TranslationDomain> get_or_add_domain(const StringName &p_domain);

View File

@@ -5854,7 +5854,7 @@ Vector<uint8_t> String::to_multibyte_char_buffer(const String &p_encoding) const
*/
String TTR(const String &p_text, const String &p_context) {
if (TranslationServer::get_singleton()) {
return TranslationServer::get_singleton()->tool_translate(p_text, p_context);
return TranslationServer::get_singleton()->get_editor_domain()->translate(p_text, p_context);
}
return p_text;
@@ -5874,7 +5874,7 @@ String TTR(const String &p_text, const String &p_context) {
*/
String TTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context) {
if (TranslationServer::get_singleton()) {
return TranslationServer::get_singleton()->tool_translate_plural(p_text, p_text_plural, p_n, p_context);
return TranslationServer::get_singleton()->get_editor_domain()->translate_plural(p_text, p_text_plural, p_n, p_context);
}
// 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) {
const String text = p_text.dedent().strip_edges();
if (TranslationServer::get_singleton()) {
return String(TranslationServer::get_singleton()->doc_translate(text, p_context)).replace("$DOCS_URL", GODOT_VERSION_DOCS_URL);
return String(TranslationServer::get_singleton()->get_doc_domain()->translate(text, p_context)).replace("$DOCS_URL", GODOT_VERSION_DOCS_URL);
}
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
const String text_plural = p_text_plural.dedent().strip_edges();
if (TranslationServer::get_singleton()) {
return String(TranslationServer::get_singleton()->doc_translate_plural(text, text_plural, p_n, p_context)).replace("$DOCS_URL", GODOT_VERSION_DOCS_URL);
return String(TranslationServer::get_singleton()->get_doc_domain()->translate_plural(text, text_plural, p_n, p_context)).replace("$DOCS_URL", GODOT_VERSION_DOCS_URL);
}
// 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
*/
String RTR(const String &p_text, const String &p_context) {
if (TranslationServer::get_singleton()) {
String rtr = TranslationServer::get_singleton()->tool_translate(p_text, p_context);
if (rtr.is_empty() || rtr == p_text) {
return TranslationServer::get_singleton()->translate(p_text, p_context);
#ifdef TOOLS_ENABLED
String rtr = TranslationServer::get_singleton()->get_editor_domain()->translate(p_text, p_context);
if (!rtr.is_empty() && rtr != p_text) {
return rtr;
}
return rtr;
#endif // TOOLS_ENABLED
return TranslationServer::get_singleton()->translate(p_text, p_context);
}
return p_text;
@@ -5959,11 +5961,13 @@ String RTR(const String &p_text, const String &p_context) {
*/
String RTRN(const String &p_text, const String &p_text_plural, int p_n, const String &p_context) {
if (TranslationServer::get_singleton()) {
String rtr = TranslationServer::get_singleton()->tool_translate_plural(p_text, p_text_plural, p_n, p_context);
if (rtr.is_empty() || rtr == p_text || rtr == p_text_plural) {
return TranslationServer::get_singleton()->translate_plural(p_text, p_text_plural, p_n, p_context);
#ifdef TOOLS_ENABLED
String rtr = TranslationServer::get_singleton()->get_editor_domain()->translate_plural(p_text, p_text_plural, p_n, p_context);
if (!rtr.is_empty() && rtr != p_text && rtr != p_text_plural) {
return rtr;
}
return rtr;
#endif // TOOLS_ENABLED
return TranslationServer::get_singleton()->translate_plural(p_text, p_text_plural, p_n, p_context);
}
// Return message based on English plural rule if translation is not possible.