test_translation_server.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**************************************************************************/
  2. /* test_translation_server.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_SERVER_H
  31. #define TEST_TRANSLATION_SERVER_H
  32. #include "core/string/translation_server.h"
  33. #include "tests/test_macros.h"
  34. namespace TestTranslationServer {
  35. TEST_CASE("[TranslationServer] Translation operations") {
  36. Ref<Translation> t1 = memnew(Translation);
  37. t1->set_locale("uk");
  38. t1->add_message("Good Morning", String(U"Добрий ранок"));
  39. Ref<Translation> t2 = memnew(Translation);
  40. t2->set_locale("uk");
  41. t2->add_message("Hello Godot", String(U"你好戈多"));
  42. TranslationServer *ts = TranslationServer::get_singleton();
  43. // Adds translation for UK locale for the first time.
  44. int l_count_before = ts->get_loaded_locales().size();
  45. ts->add_translation(t1);
  46. int l_count_after = ts->get_loaded_locales().size();
  47. CHECK(l_count_after > l_count_before);
  48. // Adds translation for UK locale again.
  49. ts->add_translation(t2);
  50. CHECK_EQ(ts->get_loaded_locales().size(), l_count_after);
  51. // Removing that translation.
  52. ts->remove_translation(t2);
  53. CHECK_EQ(ts->get_loaded_locales().size(), l_count_after);
  54. CHECK(ts->get_translation_object("uk").is_valid());
  55. ts->set_locale("uk");
  56. CHECK(ts->translate("Good Morning") == String::utf8("Добрий ранок"));
  57. ts->remove_translation(t1);
  58. CHECK(ts->get_translation_object("uk").is_null());
  59. // If no suitable Translation object has been found - the original message should be returned.
  60. CHECK(ts->translate("Good Morning") == "Good Morning");
  61. }
  62. TEST_CASE("[TranslationServer] Locale operations") {
  63. TranslationServer *ts = TranslationServer::get_singleton();
  64. // Language variant test; we supplied the variant of Español and the result should be the same string.
  65. String loc = "es_Hani_ES_tradnl";
  66. String res = ts->standardize_locale(loc);
  67. CHECK(res == loc);
  68. // No such variant in variant_map; should return everything except the variant.
  69. loc = "es_Hani_ES_missing";
  70. res = ts->standardize_locale(loc);
  71. CHECK(res == "es_Hani_ES");
  72. // Non-ISO language name check (Windows issue).
  73. loc = "iw_Hani_IL";
  74. res = ts->standardize_locale(loc);
  75. CHECK(res == "he_Hani_IL");
  76. // Country rename check.
  77. loc = "uk_Hani_UK";
  78. res = ts->standardize_locale(loc);
  79. CHECK(res == "uk_Hani_GB");
  80. // Supplying a script name that is not in the list.
  81. loc = "de_Wrong_DE";
  82. res = ts->standardize_locale(loc);
  83. CHECK(res == "de_DE");
  84. }
  85. TEST_CASE("[TranslationServer] Comparing locales") {
  86. TranslationServer *ts = TranslationServer::get_singleton();
  87. String locale_a = "es";
  88. String locale_b = "es";
  89. // Exact match check.
  90. int res = ts->compare_locales(locale_a, locale_b);
  91. CHECK(res == 10);
  92. locale_a = "sr-Latn-CS";
  93. locale_b = "sr-Latn-RS";
  94. // Script matches (+1) but country doesn't (-1).
  95. res = ts->compare_locales(locale_a, locale_b);
  96. CHECK(res == 5);
  97. locale_a = "uz-Cyrl-UZ";
  98. locale_b = "uz-Latn-UZ";
  99. // Country matches (+1) but script doesn't (-1).
  100. res = ts->compare_locales(locale_a, locale_b);
  101. CHECK(res == 5);
  102. locale_a = "aa-Latn-ER";
  103. locale_b = "aa-Latn-ER-saaho";
  104. // Script and country match (+2) with variant on one locale (+0).
  105. res = ts->compare_locales(locale_a, locale_b);
  106. CHECK(res == 7);
  107. locale_a = "uz-Cyrl-UZ";
  108. locale_b = "uz-Latn-KG";
  109. // Both script and country mismatched (-2).
  110. res = ts->compare_locales(locale_a, locale_b);
  111. CHECK(res == 3);
  112. locale_a = "es-ES";
  113. locale_b = "es-AR";
  114. // Mismatched country (-1).
  115. res = ts->compare_locales(locale_a, locale_b);
  116. CHECK(res == 4);
  117. locale_a = "es";
  118. locale_b = "es-AR";
  119. // No country for one locale (+0).
  120. res = ts->compare_locales(locale_a, locale_b);
  121. CHECK(res == 5);
  122. locale_a = "es-EC";
  123. locale_b = "fr-LU";
  124. // No match.
  125. res = ts->compare_locales(locale_a, locale_b);
  126. CHECK(res == 0);
  127. locale_a = "zh-HK";
  128. locale_b = "zh";
  129. // In full standardization, zh-HK becomes zh_Hant_HK and zh becomes
  130. // zh_Hans_CN. Both script and country mismatch (-2).
  131. res = ts->compare_locales(locale_a, locale_b);
  132. CHECK(res == 3);
  133. locale_a = "zh-CN";
  134. locale_b = "zh";
  135. // In full standardization, zh and zh-CN both become zh_Hans_CN for an
  136. // exact match.
  137. res = ts->compare_locales(locale_a, locale_b);
  138. CHECK(res == 10);
  139. }
  140. } // namespace TestTranslationServer
  141. #endif // TEST_TRANSLATION_SERVER_H