Skip to content

Commit 6c4c61f

Browse files
committed
Merge pull request #98547 from timothyqiu/loaded-locales-set
Fix duplicated entries in `TranslationServer::get_loaded_locales()`
2 parents a07fea1 + f451997 commit 6c4c61f

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

core/string/translation_domain.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,10 @@ PackedStringArray TranslationDomain::get_loaded_locales() const {
247247
PackedStringArray locales;
248248
for (const Ref<Translation> &E : translations) {
249249
ERR_CONTINUE(E.is_null());
250-
locales.push_back(E->get_locale());
250+
const String &locale = E->get_locale();
251+
if (!locales.has(locale)) {
252+
locales.push_back(locale);
253+
}
251254
}
252255
return locales;
253256
}

tests/core/string/test_translation_server.h

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,37 @@
3737

3838
namespace TestTranslationServer {
3939
TEST_CASE("[TranslationServer] Translation operations") {
40-
Ref<Translation> t = memnew(Translation);
41-
t->set_locale("uk");
42-
t->add_message("Good Morning", String::utf8("Добрий ранок"));
40+
Ref<Translation> t1 = memnew(Translation);
41+
t1->set_locale("uk");
42+
t1->add_message("Good Morning", String(U"Добрий ранок"));
43+
44+
Ref<Translation> t2 = memnew(Translation);
45+
t2->set_locale("uk");
46+
t2->add_message("Hello Godot", String(U"你好戈多"));
4347

4448
TranslationServer *ts = TranslationServer::get_singleton();
4549

50+
// Adds translation for UK locale for the first time.
4651
int l_count_before = ts->get_loaded_locales().size();
47-
ts->add_translation(t);
52+
ts->add_translation(t1);
4853
int l_count_after = ts->get_loaded_locales().size();
49-
// Newly created Translation object should be added to the list, so the counter should increase, too.
5054
CHECK(l_count_after > l_count_before);
5155

52-
Ref<Translation> trans = ts->get_translation_object("uk");
53-
CHECK(trans.is_valid());
56+
// Adds translation for UK locale again.
57+
ts->add_translation(t2);
58+
CHECK_EQ(ts->get_loaded_locales().size(), l_count_after);
59+
60+
// Removing that translation.
61+
ts->remove_translation(t2);
62+
CHECK_EQ(ts->get_loaded_locales().size(), l_count_after);
63+
64+
CHECK(ts->get_translation_object("uk").is_valid());
5465

5566
ts->set_locale("uk");
5667
CHECK(ts->translate("Good Morning") == String::utf8("Добрий ранок"));
5768

58-
ts->remove_translation(t);
59-
trans = ts->get_translation_object("uk");
60-
CHECK(trans.is_null());
69+
ts->remove_translation(t1);
70+
CHECK(ts->get_translation_object("uk").is_null());
6171
// If no suitable Translation object has been found - the original message should be returned.
6272
CHECK(ts->translate("Good Morning") == "Good Morning");
6373
}

0 commit comments

Comments
 (0)