test_translation_server.h 7.9 KB

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