| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/string/optimized_translation.h" |
| 34 |
#include "core/string/plural_rules.h" |
| 35 |
#include "core/string/translation.h" |
| 36 |
#include "core/string/translation_server.h" |
| 37 |
|
| 38 |
#ifdef TOOLS_ENABLED |
| 39 |
#include "editor/import/resource_importer_csv_translation.h" |
| 40 |
#endif |
| 41 |
|
| 42 |
#include "tests/test_macros.h" |
| 43 |
#include "tests/test_utils.h" |
| 44 |
|
| 45 |
namespace TestTranslation { |
| 46 |
|
| 47 |
TEST_CASE("[Translation] Messages") { |
| 48 |
Ref<Translation> translation; |
| 49 |
translation.instantiate(); |
| 50 |
translation->set_locale("fr"); |
| 51 |
translation->add_message("Hello", "Bonjour"); |
| 52 |
CHECK(translation->get_message("Hello") == "Bonjour"); |
| 53 |
|
| 54 |
translation->erase_message("Hello"); |
| 55 |
|
| 56 |
CHECK(translation->get_message("Hello") == ""); |
| 57 |
|
| 58 |
List<StringName> messages; |
| 59 |
translation->get_message_list(&messages); |
| 60 |
CHECK(translation->get_message_count() == 0); |
| 61 |
CHECK(messages.size() == 0); |
| 62 |
|
| 63 |
translation->add_message("Hello2", "Bonjour2"); |
| 64 |
translation->add_message("Hello3", "Bonjour3"); |
| 65 |
messages.clear(); |
| 66 |
translation->get_message_list(&messages); |
| 67 |
CHECK(translation->get_message_count() == 2); |
| 68 |
CHECK(messages.size() == 2); |
| 69 |
|
| 70 |
CHECK(messages.find("Hello2")); |
| 71 |
CHECK(messages.find("Hello3")); |
| 72 |
} |
| 73 |
|
| 74 |
TEST_CASE("[Translation] Messages with context") { |
| 75 |
Ref<Translation> translation; |
| 76 |
translation.instantiate(); |
| 77 |
translation->set_locale("fr"); |
| 78 |
translation->add_message("Hello", "Bonjour"); |
| 79 |
translation->add_message("Hello", "Salut", "friendly"); |
| 80 |
CHECK(translation->get_message("Hello") == "Bonjour"); |
| 81 |
CHECK(translation->get_message("Hello", "friendly") == "Salut"); |
| 82 |
CHECK(translation->get_message("Hello", "nonexistent_context") == ""); |
| 83 |
|
| 84 |
|
| 85 |
translation->erase_message("Hello"); |
| 86 |
|
| 87 |
CHECK(translation->get_message("Hello") == ""); |
| 88 |
CHECK(translation->get_message("Hello", "friendly") == "Salut"); |
| 89 |
CHECK(translation->get_message("Hello", "nonexistent_context") == ""); |
| 90 |
|
| 91 |
List<StringName> messages; |
| 92 |
translation->get_message_list(&messages); |
| 93 |
|
| 94 |
CHECK(translation->get_message_count() == 1); |
| 95 |
CHECK(messages.size() == 1); |
| 96 |
|
| 97 |
translation->add_message("Hello2", "Bonjour2"); |
| 98 |
translation->add_message("Hello2", "Salut2", "friendly"); |
| 99 |
translation->add_message("Hello3", "Bonjour3"); |
| 100 |
messages.clear(); |
| 101 |
translation->get_message_list(&messages); |
| 102 |
|
| 103 |
CHECK(translation->get_message_count() == 4); |
| 104 |
CHECK(messages.size() == 4); |
| 105 |
|
| 106 |
CHECK(messages.find("Hello2")); |
| 107 |
CHECK(messages.find("Hello3")); |
| 108 |
|
| 109 |
CHECK(messages.find("friendly\x04Hello2")); |
| 110 |
} |
| 111 |
|
| 112 |
TEST_CASE("[Translation] Plural messages") { |
| 113 |
{ |
| 114 |
Ref<Translation> translation; |
| 115 |
translation.instantiate(); |
| 116 |
translation->set_locale("fr"); |
| 117 |
CHECK(translation->get_nplurals() == 2); |
| 118 |
} |
| 119 |
|
| 120 |
{ |
| 121 |
Ref<Translation> translation; |
| 122 |
translation.instantiate(); |
| 123 |
translation->set_locale("invalid"); |
| 124 |
CHECK(translation->get_nplurals() == 2); |
| 125 |
} |
| 126 |
|
| 127 |
{ |
| 128 |
Ref<Translation> translation; |
| 129 |
translation.instantiate(); |
| 130 |
translation->set_plural_rules_override("Plural-Forms: nplurals=2; plural=(n >= 2);"); |
| 131 |
CHECK(translation->get_nplurals() == 2); |
| 132 |
|
| 133 |
PackedStringArray plurals; |
| 134 |
plurals.push_back("Il y a %d pomme"); |
| 135 |
plurals.push_back("Il y a %d pommes"); |
| 136 |
translation->add_plural_message("There are %d apples", plurals); |
| 137 |
ERR_PRINT_OFF; |
| 138 |
|
| 139 |
CHECK(vformat(translation->get_plural_message("There are %d apples", "", -1), -1) == ""); |
| 140 |
ERR_PRINT_ON; |
| 141 |
CHECK(vformat(translation->get_plural_message("There are %d apples", "", 0), 0) == "Il y a 0 pomme"); |
| 142 |
CHECK(vformat(translation->get_plural_message("There are %d apples", "", 1), 1) == "Il y a 1 pomme"); |
| 143 |
CHECK(vformat(translation->get_plural_message("There are %d apples", "", 2), 2) == "Il y a 2 pommes"); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
TEST_CASE("[Translation] Plural rules parsing") { |
| 148 |
ERR_PRINT_OFF; |
| 149 |
{ |
| 150 |
CHECK(PluralRules::parse("") == nullptr); |
| 151 |
|
| 152 |
CHECK(PluralRules::parse("plurals=(n != 1);") == nullptr); |
| 153 |
CHECK(PluralRules::parse("nplurals; plurals=(n != 1);") == nullptr); |
| 154 |
CHECK(PluralRules::parse("nplurals=; plurals=(n != 1);") == nullptr); |
| 155 |
CHECK(PluralRules::parse("nplurals=0; plurals=(n != 1);") == nullptr); |
| 156 |
CHECK(PluralRules::parse("nplurals=-1; plurals=(n != 1);") == nullptr); |
| 157 |
|
| 158 |
CHECK(PluralRules::parse("nplurals=2;") == nullptr); |
| 159 |
CHECK(PluralRules::parse("nplurals=2; plurals;") == nullptr); |
| 160 |
CHECK(PluralRules::parse("nplurals=2; plurals=;") == nullptr); |
| 161 |
} |
| 162 |
ERR_PRINT_ON; |
| 163 |
|
| 164 |
{ |
| 165 |
PluralRules *pr = PluralRules::parse("nplurals=3; plural=(n==0 ? 0 : n==1 ? 1 : 2);"); |
| 166 |
REQUIRE(pr != nullptr); |
| 167 |
|
| 168 |
CHECK(pr->get_nplurals() == 3); |
| 169 |
CHECK(pr->get_plural() == "(n==0 ? 0 : n==1 ? 1 : 2)"); |
| 170 |
|
| 171 |
CHECK(pr->evaluate(0) == 0); |
| 172 |
CHECK(pr->evaluate(1) == 1); |
| 173 |
CHECK(pr->evaluate(2) == 2); |
| 174 |
CHECK(pr->evaluate(3) == 2); |
| 175 |
|
| 176 |
memdelete(pr); |
| 177 |
} |
| 178 |
|
| 179 |
{ |
| 180 |
PluralRules *pr = PluralRules::parse("nplurals=1; plural=0;"); |
| 181 |
REQUIRE(pr != nullptr); |
| 182 |
|
| 183 |
CHECK(pr->get_nplurals() == 1); |
| 184 |
CHECK(pr->get_plural() == "0"); |
| 185 |
|
| 186 |
CHECK(pr->evaluate(0) == 0); |
| 187 |
CHECK(pr->evaluate(1) == 0); |
| 188 |
CHECK(pr->evaluate(2) == 0); |
| 189 |
CHECK(pr->evaluate(3) == 0); |
| 190 |
|
| 191 |
memdelete(pr); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
#ifdef TOOLS_ENABLED |
| 196 |
TEST_CASE("[OptimizedTranslation] Generate from Translation and read messages") { |
| 197 |
Ref<Translation> translation = memnew(Translation); |
| 198 |
translation->set_locale("fr"); |
| 199 |
translation->add_message("Hello", "Bonjour"); |
| 200 |
translation->add_message("Hello2", "Bonjour2"); |
| 201 |
translation->add_message("Hello3", "Bonjour3"); |
| 202 |
|
| 203 |
Ref<OptimizedTranslation> optimized_translation = memnew(OptimizedTranslation); |
| 204 |
optimized_translation->generate(translation); |
| 205 |
CHECK(optimized_translation->get_message("Hello") == "Bonjour"); |
| 206 |
CHECK(optimized_translation->get_message("Hello2") == "Bonjour2"); |
| 207 |
CHECK(optimized_translation->get_message("Hello3") == "Bonjour3"); |
| 208 |
CHECK(optimized_translation->get_message("DoesNotExist") == ""); |
| 209 |
|
| 210 |
List<StringName> messages; |
| 211 |
|
| 212 |
optimized_translation->get_message_list(&messages); |
| 213 |
CHECK(optimized_translation->get_message_count() == 0); |
| 214 |
CHECK(messages.size() == 0); |
| 215 |
} |
| 216 |
|
| 217 |
TEST_CASE("[TranslationCSV] CSV import") { |
| 218 |
Ref<ResourceImporterCSVTranslation> import_csv_translation = memnew(ResourceImporterCSVTranslation); |
| 219 |
|
| 220 |
HashMap<StringName, Variant> options; |
| 221 |
options["compress"] = false; |
| 222 |
options["delimiter"] = 0; |
| 223 |
|
| 224 |
List<String> gen_files; |
| 225 |
|
| 226 |
Error result = import_csv_translation->import(0, TestUtils::get_data_path("translations.csv"), |
| 227 |
"", options, nullptr, &gen_files); |
| 228 |
CHECK(result == OK); |
| 229 |
CHECK(gen_files.size() == 4); |
| 230 |
|
| 231 |
Ref<TranslationDomain> td = TranslationServer::get_singleton()->get_or_add_domain("godot.test"); |
| 232 |
for (const String &file : gen_files) { |
| 233 |
Ref<Translation> translation = ResourceLoader::load(file); |
| 234 |
CHECK(translation.is_valid()); |
| 235 |
td->add_translation(translation); |
| 236 |
} |
| 237 |
|
| 238 |
td->set_locale_override("en"); |
| 239 |
|
| 240 |
CHECK(td->translate("GOOD_MORNING", StringName()) == "Good Morning"); |
| 241 |
CHECK(td->translate("GOOD_EVENING", StringName()) == "Good Evening"); |
| 242 |
|
| 243 |
td->set_locale_override("de"); |
| 244 |
|
| 245 |
CHECK(td->translate("GOOD_MORNING", StringName()) == "Guten Morgen"); |
| 246 |
CHECK(td->translate("GOOD_EVENING", StringName()) == "Good Evening"); |
| 247 |
|
| 248 |
td->set_locale_override("ja"); |
| 249 |
|
| 250 |
CHECK(td->translate("GOOD_MORNING", StringName()) == String::utf8("おはよう")); |
| 251 |
CHECK(td->translate("GOOD_EVENING", StringName()) == String::utf8("こんばんは")); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
یر")); |
| 258 |
یر")); |
| 259 |
|
| 260 |
|
| 261 |
TranslationServer::get_singleton()->remove_domain("godot.test"); |
| 262 |
} |
| 263 |
#endif |
| 264 |
|
| 265 |
} |