translation.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/os.h"
  32. #include "core/os/thread.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. List<Variant> keys;
  63. p_messages.get_key_list(&keys);
  64. for (const Variant &E : keys) {
  65. translation_map[E] = p_messages[E];
  66. }
  67. }
  68. void Translation::set_locale(const String &p_locale) {
  69. locale = TranslationServer::get_singleton()->standardize_locale(p_locale);
  70. if (Thread::is_main_thread()) {
  71. _notify_translation_changed_if_applies();
  72. } else {
  73. // Avoid calling non-thread-safe functions here.
  74. callable_mp(this, &Translation::_notify_translation_changed_if_applies).call_deferred();
  75. }
  76. }
  77. void Translation::_notify_translation_changed_if_applies() {
  78. if (OS::get_singleton()->get_main_loop() && TranslationServer::get_singleton()->get_loaded_locales().has(get_locale())) {
  79. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
  80. }
  81. }
  82. void Translation::add_message(const StringName &p_src_text, const StringName &p_xlated_text, const StringName &p_context) {
  83. translation_map[p_src_text] = p_xlated_text;
  84. }
  85. void Translation::add_plural_message(const StringName &p_src_text, const Vector<String> &p_plural_xlated_texts, const StringName &p_context) {
  86. 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");
  87. ERR_FAIL_COND_MSG(p_plural_xlated_texts.is_empty(), "Parameter vector p_plural_xlated_texts passed in is empty.");
  88. translation_map[p_src_text] = p_plural_xlated_texts[0];
  89. }
  90. StringName Translation::get_message(const StringName &p_src_text, const StringName &p_context) const {
  91. StringName ret;
  92. if (GDVIRTUAL_CALL(_get_message, p_src_text, p_context, ret)) {
  93. return ret;
  94. }
  95. if (p_context != StringName()) {
  96. 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");
  97. }
  98. HashMap<StringName, StringName>::ConstIterator E = translation_map.find(p_src_text);
  99. if (!E) {
  100. return StringName();
  101. }
  102. return E->value;
  103. }
  104. StringName Translation::get_plural_message(const StringName &p_src_text, const StringName &p_plural_text, int p_n, const StringName &p_context) const {
  105. StringName ret;
  106. if (GDVIRTUAL_CALL(_get_plural_message, p_src_text, p_plural_text, p_n, p_context, ret)) {
  107. return ret;
  108. }
  109. 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");
  110. return get_message(p_src_text);
  111. }
  112. void Translation::erase_message(const StringName &p_src_text, const StringName &p_context) {
  113. if (p_context != StringName()) {
  114. 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");
  115. }
  116. translation_map.erase(p_src_text);
  117. }
  118. void Translation::get_message_list(List<StringName> *r_messages) const {
  119. for (const KeyValue<StringName, StringName> &E : translation_map) {
  120. r_messages->push_back(E.key);
  121. }
  122. }
  123. int Translation::get_message_count() const {
  124. return translation_map.size();
  125. }
  126. void Translation::_bind_methods() {
  127. ClassDB::bind_method(D_METHOD("set_locale", "locale"), &Translation::set_locale);
  128. ClassDB::bind_method(D_METHOD("get_locale"), &Translation::get_locale);
  129. ClassDB::bind_method(D_METHOD("add_message", "src_message", "xlated_message", "context"), &Translation::add_message, DEFVAL(StringName()));
  130. ClassDB::bind_method(D_METHOD("add_plural_message", "src_message", "xlated_messages", "context"), &Translation::add_plural_message, DEFVAL(StringName()));
  131. ClassDB::bind_method(D_METHOD("get_message", "src_message", "context"), &Translation::get_message, DEFVAL(StringName()));
  132. ClassDB::bind_method(D_METHOD("get_plural_message", "src_message", "src_plural_message", "n", "context"), &Translation::get_plural_message, DEFVAL(StringName()));
  133. ClassDB::bind_method(D_METHOD("erase_message", "src_message", "context"), &Translation::erase_message, DEFVAL(StringName()));
  134. ClassDB::bind_method(D_METHOD("get_message_list"), &Translation::_get_message_list);
  135. ClassDB::bind_method(D_METHOD("get_translated_message_list"), &Translation::get_translated_message_list);
  136. ClassDB::bind_method(D_METHOD("get_message_count"), &Translation::get_message_count);
  137. ClassDB::bind_method(D_METHOD("_set_messages", "messages"), &Translation::_set_messages);
  138. ClassDB::bind_method(D_METHOD("_get_messages"), &Translation::_get_messages);
  139. GDVIRTUAL_BIND(_get_plural_message, "src_message", "src_plural_message", "n", "context");
  140. GDVIRTUAL_BIND(_get_message, "src_message", "context");
  141. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "messages", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_messages", "_get_messages");
  142. ADD_PROPERTY(PropertyInfo(Variant::STRING, "locale"), "set_locale", "get_locale");
  143. }