Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/string/test_translation.h
10278 views
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/translation.h"
35
#include "core/string/translation_po.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 = memnew(Translation);
49
translation->set_locale("fr");
50
translation->add_message("Hello", "Bonjour");
51
CHECK(translation->get_message("Hello") == "Bonjour");
52
53
translation->erase_message("Hello");
54
// The message no longer exists, so it returns an empty string instead.
55
CHECK(translation->get_message("Hello") == "");
56
57
List<StringName> messages;
58
translation->get_message_list(&messages);
59
CHECK(translation->get_message_count() == 0);
60
CHECK(messages.size() == 0);
61
62
translation->add_message("Hello2", "Bonjour2");
63
translation->add_message("Hello3", "Bonjour3");
64
messages.clear();
65
translation->get_message_list(&messages);
66
CHECK(translation->get_message_count() == 2);
67
CHECK(messages.size() == 2);
68
// Messages are stored in a Map, don't assume ordering.
69
CHECK(messages.find("Hello2"));
70
CHECK(messages.find("Hello3"));
71
}
72
73
TEST_CASE("[TranslationPO] Messages with context") {
74
Ref<TranslationPO> translation = memnew(TranslationPO);
75
translation->set_locale("fr");
76
translation->add_message("Hello", "Bonjour");
77
translation->add_message("Hello", "Salut", "friendly");
78
CHECK(translation->get_message("Hello") == "Bonjour");
79
CHECK(translation->get_message("Hello", "friendly") == "Salut");
80
CHECK(translation->get_message("Hello", "nonexistent_context") == "");
81
82
// Only remove the message for the default context, not the "friendly" context.
83
translation->erase_message("Hello");
84
// The message no longer exists, so it returns an empty string instead.
85
CHECK(translation->get_message("Hello") == "");
86
CHECK(translation->get_message("Hello", "friendly") == "Salut");
87
CHECK(translation->get_message("Hello", "nonexistent_context") == "");
88
89
List<StringName> messages;
90
translation->get_message_list(&messages);
91
92
// `get_message_count()` takes all contexts into account.
93
CHECK(translation->get_message_count() == 1);
94
// Only the default context is taken into account.
95
// Since "Hello" is now only present in a non-default context, it is not counted in the list of messages.
96
CHECK(messages.size() == 0);
97
98
translation->add_message("Hello2", "Bonjour2");
99
translation->add_message("Hello2", "Salut2", "friendly");
100
translation->add_message("Hello3", "Bonjour3");
101
messages.clear();
102
translation->get_message_list(&messages);
103
104
// `get_message_count()` takes all contexts into account.
105
CHECK(translation->get_message_count() == 4);
106
// Only the default context is taken into account.
107
CHECK(messages.size() == 2);
108
// Messages are stored in a Map, don't assume ordering.
109
CHECK(messages.find("Hello2"));
110
CHECK(messages.find("Hello3"));
111
}
112
113
TEST_CASE("[TranslationPO] Plural messages") {
114
Ref<TranslationPO> translation = memnew(TranslationPO);
115
translation->set_locale("fr");
116
translation->set_plural_rule("Plural-Forms: nplurals=2; plural=(n >= 2);");
117
CHECK(translation->get_plural_forms() == 2);
118
119
PackedStringArray plurals;
120
plurals.push_back("Il y a %d pomme");
121
plurals.push_back("Il y a %d pommes");
122
translation->add_plural_message("There are %d apples", plurals);
123
ERR_PRINT_OFF;
124
// This is invalid, as the number passed to `get_plural_message()` may not be negative.
125
CHECK(vformat(translation->get_plural_message("There are %d apples", "", -1), -1) == "");
126
ERR_PRINT_ON;
127
CHECK(vformat(translation->get_plural_message("There are %d apples", "", 0), 0) == "Il y a 0 pomme");
128
CHECK(vformat(translation->get_plural_message("There are %d apples", "", 1), 1) == "Il y a 1 pomme");
129
CHECK(vformat(translation->get_plural_message("There are %d apples", "", 2), 2) == "Il y a 2 pommes");
130
}
131
132
#ifdef TOOLS_ENABLED
133
TEST_CASE("[OptimizedTranslation] Generate from Translation and read messages") {
134
Ref<Translation> translation = memnew(Translation);
135
translation->set_locale("fr");
136
translation->add_message("Hello", "Bonjour");
137
translation->add_message("Hello2", "Bonjour2");
138
translation->add_message("Hello3", "Bonjour3");
139
140
Ref<OptimizedTranslation> optimized_translation = memnew(OptimizedTranslation);
141
optimized_translation->generate(translation);
142
CHECK(optimized_translation->get_message("Hello") == "Bonjour");
143
CHECK(optimized_translation->get_message("Hello2") == "Bonjour2");
144
CHECK(optimized_translation->get_message("Hello3") == "Bonjour3");
145
CHECK(optimized_translation->get_message("DoesNotExist") == "");
146
147
List<StringName> messages;
148
// `get_message_list()` can't return the list of messages stored in an OptimizedTranslation.
149
optimized_translation->get_message_list(&messages);
150
CHECK(optimized_translation->get_message_count() == 0);
151
CHECK(messages.size() == 0);
152
}
153
154
TEST_CASE("[TranslationCSV] CSV import") {
155
Ref<ResourceImporterCSVTranslation> import_csv_translation = memnew(ResourceImporterCSVTranslation);
156
157
HashMap<StringName, Variant> options;
158
options["compress"] = false;
159
options["delimiter"] = 0;
160
161
List<String> gen_files;
162
163
Error result = import_csv_translation->import(0, TestUtils::get_data_path("translations.csv"),
164
"", options, nullptr, &gen_files);
165
CHECK(result == OK);
166
CHECK(gen_files.size() == 4);
167
168
TranslationServer *ts = TranslationServer::get_singleton();
169
170
for (const String &file : gen_files) {
171
Ref<Translation> translation = ResourceLoader::load(file);
172
CHECK(translation.is_valid());
173
ts->add_translation(translation);
174
}
175
176
ts->set_locale("en");
177
178
// `tr` can be called on any Object, we reuse TranslationServer for convenience.
179
CHECK(ts->tr("GOOD_MORNING") == "Good Morning");
180
CHECK(ts->tr("GOOD_EVENING") == "Good Evening");
181
182
ts->set_locale("de");
183
184
CHECK(ts->tr("GOOD_MORNING") == "Guten Morgen");
185
CHECK(ts->tr("GOOD_EVENING") == "Good Evening"); // Left blank in CSV, should source from 'en'.
186
187
ts->set_locale("ja");
188
189
CHECK(ts->tr("GOOD_MORNING") == String::utf8("おはよう"));
190
CHECK(ts->tr("GOOD_EVENING") == String::utf8("こんばんは"));
191
192
/* FIXME: This passes, but triggers a chain reaction that makes test_viewport
193
* and test_text_edit explode in a billion glittery Unicode particles.
194
ts->set_locale("fa");
195
196
CHECK(ts->tr("GOOD_MORNING") == String::utf8("صبح بخیر"));
197
CHECK(ts->tr("GOOD_EVENING") == String::utf8("عصر بخیر"));
198
*/
199
}
200
#endif // TOOLS_ENABLED
201
202
} // namespace TestTranslation
203
204