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