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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/*************************************************************************/
/* test_translation.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef TEST_TRANSLATION_H
#define TEST_TRANSLATION_H
#include "core/string/optimized_translation.h"
#include "core/string/translation.h"
#include "core/string/translation_po.h"
#ifdef TOOLS_ENABLED
#include "editor/import/resource_importer_csv_translation.h"
#endif
#include "tests/test_macros.h"
#include "tests/test_utils.h"
namespace TestTranslation {
TEST_CASE("[Translation] Messages") {
Ref<Translation> translation = memnew(Translation);
translation->set_locale("fr");
translation->add_message("Hello", "Bonjour");
CHECK(translation->get_message("Hello") == "Bonjour");
translation->erase_message("Hello");
// The message no longer exists, so it returns an empty string instead.
CHECK(translation->get_message("Hello") == "");
List<StringName> messages;
translation->get_message_list(&messages);
CHECK(translation->get_message_count() == 0);
CHECK(messages.size() == 0);
translation->add_message("Hello2", "Bonjour2");
translation->add_message("Hello3", "Bonjour3");
messages.clear();
translation->get_message_list(&messages);
CHECK(translation->get_message_count() == 2);
CHECK(messages.size() == 2);
// Messages are stored in a Map, don't assume ordering.
CHECK(messages.find("Hello2"));
CHECK(messages.find("Hello3"));
}
TEST_CASE("[TranslationPO] Messages with context") {
Ref<TranslationPO> translation = memnew(TranslationPO);
translation->set_locale("fr");
translation->add_message("Hello", "Bonjour");
translation->add_message("Hello", "Salut", "friendly");
CHECK(translation->get_message("Hello") == "Bonjour");
CHECK(translation->get_message("Hello", "friendly") == "Salut");
CHECK(translation->get_message("Hello", "nonexistent_context") == "");
// Only remove the message for the default context, not the "friendly" context.
translation->erase_message("Hello");
// The message no longer exists, so it returns an empty string instead.
CHECK(translation->get_message("Hello") == "");
CHECK(translation->get_message("Hello", "friendly") == "Salut");
CHECK(translation->get_message("Hello", "nonexistent_context") == "");
List<StringName> messages;
translation->get_message_list(&messages);
// `get_message_count()` takes all contexts into account.
CHECK(translation->get_message_count() == 1);
// Only the default context is taken into account.
// Since "Hello" is now only present in a non-default context, it is not counted in the list of messages.
CHECK(messages.size() == 0);
translation->add_message("Hello2", "Bonjour2");
translation->add_message("Hello2", "Salut2", "friendly");
translation->add_message("Hello3", "Bonjour3");
messages.clear();
translation->get_message_list(&messages);
// `get_message_count()` takes all contexts into account.
CHECK(translation->get_message_count() == 4);
// Only the default context is taken into account.
CHECK(messages.size() == 2);
// Messages are stored in a Map, don't assume ordering.
CHECK(messages.find("Hello2"));
CHECK(messages.find("Hello3"));
}
TEST_CASE("[TranslationPO] Plural messages") {
Ref<TranslationPO> translation = memnew(TranslationPO);
translation->set_locale("fr");
translation->set_plural_rule("Plural-Forms: nplurals=2; plural=(n >= 2);");
CHECK(translation->get_plural_forms() == 2);
PackedStringArray plurals;
plurals.push_back("Il y a %d pomme");
plurals.push_back("Il y a %d pommes");
translation->add_plural_message("There are %d apples", plurals);
ERR_PRINT_OFF;
// This is invalid, as the number passed to `get_plural_message()` may not be negative.
CHECK(vformat(translation->get_plural_message("There are %d apples", "", -1), -1) == "");
ERR_PRINT_ON;
CHECK(vformat(translation->get_plural_message("There are %d apples", "", 0), 0) == "Il y a 0 pomme");
CHECK(vformat(translation->get_plural_message("There are %d apples", "", 1), 1) == "Il y a 1 pomme");
CHECK(vformat(translation->get_plural_message("There are %d apples", "", 2), 2) == "Il y a 2 pommes");
}
TEST_CASE("[OptimizedTranslation] Generate from Translation and read messages") {
Ref<Translation> translation = memnew(Translation);
translation->set_locale("fr");
translation->add_message("Hello", "Bonjour");
translation->add_message("Hello2", "Bonjour2");
translation->add_message("Hello3", "Bonjour3");
Ref<OptimizedTranslation> optimized_translation = memnew(OptimizedTranslation);
optimized_translation->generate(translation);
CHECK(optimized_translation->get_message("Hello") == "Bonjour");
CHECK(optimized_translation->get_message("Hello2") == "Bonjour2");
CHECK(optimized_translation->get_message("Hello3") == "Bonjour3");
CHECK(optimized_translation->get_message("DoesNotExist") == "");
List<StringName> messages;
// `get_message_list()` can't return the list of messages stored in an OptimizedTranslation.
optimized_translation->get_message_list(&messages);
CHECK(optimized_translation->get_message_count() == 0);
CHECK(messages.size() == 0);
}
#ifdef TOOLS_ENABLED
TEST_CASE("[Translation] CSV import") {
Ref<ResourceImporterCSVTranslation> import_csv_translation = memnew(ResourceImporterCSVTranslation);
Map<StringName, Variant> options;
options["compress"] = false;
options["delimiter"] = 0;
List<String> gen_files;
Error result = import_csv_translation->import(TestUtils::get_data_path("translations.csv"),
"", options, nullptr, &gen_files);
CHECK(result == OK);
CHECK(gen_files.size() == 2);
for (const String &file : gen_files) {
Ref<Translation> translation = ResourceLoader::load(file);
CHECK(translation.is_valid());
TranslationServer::get_singleton()->add_translation(translation);
}
TranslationServer::get_singleton()->set_locale("de");
CHECK(Object().tr("GOOD_MORNING", "") == "Guten Morgen");
}
#endif // TOOLS_ENABLED
} // namespace TestTranslation
#endif // TEST_TRANSLATION_H
|