2
0

test_translation.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*************************************************************************/
  2. /* test_translation.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "thirdparty/doctest/doctest.h"
  39. namespace TestTranslation {
  40. TEST_CASE("[Translation] Messages") {
  41. Ref<Translation> translation = memnew(Translation);
  42. translation->set_locale("fr");
  43. translation->add_message("Hello", "Bonjour");
  44. CHECK(translation->get_message("Hello") == "Bonjour");
  45. translation->erase_message("Hello");
  46. // The message no longer exists, so it returns an empty string instead.
  47. CHECK(translation->get_message("Hello") == "");
  48. List<StringName> messages;
  49. translation->get_message_list(&messages);
  50. CHECK(translation->get_message_count() == 0);
  51. CHECK(messages.size() == 0);
  52. translation->add_message("Hello2", "Bonjour2");
  53. translation->add_message("Hello3", "Bonjour3");
  54. messages.clear();
  55. translation->get_message_list(&messages);
  56. CHECK(translation->get_message_count() == 2);
  57. CHECK(messages.size() == 2);
  58. // Messages are stored in a Map, don't assume ordering.
  59. CHECK(messages.find("Hello2"));
  60. CHECK(messages.find("Hello3"));
  61. }
  62. TEST_CASE("[TranslationPO] Messages with context") {
  63. Ref<TranslationPO> translation = memnew(TranslationPO);
  64. translation->set_locale("fr");
  65. translation->add_message("Hello", "Bonjour");
  66. translation->add_message("Hello", "Salut", "friendly");
  67. CHECK(translation->get_message("Hello") == "Bonjour");
  68. CHECK(translation->get_message("Hello", "friendly") == "Salut");
  69. CHECK(translation->get_message("Hello", "nonexistent_context") == "");
  70. // Only remove the message for the default context, not the "friendly" context.
  71. translation->erase_message("Hello");
  72. // The message no longer exists, so it returns an empty string instead.
  73. CHECK(translation->get_message("Hello") == "");
  74. CHECK(translation->get_message("Hello", "friendly") == "Salut");
  75. CHECK(translation->get_message("Hello", "nonexistent_context") == "");
  76. List<StringName> messages;
  77. translation->get_message_list(&messages);
  78. // `get_message_count()` takes all contexts into account.
  79. CHECK(translation->get_message_count() == 1);
  80. // Only the default context is taken into account.
  81. // Since "Hello" is now only present in a non-default context, it is not counted in the list of messages.
  82. CHECK(messages.size() == 0);
  83. translation->add_message("Hello2", "Bonjour2");
  84. translation->add_message("Hello2", "Salut2", "friendly");
  85. translation->add_message("Hello3", "Bonjour3");
  86. messages.clear();
  87. translation->get_message_list(&messages);
  88. // `get_message_count()` takes all contexts into account.
  89. CHECK(translation->get_message_count() == 4);
  90. // Only the default context is taken into account.
  91. CHECK(messages.size() == 2);
  92. // Messages are stored in a Map, don't assume ordering.
  93. CHECK(messages.find("Hello2"));
  94. CHECK(messages.find("Hello3"));
  95. }
  96. TEST_CASE("[TranslationPO] Plural messages") {
  97. Ref<TranslationPO> translation = memnew(TranslationPO);
  98. translation->set_locale("fr");
  99. translation->set_plural_rule("Plural-Forms: nplurals=2; plural=(n >= 2);");
  100. CHECK(translation->get_plural_forms() == 2);
  101. PackedStringArray plurals;
  102. plurals.push_back("Il y a %d pomme");
  103. plurals.push_back("Il y a %d pommes");
  104. translation->add_plural_message("There are %d apples", plurals);
  105. ERR_PRINT_OFF;
  106. // This is invalid, as the number passed to `get_plural_message()` may not be negative.
  107. CHECK(vformat(translation->get_plural_message("There are %d apples", "", -1), -1) == "");
  108. ERR_PRINT_ON;
  109. CHECK(vformat(translation->get_plural_message("There are %d apples", "", 0), 0) == "Il y a 0 pomme");
  110. CHECK(vformat(translation->get_plural_message("There are %d apples", "", 1), 1) == "Il y a 1 pomme");
  111. CHECK(vformat(translation->get_plural_message("There are %d apples", "", 2), 2) == "Il y a 2 pommes");
  112. }
  113. TEST_CASE("[OptimizedTranslation] Generate from Translation and read messages") {
  114. Ref<Translation> translation = memnew(Translation);
  115. translation->set_locale("fr");
  116. translation->add_message("Hello", "Bonjour");
  117. translation->add_message("Hello2", "Bonjour2");
  118. translation->add_message("Hello3", "Bonjour3");
  119. Ref<OptimizedTranslation> optimized_translation = memnew(OptimizedTranslation);
  120. optimized_translation->generate(translation);
  121. CHECK(optimized_translation->get_message("Hello") == "Bonjour");
  122. CHECK(optimized_translation->get_message("Hello2") == "Bonjour2");
  123. CHECK(optimized_translation->get_message("Hello3") == "Bonjour3");
  124. CHECK(optimized_translation->get_message("DoesNotExist") == "");
  125. List<StringName> messages;
  126. // `get_message_list()` can't return the list of messages stored in an OptimizedTranslation.
  127. optimized_translation->get_message_list(&messages);
  128. CHECK(optimized_translation->get_message_count() == 0);
  129. CHECK(messages.size() == 0);
  130. }
  131. #ifdef TOOLS_ENABLED
  132. TEST_CASE("[Translation] CSV import") {
  133. Ref<ResourceImporterCSVTranslation> import_csv_translation = memnew(ResourceImporterCSVTranslation);
  134. Map<StringName, Variant> options;
  135. options["compress"] = false;
  136. options["delimiter"] = 0;
  137. List<String> gen_files;
  138. Error result = import_csv_translation->import(TestUtils::get_data_path("translations.csv"),
  139. "", options, nullptr, &gen_files);
  140. CHECK(result == OK);
  141. CHECK(gen_files.size() == 2);
  142. for (const String &file : gen_files) {
  143. Ref<Translation> translation = ResourceLoader::load(file);
  144. CHECK(translation.is_valid());
  145. TranslationServer::get_singleton()->add_translation(translation);
  146. }
  147. TranslationServer::get_singleton()->set_locale("de");
  148. CHECK(Object().tr("GOOD_MORNING", "") == "Guten Morgen");
  149. }
  150. #endif // TOOLS_ENABLED
  151. } // namespace TestTranslation
  152. #endif // TEST_TRANSLATION_H