test_translation_server.h 6.8 KB

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