Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/string/test_translation.cpp
45997 views
1
/**************************************************************************/
2
/* test_translation.cpp */
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
#include "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_translation)
34
35
#include "core/io/resource_loader.h"
36
#include "core/string/optimized_translation.h"
37
#include "core/string/plural_rules.h"
38
#include "core/string/translation.h"
39
#include "core/string/translation_server.h"
40
41
#ifdef TOOLS_ENABLED
42
#include "editor/import/resource_importer_csv_translation.h"
43
#endif
44
45
#include "tests/test_utils.h"
46
47
namespace TestTranslation {
48
49
TEST_CASE("[Translation] Messages") {
50
Ref<Translation> translation;
51
translation.instantiate();
52
translation->set_locale("fr");
53
translation->add_message("Hello", "Bonjour");
54
CHECK(translation->get_message("Hello") == "Bonjour");
55
56
translation->erase_message("Hello");
57
// The message no longer exists, so it returns an empty string instead.
58
CHECK(translation->get_message("Hello") == "");
59
60
List<StringName> messages;
61
translation->get_message_list(&messages);
62
CHECK(translation->get_message_count() == 0);
63
CHECK(messages.size() == 0);
64
65
translation->add_message("Hello2", "Bonjour2");
66
translation->add_message("Hello3", "Bonjour3");
67
messages.clear();
68
translation->get_message_list(&messages);
69
CHECK(translation->get_message_count() == 2);
70
CHECK(messages.size() == 2);
71
// Messages are stored in a Map, don't assume ordering.
72
CHECK(messages.find("Hello2"));
73
CHECK(messages.find("Hello3"));
74
}
75
76
TEST_CASE("[Translation] Messages with context") {
77
Ref<Translation> translation;
78
translation.instantiate();
79
translation->set_locale("fr");
80
translation->add_message("Hello", "Bonjour");
81
translation->add_message("Hello", "Salut", "friendly");
82
CHECK(translation->get_message("Hello") == "Bonjour");
83
CHECK(translation->get_message("Hello", "friendly") == "Salut");
84
CHECK(translation->get_message("Hello", "nonexistent_context") == "");
85
86
// Only remove the message for the default context, not the "friendly" context.
87
translation->erase_message("Hello");
88
// The message no longer exists, so it returns an empty string instead.
89
CHECK(translation->get_message("Hello") == "");
90
CHECK(translation->get_message("Hello", "friendly") == "Salut");
91
CHECK(translation->get_message("Hello", "nonexistent_context") == "");
92
93
List<StringName> messages;
94
translation->get_message_list(&messages);
95
96
CHECK(translation->get_message_count() == 1);
97
CHECK(messages.size() == 1);
98
99
translation->add_message("Hello2", "Bonjour2");
100
translation->add_message("Hello2", "Salut2", "friendly");
101
translation->add_message("Hello3", "Bonjour3");
102
messages.clear();
103
translation->get_message_list(&messages);
104
105
CHECK(translation->get_message_count() == 4);
106
CHECK(messages.size() == 4);
107
// Messages are stored in a Map, don't assume ordering.
108
CHECK(messages.find("Hello2"));
109
CHECK(messages.find("Hello3"));
110
// Context and untranslated string are separated by EOT.
111
CHECK(messages.find("friendly\x04Hello2"));
112
}
113
114
TEST_CASE("[Translation] Plural messages") {
115
{
116
Ref<Translation> translation;
117
translation.instantiate();
118
translation->set_locale("fr");
119
CHECK(translation->get_nplurals() == 2);
120
}
121
122
{
123
Ref<Translation> translation;
124
translation.instantiate();
125
translation->set_locale("invalid");
126
CHECK(translation->get_nplurals() == 2);
127
}
128
129
{
130
Ref<Translation> translation;
131
translation.instantiate();
132
translation->set_plural_rules_override("Plural-Forms: nplurals=2; plural=(n >= 2);");
133
CHECK(translation->get_nplurals() == 2);
134
135
PackedStringArray plurals;
136
plurals.push_back("Il y a %d pomme");
137
plurals.push_back("Il y a %d pommes");
138
translation->add_plural_message("There are %d apples", plurals);
139
ERR_PRINT_OFF;
140
// This is invalid, as the number passed to `get_plural_message()` may not be negative.
141
CHECK(vformat(translation->get_plural_message("There are %d apples", "", -1), -1) == "");
142
ERR_PRINT_ON;
143
CHECK(vformat(translation->get_plural_message("There are %d apples", "", 0), 0) == "Il y a 0 pomme");
144
CHECK(vformat(translation->get_plural_message("There are %d apples", "", 1), 1) == "Il y a 1 pomme");
145
CHECK(vformat(translation->get_plural_message("There are %d apples", "", 2), 2) == "Il y a 2 pommes");
146
}
147
}
148
149
TEST_CASE("[Translation] Plural rules parsing") {
150
ERR_PRINT_OFF;
151
{
152
CHECK(PluralRules::parse("") == nullptr);
153
154
CHECK(PluralRules::parse("plurals=(n != 1);") == nullptr);
155
CHECK(PluralRules::parse("nplurals; plurals=(n != 1);") == nullptr);
156
CHECK(PluralRules::parse("nplurals=; plurals=(n != 1);") == nullptr);
157
CHECK(PluralRules::parse("nplurals=0; plurals=(n != 1);") == nullptr);
158
CHECK(PluralRules::parse("nplurals=-1; plurals=(n != 1);") == nullptr);
159
160
CHECK(PluralRules::parse("nplurals=2;") == nullptr);
161
CHECK(PluralRules::parse("nplurals=2; plurals;") == nullptr);
162
CHECK(PluralRules::parse("nplurals=2; plurals=;") == nullptr);
163
}
164
ERR_PRINT_ON;
165
166
{
167
PluralRules *pr = PluralRules::parse("nplurals=3; plural=(n==0 ? 0 : n==1 ? 1 : 2);");
168
REQUIRE(pr != nullptr);
169
170
CHECK(pr->get_nplurals() == 3);
171
CHECK(pr->get_plural() == "(n==0 ? 0 : n==1 ? 1 : 2)");
172
173
CHECK(pr->evaluate(0) == 0);
174
CHECK(pr->evaluate(1) == 1);
175
CHECK(pr->evaluate(2) == 2);
176
CHECK(pr->evaluate(3) == 2);
177
178
memdelete(pr);
179
}
180
181
{
182
PluralRules *pr = PluralRules::parse("nplurals=1; plural=0;");
183
REQUIRE(pr != nullptr);
184
185
CHECK(pr->get_nplurals() == 1);
186
CHECK(pr->get_plural() == "0");
187
188
CHECK(pr->evaluate(0) == 0);
189
CHECK(pr->evaluate(1) == 0);
190
CHECK(pr->evaluate(2) == 0);
191
CHECK(pr->evaluate(3) == 0);
192
193
memdelete(pr);
194
}
195
}
196
197
#ifdef TOOLS_ENABLED
198
TEST_CASE("[OptimizedTranslation] Generate from Translation and read messages") {
199
Ref<Translation> translation = memnew(Translation);
200
translation->set_locale("fr");
201
translation->add_message("Hello", "Bonjour");
202
translation->add_message("Hello2", "Bonjour2");
203
translation->add_message("Hello3", "Bonjour3");
204
205
Ref<OptimizedTranslation> optimized_translation = memnew(OptimizedTranslation);
206
optimized_translation->generate(translation);
207
CHECK(optimized_translation->get_message("Hello") == "Bonjour");
208
CHECK(optimized_translation->get_message("Hello2") == "Bonjour2");
209
CHECK(optimized_translation->get_message("Hello3") == "Bonjour3");
210
CHECK(optimized_translation->get_message("DoesNotExist") == "");
211
212
List<StringName> messages;
213
// `get_message_list()` can't return the list of messages stored in an OptimizedTranslation.
214
optimized_translation->get_message_list(&messages);
215
CHECK(optimized_translation->get_message_count() == 0);
216
CHECK(messages.size() == 0);
217
}
218
219
TEST_CASE("[TranslationCSV] CSV import") {
220
Ref<ResourceImporterCSVTranslation> import_csv_translation = memnew(ResourceImporterCSVTranslation);
221
222
HashMap<StringName, Variant> options;
223
options["compress"] = false;
224
options["delimiter"] = 0;
225
226
List<String> gen_files;
227
228
Error result = import_csv_translation->import(0, TestUtils::get_data_path("translations.csv"),
229
"", options, nullptr, &gen_files);
230
CHECK(result == OK);
231
CHECK(gen_files.size() == 4);
232
233
Ref<TranslationDomain> td = TranslationServer::get_singleton()->get_or_add_domain("godot.test");
234
for (const String &file : gen_files) {
235
Ref<Translation> translation = ResourceLoader::load(file);
236
CHECK(translation.is_valid());
237
td->add_translation(translation);
238
}
239
240
td->set_locale_override("en");
241
242
CHECK(td->translate("GOOD_MORNING", StringName()) == "Good Morning");
243
CHECK(td->translate("GOOD_EVENING", StringName()) == "Good Evening");
244
245
td->set_locale_override("de");
246
247
CHECK(td->translate("GOOD_MORNING", StringName()) == "Guten Morgen");
248
CHECK(td->translate("GOOD_EVENING", StringName()) == "Good Evening"); // Left blank in CSV, should source from 'en'.
249
250
td->set_locale_override("ja");
251
252
CHECK(td->translate("GOOD_MORNING", StringName()) == String::utf8("おはよう"));
253
CHECK(td->translate("GOOD_EVENING", StringName()) == String::utf8("こんばんは"));
254
255
/* FIXME: This passes, but triggers a chain reaction that makes test_viewport
256
* and test_text_edit explode in a billion glittery Unicode particles.
257
td->set_locale_override("fa");
258
259
CHECK(td->translate("GOOD_MORNING", String()) == String::utf8("صبح بخیر"));
260
CHECK(td->translate("GOOD_EVENING", String()) == String::utf8("عصر بخیر"));
261
*/
262
263
TranslationServer::get_singleton()->remove_domain("godot.test");
264
}
265
266
#endif // TOOLS_ENABLED
267
268
} // namespace TestTranslation
269
270