Path: blob/master/tests/core/string/test_translation_server.cpp
45997 views
/**************************************************************************/1/* test_translation_server.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "tests/test_macros.h"3132TEST_FORCE_LINK(test_translation_server)3334#include "core/string/translation_server.h"3536namespace TestTranslationServer {3738TEST_CASE("[TranslationServer] Translation operations") {39Ref<TranslationDomain> td = TranslationServer::get_singleton()->get_or_add_domain("godot.test");40CHECK(td->get_translations().is_empty());4142Ref<Translation> t1 = memnew(Translation);43t1->set_locale("uk"); // Ukrainian.44t1->add_message("Good Morning", String(U"Добрий ранок"));45td->add_translation(t1);46CHECK(td->get_translations().size() == 1);47CHECK(td->has_translation_for_locale("uk", true));48CHECK(td->has_translation_for_locale("uk", false));49CHECK_FALSE(td->has_translation_for_locale("uk_UA", true));50CHECK(td->has_translation_for_locale("uk_UA", false));51CHECK(td->find_translations("uk", false).size() == 1);52CHECK(td->find_translations("uk", true).size() == 1);53CHECK(td->find_translations("uk_UA", false).size() == 1);54CHECK(td->find_translations("uk_UA", true).size() == 0);5556Ref<Translation> t2 = memnew(Translation);57t2->set_locale("uk_UA"); // Ukrainian in Ukraine.58t2->add_message("Hello Godot", String(U"Привіт, Годо."));59td->add_translation(t2);60CHECK(td->get_translations().size() == 2);61CHECK(td->has_translation_for_locale("uk", true));62CHECK(td->has_translation_for_locale("uk", false));63CHECK(td->has_translation_for_locale("uk_UA", true));64CHECK(td->has_translation_for_locale("uk_UA", false));65CHECK(td->find_translations("uk", false).size() == 2);66CHECK(td->find_translations("uk", true).size() == 1);67CHECK(td->find_translations("uk_UA", false).size() == 2);68CHECK(td->find_translations("uk_UA", true).size() == 1);6970td->set_locale_override("uk");71CHECK(td->translate("Good Morning", StringName()) == String::utf8("Добрий ранок"));7273td->remove_translation(t1);74CHECK(td->get_translations().size() == 1);75CHECK_FALSE(td->has_translation_for_locale("uk", true));76CHECK(td->has_translation_for_locale("uk", false));77CHECK(td->has_translation_for_locale("uk_UA", true));78CHECK(td->has_translation_for_locale("uk_UA", false));79CHECK(td->find_translations("uk", true).size() == 0);80CHECK(td->find_translations("uk", false).size() == 1);81CHECK(td->find_translations("uk_UA", true).size() == 1);82CHECK(td->find_translations("uk_UA", false).size() == 1);8384// If no suitable Translation object has been found - the original message should be returned.85CHECK(td->translate("Good Morning", StringName()) == "Good Morning");8687TranslationServer::get_singleton()->remove_domain("godot.test");88}8990TEST_CASE("[TranslationServer] Locale operations") {91TranslationServer *ts = TranslationServer::get_singleton();9293// Language variant test; we supplied the variant of Español and the result should be the same string.94String loc = "es_Hani_ES_tradnl";95String res = ts->standardize_locale(loc);9697CHECK(res == loc);9899// No such variant in variant_map; should return everything except the variant.100loc = "es_Hani_ES_missing";101res = ts->standardize_locale(loc);102103CHECK(res == "es_Hani_ES");104105// Non-ISO language name check (Windows issue).106loc = "iw_Hani_IL";107res = ts->standardize_locale(loc);108109CHECK(res == "he_Hani_IL");110111// Country rename check.112loc = "uk_Hani_UK";113res = ts->standardize_locale(loc);114115CHECK(res == "uk_Hani_GB");116117// Supplying a script name that is not in the list.118loc = "de_Wrong_DE";119res = ts->standardize_locale(loc);120121CHECK(res == "de_DE");122123// No added defaults.124loc = "es_ES";125res = ts->standardize_locale(loc, true);126127CHECK(res == "es_ES");128129// Add default script.130loc = "az_AZ";131res = ts->standardize_locale(loc, true);132133CHECK(res == "az_Latn_AZ");134135// Add default country.136loc = "pa_Arab";137res = ts->standardize_locale(loc, true);138139CHECK(res == "pa_Arab_PK");140141// Add default script and country.142loc = "zh";143res = ts->standardize_locale(loc, true);144145CHECK(res == "zh_Hans_CN");146147// Explicitly don't add defaults.148loc = "zh";149res = ts->standardize_locale(loc, false);150151CHECK(res == "zh");152}153154TEST_CASE("[TranslationServer] Comparing locales") {155TranslationServer *ts = TranslationServer::get_singleton();156157String locale_a = "es";158String locale_b = "es";159160// Exact match check.161int res = ts->compare_locales(locale_a, locale_b);162163CHECK(res == 10);164165locale_a = "sr-Latn-CS";166locale_b = "sr-Latn-RS";167168// Script matches (+1) but country doesn't (-1).169res = ts->compare_locales(locale_a, locale_b);170171CHECK(res == 5);172173locale_a = "uz-Cyrl-UZ";174locale_b = "uz-Latn-UZ";175176// Country matches (+1) but script doesn't (-1).177res = ts->compare_locales(locale_a, locale_b);178179CHECK(res == 5);180181locale_a = "aa-Latn-ER";182locale_b = "aa-Latn-ER-saaho";183184// Script and country match (+2) with variant on one locale (+0).185res = ts->compare_locales(locale_a, locale_b);186187CHECK(res == 7);188189locale_a = "uz-Cyrl-UZ";190locale_b = "uz-Latn-KG";191192// Both script and country mismatched (-2).193res = ts->compare_locales(locale_a, locale_b);194195CHECK(res == 3);196197locale_a = "es-ES";198locale_b = "es-AR";199200// Mismatched country (-1).201res = ts->compare_locales(locale_a, locale_b);202203CHECK(res == 4);204205locale_a = "es";206locale_b = "es-AR";207208// No country for one locale (+0).209res = ts->compare_locales(locale_a, locale_b);210211CHECK(res == 5);212213locale_a = "es-EC";214locale_b = "fr-LU";215216// No match.217res = ts->compare_locales(locale_a, locale_b);218219CHECK(res == 0);220221locale_a = "zh-HK";222locale_b = "zh";223224// In full standardization, zh-HK becomes zh_Hant_HK and zh becomes225// zh_Hans_CN. Both script and country mismatch (-2).226res = ts->compare_locales(locale_a, locale_b);227228CHECK(res == 3);229230locale_a = "zh-CN";231locale_b = "zh";232233// In full standardization, zh and zh-CN both become zh_Hans_CN for an234// exact match.235res = ts->compare_locales(locale_a, locale_b);236237CHECK(res == 10);238}239240} // namespace TestTranslationServer241242243