Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/string/test_translation_server.cpp
45997 views
1
/**************************************************************************/
2
/* test_translation_server.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_server)
34
35
#include "core/string/translation_server.h"
36
37
namespace TestTranslationServer {
38
39
TEST_CASE("[TranslationServer] Translation operations") {
40
Ref<TranslationDomain> td = TranslationServer::get_singleton()->get_or_add_domain("godot.test");
41
CHECK(td->get_translations().is_empty());
42
43
Ref<Translation> t1 = memnew(Translation);
44
t1->set_locale("uk"); // Ukrainian.
45
t1->add_message("Good Morning", String(U"Добрий ранок"));
46
td->add_translation(t1);
47
CHECK(td->get_translations().size() == 1);
48
CHECK(td->has_translation_for_locale("uk", true));
49
CHECK(td->has_translation_for_locale("uk", false));
50
CHECK_FALSE(td->has_translation_for_locale("uk_UA", true));
51
CHECK(td->has_translation_for_locale("uk_UA", false));
52
CHECK(td->find_translations("uk", false).size() == 1);
53
CHECK(td->find_translations("uk", true).size() == 1);
54
CHECK(td->find_translations("uk_UA", false).size() == 1);
55
CHECK(td->find_translations("uk_UA", true).size() == 0);
56
57
Ref<Translation> t2 = memnew(Translation);
58
t2->set_locale("uk_UA"); // Ukrainian in Ukraine.
59
t2->add_message("Hello Godot", String(U"Привіт, Годо."));
60
td->add_translation(t2);
61
CHECK(td->get_translations().size() == 2);
62
CHECK(td->has_translation_for_locale("uk", true));
63
CHECK(td->has_translation_for_locale("uk", false));
64
CHECK(td->has_translation_for_locale("uk_UA", true));
65
CHECK(td->has_translation_for_locale("uk_UA", false));
66
CHECK(td->find_translations("uk", false).size() == 2);
67
CHECK(td->find_translations("uk", true).size() == 1);
68
CHECK(td->find_translations("uk_UA", false).size() == 2);
69
CHECK(td->find_translations("uk_UA", true).size() == 1);
70
71
td->set_locale_override("uk");
72
CHECK(td->translate("Good Morning", StringName()) == String::utf8("Добрий ранок"));
73
74
td->remove_translation(t1);
75
CHECK(td->get_translations().size() == 1);
76
CHECK_FALSE(td->has_translation_for_locale("uk", true));
77
CHECK(td->has_translation_for_locale("uk", false));
78
CHECK(td->has_translation_for_locale("uk_UA", true));
79
CHECK(td->has_translation_for_locale("uk_UA", false));
80
CHECK(td->find_translations("uk", true).size() == 0);
81
CHECK(td->find_translations("uk", false).size() == 1);
82
CHECK(td->find_translations("uk_UA", true).size() == 1);
83
CHECK(td->find_translations("uk_UA", false).size() == 1);
84
85
// If no suitable Translation object has been found - the original message should be returned.
86
CHECK(td->translate("Good Morning", StringName()) == "Good Morning");
87
88
TranslationServer::get_singleton()->remove_domain("godot.test");
89
}
90
91
TEST_CASE("[TranslationServer] Locale operations") {
92
TranslationServer *ts = TranslationServer::get_singleton();
93
94
// Language variant test; we supplied the variant of Español and the result should be the same string.
95
String loc = "es_Hani_ES_tradnl";
96
String res = ts->standardize_locale(loc);
97
98
CHECK(res == loc);
99
100
// No such variant in variant_map; should return everything except the variant.
101
loc = "es_Hani_ES_missing";
102
res = ts->standardize_locale(loc);
103
104
CHECK(res == "es_Hani_ES");
105
106
// Non-ISO language name check (Windows issue).
107
loc = "iw_Hani_IL";
108
res = ts->standardize_locale(loc);
109
110
CHECK(res == "he_Hani_IL");
111
112
// Country rename check.
113
loc = "uk_Hani_UK";
114
res = ts->standardize_locale(loc);
115
116
CHECK(res == "uk_Hani_GB");
117
118
// Supplying a script name that is not in the list.
119
loc = "de_Wrong_DE";
120
res = ts->standardize_locale(loc);
121
122
CHECK(res == "de_DE");
123
124
// No added defaults.
125
loc = "es_ES";
126
res = ts->standardize_locale(loc, true);
127
128
CHECK(res == "es_ES");
129
130
// Add default script.
131
loc = "az_AZ";
132
res = ts->standardize_locale(loc, true);
133
134
CHECK(res == "az_Latn_AZ");
135
136
// Add default country.
137
loc = "pa_Arab";
138
res = ts->standardize_locale(loc, true);
139
140
CHECK(res == "pa_Arab_PK");
141
142
// Add default script and country.
143
loc = "zh";
144
res = ts->standardize_locale(loc, true);
145
146
CHECK(res == "zh_Hans_CN");
147
148
// Explicitly don't add defaults.
149
loc = "zh";
150
res = ts->standardize_locale(loc, false);
151
152
CHECK(res == "zh");
153
}
154
155
TEST_CASE("[TranslationServer] Comparing locales") {
156
TranslationServer *ts = TranslationServer::get_singleton();
157
158
String locale_a = "es";
159
String locale_b = "es";
160
161
// Exact match check.
162
int res = ts->compare_locales(locale_a, locale_b);
163
164
CHECK(res == 10);
165
166
locale_a = "sr-Latn-CS";
167
locale_b = "sr-Latn-RS";
168
169
// Script matches (+1) but country doesn't (-1).
170
res = ts->compare_locales(locale_a, locale_b);
171
172
CHECK(res == 5);
173
174
locale_a = "uz-Cyrl-UZ";
175
locale_b = "uz-Latn-UZ";
176
177
// Country matches (+1) but script doesn't (-1).
178
res = ts->compare_locales(locale_a, locale_b);
179
180
CHECK(res == 5);
181
182
locale_a = "aa-Latn-ER";
183
locale_b = "aa-Latn-ER-saaho";
184
185
// Script and country match (+2) with variant on one locale (+0).
186
res = ts->compare_locales(locale_a, locale_b);
187
188
CHECK(res == 7);
189
190
locale_a = "uz-Cyrl-UZ";
191
locale_b = "uz-Latn-KG";
192
193
// Both script and country mismatched (-2).
194
res = ts->compare_locales(locale_a, locale_b);
195
196
CHECK(res == 3);
197
198
locale_a = "es-ES";
199
locale_b = "es-AR";
200
201
// Mismatched country (-1).
202
res = ts->compare_locales(locale_a, locale_b);
203
204
CHECK(res == 4);
205
206
locale_a = "es";
207
locale_b = "es-AR";
208
209
// No country for one locale (+0).
210
res = ts->compare_locales(locale_a, locale_b);
211
212
CHECK(res == 5);
213
214
locale_a = "es-EC";
215
locale_b = "fr-LU";
216
217
// No match.
218
res = ts->compare_locales(locale_a, locale_b);
219
220
CHECK(res == 0);
221
222
locale_a = "zh-HK";
223
locale_b = "zh";
224
225
// In full standardization, zh-HK becomes zh_Hant_HK and zh becomes
226
// zh_Hans_CN. Both script and country mismatch (-2).
227
res = ts->compare_locales(locale_a, locale_b);
228
229
CHECK(res == 3);
230
231
locale_a = "zh-CN";
232
locale_b = "zh";
233
234
// In full standardization, zh and zh-CN both become zh_Hans_CN for an
235
// exact match.
236
res = ts->compare_locales(locale_a, locale_b);
237
238
CHECK(res == 10);
239
}
240
241
} // namespace TestTranslationServer
242
243