Path: blob/master/tests/core/string/test_translation_server.h
21276 views
/**************************************************************************/1/* test_translation_server.h */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#pragma once3132#include "core/string/translation_server.h"3334#include "tests/test_macros.h"3536namespace TestTranslationServer {37TEST_CASE("[TranslationServer] Translation operations") {38Ref<TranslationDomain> td = TranslationServer::get_singleton()->get_or_add_domain("godot.test");39CHECK(td->get_translations().is_empty());4041Ref<Translation> t1 = memnew(Translation);42t1->set_locale("uk"); // Ukrainian.43t1->add_message("Good Morning", String(U"Добрий ранок"));44td->add_translation(t1);45CHECK(td->get_translations().size() == 1);46CHECK(td->has_translation_for_locale("uk", true));47CHECK(td->has_translation_for_locale("uk", false));48CHECK_FALSE(td->has_translation_for_locale("uk_UA", true));49CHECK(td->has_translation_for_locale("uk_UA", false));50CHECK(td->find_translations("uk", false).size() == 1);51CHECK(td->find_translations("uk", true).size() == 1);52CHECK(td->find_translations("uk_UA", false).size() == 1);53CHECK(td->find_translations("uk_UA", true).size() == 0);5455Ref<Translation> t2 = memnew(Translation);56t2->set_locale("uk_UA"); // Ukrainian in Ukraine.57t2->add_message("Hello Godot", String(U"Привіт, Годо."));58td->add_translation(t2);59CHECK(td->get_translations().size() == 2);60CHECK(td->has_translation_for_locale("uk", true));61CHECK(td->has_translation_for_locale("uk", false));62CHECK(td->has_translation_for_locale("uk_UA", true));63CHECK(td->has_translation_for_locale("uk_UA", false));64CHECK(td->find_translations("uk", false).size() == 2);65CHECK(td->find_translations("uk", true).size() == 1);66CHECK(td->find_translations("uk_UA", false).size() == 2);67CHECK(td->find_translations("uk_UA", true).size() == 1);6869td->set_locale_override("uk");70CHECK(td->translate("Good Morning", StringName()) == String::utf8("Добрий ранок"));7172td->remove_translation(t1);73CHECK(td->get_translations().size() == 1);74CHECK_FALSE(td->has_translation_for_locale("uk", true));75CHECK(td->has_translation_for_locale("uk", false));76CHECK(td->has_translation_for_locale("uk_UA", true));77CHECK(td->has_translation_for_locale("uk_UA", false));78CHECK(td->find_translations("uk", true).size() == 0);79CHECK(td->find_translations("uk", false).size() == 1);80CHECK(td->find_translations("uk_UA", true).size() == 1);81CHECK(td->find_translations("uk_UA", false).size() == 1);8283// If no suitable Translation object has been found - the original message should be returned.84CHECK(td->translate("Good Morning", StringName()) == "Good Morning");8586TranslationServer::get_singleton()->remove_domain("godot.test");87}8889TEST_CASE("[TranslationServer] Locale operations") {90TranslationServer *ts = TranslationServer::get_singleton();9192// Language variant test; we supplied the variant of Español and the result should be the same string.93String loc = "es_Hani_ES_tradnl";94String res = ts->standardize_locale(loc);9596CHECK(res == loc);9798// No such variant in variant_map; should return everything except the variant.99loc = "es_Hani_ES_missing";100res = ts->standardize_locale(loc);101102CHECK(res == "es_Hani_ES");103104// Non-ISO language name check (Windows issue).105loc = "iw_Hani_IL";106res = ts->standardize_locale(loc);107108CHECK(res == "he_Hani_IL");109110// Country rename check.111loc = "uk_Hani_UK";112res = ts->standardize_locale(loc);113114CHECK(res == "uk_Hani_GB");115116// Supplying a script name that is not in the list.117loc = "de_Wrong_DE";118res = ts->standardize_locale(loc);119120CHECK(res == "de_DE");121122// No added defaults.123loc = "es_ES";124res = ts->standardize_locale(loc, true);125126CHECK(res == "es_ES");127128// Add default script.129loc = "az_AZ";130res = ts->standardize_locale(loc, true);131132CHECK(res == "az_Latn_AZ");133134// Add default country.135loc = "pa_Arab";136res = ts->standardize_locale(loc, true);137138CHECK(res == "pa_Arab_PK");139140// Add default script and country.141loc = "zh";142res = ts->standardize_locale(loc, true);143144CHECK(res == "zh_Hans_CN");145146// Explicitly don't add defaults.147loc = "zh";148res = ts->standardize_locale(loc, false);149150CHECK(res == "zh");151}152153TEST_CASE("[TranslationServer] Comparing locales") {154TranslationServer *ts = TranslationServer::get_singleton();155156String locale_a = "es";157String locale_b = "es";158159// Exact match check.160int res = ts->compare_locales(locale_a, locale_b);161162CHECK(res == 10);163164locale_a = "sr-Latn-CS";165locale_b = "sr-Latn-RS";166167// Script matches (+1) but country doesn't (-1).168res = ts->compare_locales(locale_a, locale_b);169170CHECK(res == 5);171172locale_a = "uz-Cyrl-UZ";173locale_b = "uz-Latn-UZ";174175// Country matches (+1) but script doesn't (-1).176res = ts->compare_locales(locale_a, locale_b);177178CHECK(res == 5);179180locale_a = "aa-Latn-ER";181locale_b = "aa-Latn-ER-saaho";182183// Script and country match (+2) with variant on one locale (+0).184res = ts->compare_locales(locale_a, locale_b);185186CHECK(res == 7);187188locale_a = "uz-Cyrl-UZ";189locale_b = "uz-Latn-KG";190191// Both script and country mismatched (-2).192res = ts->compare_locales(locale_a, locale_b);193194CHECK(res == 3);195196locale_a = "es-ES";197locale_b = "es-AR";198199// Mismatched country (-1).200res = ts->compare_locales(locale_a, locale_b);201202CHECK(res == 4);203204locale_a = "es";205locale_b = "es-AR";206207// No country for one locale (+0).208res = ts->compare_locales(locale_a, locale_b);209210CHECK(res == 5);211212locale_a = "es-EC";213locale_b = "fr-LU";214215// No match.216res = ts->compare_locales(locale_a, locale_b);217218CHECK(res == 0);219220locale_a = "zh-HK";221locale_b = "zh";222223// In full standardization, zh-HK becomes zh_Hant_HK and zh becomes224// zh_Hans_CN. Both script and country mismatch (-2).225res = ts->compare_locales(locale_a, locale_b);226227CHECK(res == 3);228229locale_a = "zh-CN";230locale_b = "zh";231232// In full standardization, zh and zh-CN both become zh_Hans_CN for an233// exact match.234res = ts->compare_locales(locale_a, locale_b);235236CHECK(res == 10);237}238} // namespace TestTranslationServer239240241