test_translation.h 8.6 KB

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