Debian Codegraph @

tests/core/string/test_translation.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_translation.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
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 // The message no longer exists, so it returns an empty string instead.
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 // Messages are stored in a Map, don't assume ordering.
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 // Only remove the message for the default context, not the "friendly" context.
85 translation->erase_message("Hello");
86 // The message no longer exists, so it returns an empty string instead.
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 // Messages are stored in a Map, don't assume ordering.
106 CHECK(messages.find("Hello2"));
107 CHECK(messages.find("Hello3"));
108 // Context and untranslated string are separated by EOT.
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 // This is invalid, as the number passed to `get_plural_message()` may not be negative.
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 // `get_message_list()` can't return the list of messages stored in an OptimizedTranslation.
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"); // Left blank in CSV, should source from 'en'.
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 /* FIXME: This passes, but triggers a chain reaction that makes test_viewport
254 * and test_text_edit explode in a billion glittery Unicode particles.
255 td->set_locale_override("fa");
256
257 CHECK(td->translate("GOOD_MORNING", String()) == String::utf8("صبح بخیر"));
258 CHECK(td->translate("GOOD_EVENING", String()) == String::utf8("عصر بخیر"));
259 */
260
261 TranslationServer::get_singleton()->remove_domain("godot.test");
262 }
263 #endif // TOOLS_ENABLED
264
265 } // namespace TestTranslation