test_translation.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**************************************************************************/
  2. /* test_translation.h */
  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. #ifndef TEST_TRANSLATION_H
  31. #define TEST_TRANSLATION_H
  32. #include "core/string/optimized_translation.h"
  33. #include "core/string/translation.h"
  34. #include "core/string/translation_po.h"
  35. #include "core/string/translation_server.h"
  36. #ifdef TOOLS_ENABLED
  37. #include "editor/import/resource_importer_csv_translation.h"
  38. #endif
  39. #include "tests/test_macros.h"
  40. #include "tests/test_utils.h"
  41. namespace TestTranslation {
  42. TEST_CASE("[Translation] Messages") {
  43. Ref<Translation> translation = memnew(Translation);
  44. translation->set_locale("fr");
  45. translation->add_message("Hello", "Bonjour");
  46. CHECK(translation->get_message("Hello") == "Bonjour");
  47. translation->erase_message("Hello");
  48. // The message no longer exists, so it returns an empty string instead.
  49. CHECK(translation->get_message("Hello") == "");
  50. List<StringName> messages;
  51. translation->get_message_list(&messages);
  52. CHECK(translation->get_message_count() == 0);
  53. CHECK(messages.size() == 0);
  54. translation->add_message("Hello2", "Bonjour2");
  55. translation->add_message("Hello3", "Bonjour3");
  56. messages.clear();
  57. translation->get_message_list(&messages);
  58. CHECK(translation->get_message_count() == 2);
  59. CHECK(messages.size() == 2);
  60. // Messages are stored in a Map, don't assume ordering.
  61. CHECK(messages.find("Hello2"));
  62. CHECK(messages.find("Hello3"));
  63. }
  64. TEST_CASE("[TranslationPO] Messages with context") {
  65. Ref<TranslationPO> translation = memnew(TranslationPO);
  66. translation->set_locale("fr");
  67. translation->add_message("Hello", "Bonjour");
  68. translation->add_message("Hello", "Salut", "friendly");
  69. CHECK(translation->get_message("Hello") == "Bonjour");
  70. CHECK(translation->get_message("Hello", "friendly") == "Salut");
  71. CHECK(translation->get_message("Hello", "nonexistent_context") == "");
  72. // Only remove the message for the default context, not the "friendly" context.
  73. translation->erase_message("Hello");
  74. // The message no longer exists, so it returns an empty string instead.
  75. CHECK(translation->get_message("Hello") == "");
  76. CHECK(translation->get_message("Hello", "friendly") == "Salut");
  77. CHECK(translation->get_message("Hello", "nonexistent_context") == "");
  78. List<StringName> messages;
  79. translation->get_message_list(&messages);
  80. // `get_message_count()` takes all contexts into account.
  81. CHECK(translation->get_message_count() == 1);
  82. // Only the default context is taken into account.
  83. // Since "Hello" is now only present in a non-default context, it is not counted in the list of messages.
  84. CHECK(messages.size() == 0);
  85. translation->add_message("Hello2", "Bonjour2");
  86. translation->add_message("Hello2", "Salut2", "friendly");
  87. translation->add_message("Hello3", "Bonjour3");
  88. messages.clear();
  89. translation->get_message_list(&messages);
  90. // `get_message_count()` takes all contexts into account.
  91. CHECK(translation->get_message_count() == 4);
  92. // Only the default context is taken into account.
  93. CHECK(messages.size() == 2);
  94. // Messages are stored in a Map, don't assume ordering.
  95. CHECK(messages.find("Hello2"));
  96. CHECK(messages.find("Hello3"));
  97. }
  98. TEST_CASE("[TranslationPO] Plural messages") {
  99. Ref<TranslationPO> translation = memnew(TranslationPO);
  100. translation->set_locale("fr");
  101. translation->set_plural_rule("Plural-Forms: nplurals=2; plural=(n >= 2);");
  102. CHECK(translation->get_plural_forms() == 2);
  103. PackedStringArray plurals;
  104. plurals.push_back("Il y a %d pomme");
  105. plurals.push_back("Il y a %d pommes");
  106. translation->add_plural_message("There are %d apples", plurals);
  107. ERR_PRINT_OFF;
  108. // This is invalid, as the number passed to `get_plural_message()` may not be negative.
  109. CHECK(vformat(translation->get_plural_message("There are %d apples", "", -1), -1) == "");
  110. ERR_PRINT_ON;
  111. CHECK(vformat(translation->get_plural_message("There are %d apples", "", 0), 0) == "Il y a 0 pomme");
  112. CHECK(vformat(translation->get_plural_message("There are %d apples", "", 1), 1) == "Il y a 1 pomme");
  113. CHECK(vformat(translation->get_plural_message("There are %d apples", "", 2), 2) == "Il y a 2 pommes");
  114. }
  115. #ifdef TOOLS_ENABLED
  116. TEST_CASE("[OptimizedTranslation] Generate from Translation and read messages") {
  117. Ref<Translation> translation = memnew(Translation);
  118. translation->set_locale("fr");
  119. translation->add_message("Hello", "Bonjour");
  120. translation->add_message("Hello2", "Bonjour2");
  121. translation->add_message("Hello3", "Bonjour3");
  122. Ref<OptimizedTranslation> optimized_translation = memnew(OptimizedTranslation);
  123. optimized_translation->generate(translation);
  124. CHECK(optimized_translation->get_message("Hello") == "Bonjour");
  125. CHECK(optimized_translation->get_message("Hello2") == "Bonjour2");
  126. CHECK(optimized_translation->get_message("Hello3") == "Bonjour3");
  127. CHECK(optimized_translation->get_message("DoesNotExist") == "");
  128. List<StringName> messages;
  129. // `get_message_list()` can't return the list of messages stored in an OptimizedTranslation.
  130. optimized_translation->get_message_list(&messages);
  131. CHECK(optimized_translation->get_message_count() == 0);
  132. CHECK(messages.size() == 0);
  133. }
  134. TEST_CASE("[TranslationCSV] CSV import") {
  135. Ref<ResourceImporterCSVTranslation> import_csv_translation = memnew(ResourceImporterCSVTranslation);
  136. HashMap<StringName, Variant> options;
  137. options["compress"] = false;
  138. options["delimiter"] = 0;
  139. List<String> gen_files;
  140. Error result = import_csv_translation->import(0, TestUtils::get_data_path("translations.csv"),
  141. "", options, nullptr, &gen_files);
  142. CHECK(result == OK);
  143. CHECK(gen_files.size() == 4);
  144. TranslationServer *ts = TranslationServer::get_singleton();
  145. for (const String &file : gen_files) {
  146. Ref<Translation> translation = ResourceLoader::load(file);
  147. CHECK(translation.is_valid());
  148. ts->add_translation(translation);
  149. }
  150. ts->set_locale("en");
  151. // `tr` can be called on any Object, we reuse TranslationServer for convenience.
  152. CHECK(ts->tr("GOOD_MORNING") == "Good Morning");
  153. CHECK(ts->tr("GOOD_EVENING") == "Good Evening");
  154. ts->set_locale("de");
  155. CHECK(ts->tr("GOOD_MORNING") == "Guten Morgen");
  156. CHECK(ts->tr("GOOD_EVENING") == "Good Evening"); // Left blank in CSV, should source from 'en'.
  157. ts->set_locale("ja");
  158. CHECK(ts->tr("GOOD_MORNING") == String::utf8("おはよう"));
  159. CHECK(ts->tr("GOOD_EVENING") == String::utf8("こんばんは"));
  160. /* FIXME: This passes, but triggers a chain reaction that makes test_viewport
  161. * and test_text_edit explode in a billion glittery Unicode particles.
  162. ts->set_locale("fa");
  163. CHECK(ts->tr("GOOD_MORNING") == String::utf8("صبح بخیر"));
  164. CHECK(ts->tr("GOOD_EVENING") == String::utf8("عصر بخیر"));
  165. */
  166. }
  167. #endif // TOOLS_ENABLED
  168. } // namespace TestTranslation
  169. #endif // TEST_TRANSLATION_H