translation.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**************************************************************************/
  2. /* translation.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "translation.h"
  31. #include "core/os/thread.h"
  32. #include "core/string/plural_rules.h"
  33. #include "core/string/translation_server.h"
  34. Dictionary Translation::_get_messages() const {
  35. Dictionary d;
  36. for (const KeyValue<StringName, StringName> &E : translation_map) {
  37. d[E.key] = E.value;
  38. }
  39. return d;
  40. }
  41. Vector<String> Translation::_get_message_list() const {
  42. Vector<String> msgs;
  43. msgs.resize(translation_map.size());
  44. int idx = 0;
  45. for (const KeyValue<StringName, StringName> &E : translation_map) {
  46. msgs.set(idx, E.key);
  47. idx += 1;
  48. }
  49. return msgs;
  50. }
  51. Vector<String> Translation::get_translated_message_list() const {
  52. Vector<String> msgs;
  53. msgs.resize(translation_map.size());
  54. int idx = 0;
  55. for (const KeyValue<StringName, StringName> &E : translation_map) {
  56. msgs.set(idx, E.value);
  57. idx += 1;
  58. }
  59. return msgs;
  60. }
  61. void Translation::_set_messages(const Dictionary &p_messages) {
  62. for (const KeyValue<Variant, Variant> &kv : p_messages) {
  63. translation_map[kv.key] = kv.value;
  64. }
  65. }
  66. void Translation::set_locale(const String &p_locale) {
  67. locale = TranslationServer::get_singleton()->standardize_locale(p_locale);
  68. if (plural_rules_cache && plural_rules_override.is_empty()) {
  69. memdelete(plural_rules_cache);
  70. plural_rules_cache = nullptr;
  71. }
  72. }
  73. void Translation::add_message(const StringName &p_src_text, const StringName &p_xlated_text, const StringName &p_context) {
  74. translation_map[p_src_text] = p_xlated_text;
  75. }
  76. void Translation::add_plural_message(const StringName &p_src_text, const Vector<String> &p_plural_xlated_texts, const StringName &p_context) {
  77. WARN_PRINT("Translation class doesn't handle plural messages. Calling add_plural_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles plurals, such as TranslationPO class");
  78. ERR_FAIL_COND_MSG(p_plural_xlated_texts.is_empty(), "Parameter vector p_plural_xlated_texts passed in is empty.");
  79. translation_map[p_src_text] = p_plural_xlated_texts[0];
  80. }
  81. StringName Translation::get_message(const StringName &p_src_text, const StringName &p_context) const {
  82. StringName ret;
  83. if (GDVIRTUAL_CALL(_get_message, p_src_text, p_context, ret)) {
  84. return ret;
  85. }
  86. if (p_context != StringName()) {
  87. WARN_PRINT("Translation class doesn't handle context. Using context in get_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles context, such as TranslationPO class");
  88. }
  89. HashMap<StringName, StringName>::ConstIterator E = translation_map.find(p_src_text);
  90. if (!E) {
  91. return StringName();
  92. }
  93. return E->value;
  94. }
  95. StringName Translation::get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context) const {
  96. StringName ret;
  97. if (GDVIRTUAL_CALL(_get_plural_message, p_src_text, p_plural_text, p_n, p_context, ret)) {
  98. return ret;
  99. }
  100. WARN_PRINT("Translation class doesn't handle plural messages. Calling get_plural_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles plurals, such as TranslationPO class");
  101. return get_message(p_src_text);
  102. }
  103. void Translation::erase_message(const StringName &p_src_text, const StringName &p_context) {
  104. if (p_context != StringName()) {
  105. WARN_PRINT("Translation class doesn't handle context. Using context in erase_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles context, such as TranslationPO class");
  106. }
  107. translation_map.erase(p_src_text);
  108. }
  109. void Translation::get_message_list(List<StringName> *r_messages) const {
  110. for (const KeyValue<StringName, StringName> &E : translation_map) {
  111. r_messages->push_back(E.key);
  112. }
  113. }
  114. int Translation::get_message_count() const {
  115. return translation_map.size();
  116. }
  117. PluralRules *Translation::_get_plural_rules() const {
  118. if (plural_rules_cache) {
  119. return plural_rules_cache;
  120. }
  121. if (!plural_rules_override.is_empty()) {
  122. plural_rules_cache = PluralRules::parse(plural_rules_override);
  123. }
  124. if (!plural_rules_cache) {
  125. // Locale's default plural rules.
  126. const String &default_rule = TranslationServer::get_singleton()->get_plural_rules(locale);
  127. if (!default_rule.is_empty()) {
  128. plural_rules_cache = PluralRules::parse(default_rule);
  129. }
  130. // Use English plural rules as a fallback.
  131. if (!plural_rules_cache) {
  132. plural_rules_cache = PluralRules::parse("nplurals=2; plural=(n != 1);");
  133. }
  134. }
  135. DEV_ASSERT(plural_rules_cache != nullptr);
  136. return plural_rules_cache;
  137. }
  138. void Translation::set_plural_rules_override(const String &p_rules) {
  139. plural_rules_override = p_rules;
  140. if (plural_rules_cache) {
  141. memdelete(plural_rules_cache);
  142. plural_rules_cache = nullptr;
  143. }
  144. }
  145. String Translation::get_plural_rules_override() const {
  146. return plural_rules_override;
  147. }
  148. void Translation::_bind_methods() {
  149. ClassDB::bind_method(D_METHOD("set_locale", "locale"), &Translation::set_locale);
  150. ClassDB::bind_method(D_METHOD("get_locale"), &Translation::get_locale);
  151. ClassDB::bind_method(D_METHOD("add_message", "src_message", "xlated_message", "context"), &Translation::add_message, DEFVAL(StringName()));
  152. ClassDB::bind_method(D_METHOD("add_plural_message", "src_message", "xlated_messages", "context"), &Translation::add_plural_message, DEFVAL(StringName()));
  153. ClassDB::bind_method(D_METHOD("get_message", "src_message", "context"), &Translation::get_message, DEFVAL(StringName()));
  154. ClassDB::bind_method(D_METHOD("get_plural_message", "src_message", "src_plural_message", "n", "context"), &Translation::get_plural_message, DEFVAL(StringName()));
  155. ClassDB::bind_method(D_METHOD("erase_message", "src_message", "context"), &Translation::erase_message, DEFVAL(StringName()));
  156. ClassDB::bind_method(D_METHOD("get_message_list"), &Translation::_get_message_list);
  157. ClassDB::bind_method(D_METHOD("get_translated_message_list"), &Translation::get_translated_message_list);
  158. ClassDB::bind_method(D_METHOD("get_message_count"), &Translation::get_message_count);
  159. ClassDB::bind_method(D_METHOD("_set_messages", "messages"), &Translation::_set_messages);
  160. ClassDB::bind_method(D_METHOD("_get_messages"), &Translation::_get_messages);
  161. ClassDB::bind_method(D_METHOD("set_plural_rules_override", "rules"), &Translation::set_plural_rules_override);
  162. ClassDB::bind_method(D_METHOD("get_plural_rules_override"), &Translation::get_plural_rules_override);
  163. GDVIRTUAL_BIND(_get_plural_message, "src_message", "src_plural_message", "n", "context");
  164. GDVIRTUAL_BIND(_get_message, "src_message", "context");
  165. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "messages", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_messages", "_get_messages");
  166. ADD_PROPERTY(PropertyInfo(Variant::STRING, "locale"), "set_locale", "get_locale");
  167. ADD_PROPERTY(PropertyInfo(Variant::STRING, "plural_rules_override"), "set_plural_rules_override", "get_plural_rules_override");
  168. }
  169. Translation::~Translation() {
  170. if (plural_rules_cache) {
  171. memdelete(plural_rules_cache);
  172. plural_rules_cache = nullptr;
  173. }
  174. }