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