From c0d837a2ea9ca888f673485c4b9d8d9ae1936375 Mon Sep 17 00:00:00 2001 From: SkyJJ Date: Thu, 16 Jul 2020 10:52:06 +0200 Subject: Added plurals and context support to Translation --- core/object.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'core/object.cpp') diff --git a/core/object.cpp b/core/object.cpp index ff6d4a666f..9d38f36009 100644 --- a/core/object.cpp +++ b/core/object.cpp @@ -1432,12 +1432,23 @@ void Object::initialize_class() { initialized = true; } -StringName Object::tr(const StringName &p_message) const { +StringName Object::tr(const StringName &p_message, const StringName &p_context) const { if (!_can_translate || !TranslationServer::get_singleton()) { return p_message; } + return TranslationServer::get_singleton()->translate(p_message, p_context); +} - return TranslationServer::get_singleton()->translate(p_message); +String Object::tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const { + if (!_can_translate || !TranslationServer::get_singleton()) { + // Return message based on English plural rule if translation is not possible. + if (p_n == 1) { + return p_message; + } else { + return p_message_plural; + } + } + return TranslationServer::get_singleton()->translate_plural(p_message, p_message_plural, p_n, p_context); } void Object::_clear_internal_resource_paths(const Variant &p_var) { @@ -1578,7 +1589,8 @@ void Object::_bind_methods() { ClassDB::bind_method(D_METHOD("set_message_translation", "enable"), &Object::set_message_translation); ClassDB::bind_method(D_METHOD("can_translate_messages"), &Object::can_translate_messages); - ClassDB::bind_method(D_METHOD("tr", "message"), &Object::tr); + ClassDB::bind_method(D_METHOD("tr", "message", "context"), &Object::tr, DEFVAL("")); + ClassDB::bind_method(D_METHOD("tr_n", "message", "plural_message", "n", "context"), &Object::tr_n, DEFVAL("")); ClassDB::bind_method(D_METHOD("is_queued_for_deletion"), &Object::is_queued_for_deletion); -- cgit v1.2.3 From 0ef758eaeeb5b2f44e132865f0f10baf692e972f Mon Sep 17 00:00:00 2001 From: SkyJJ Date: Fri, 7 Aug 2020 13:17:12 +0200 Subject: Updated Translation architecture to have TranslationPO, did some commit fixes and updated class Reference. --- core/object.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'core/object.cpp') diff --git a/core/object.cpp b/core/object.cpp index 9d38f36009..67c605c39b 100644 --- a/core/object.cpp +++ b/core/object.cpp @@ -1432,7 +1432,7 @@ void Object::initialize_class() { initialized = true; } -StringName Object::tr(const StringName &p_message, const StringName &p_context) const { +String Object::tr(const StringName &p_message, const StringName &p_context) const { if (!_can_translate || !TranslationServer::get_singleton()) { return p_message; } @@ -1444,9 +1444,8 @@ String Object::tr_n(const StringName &p_message, const StringName &p_message_plu // Return message based on English plural rule if translation is not possible. if (p_n == 1) { return p_message; - } else { - return p_message_plural; } + return p_message_plural; } return TranslationServer::get_singleton()->translate_plural(p_message, p_message_plural, p_n, p_context); } -- cgit v1.2.3