Path: blob/master/tests/core/string/test_translation.h
10278 views
/**************************************************************************/1/* test_translation.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/optimized_translation.h"33#include "core/string/translation.h"34#include "core/string/translation_po.h"35#include "core/string/translation_server.h"3637#ifdef TOOLS_ENABLED38#include "editor/import/resource_importer_csv_translation.h"39#endif4041#include "tests/test_macros.h"42#include "tests/test_utils.h"4344namespace TestTranslation {4546TEST_CASE("[Translation] Messages") {47Ref<Translation> translation = memnew(Translation);48translation->set_locale("fr");49translation->add_message("Hello", "Bonjour");50CHECK(translation->get_message("Hello") == "Bonjour");5152translation->erase_message("Hello");53// The message no longer exists, so it returns an empty string instead.54CHECK(translation->get_message("Hello") == "");5556List<StringName> messages;57translation->get_message_list(&messages);58CHECK(translation->get_message_count() == 0);59CHECK(messages.size() == 0);6061translation->add_message("Hello2", "Bonjour2");62translation->add_message("Hello3", "Bonjour3");63messages.clear();64translation->get_message_list(&messages);65CHECK(translation->get_message_count() == 2);66CHECK(messages.size() == 2);67// Messages are stored in a Map, don't assume ordering.68CHECK(messages.find("Hello2"));69CHECK(messages.find("Hello3"));70}7172TEST_CASE("[TranslationPO] Messages with context") {73Ref<TranslationPO> translation = memnew(TranslationPO);74translation->set_locale("fr");75translation->add_message("Hello", "Bonjour");76translation->add_message("Hello", "Salut", "friendly");77CHECK(translation->get_message("Hello") == "Bonjour");78CHECK(translation->get_message("Hello", "friendly") == "Salut");79CHECK(translation->get_message("Hello", "nonexistent_context") == "");8081// Only remove the message for the default context, not the "friendly" context.82translation->erase_message("Hello");83// The message no longer exists, so it returns an empty string instead.84CHECK(translation->get_message("Hello") == "");85CHECK(translation->get_message("Hello", "friendly") == "Salut");86CHECK(translation->get_message("Hello", "nonexistent_context") == "");8788List<StringName> messages;89translation->get_message_list(&messages);9091// `get_message_count()` takes all contexts into account.92CHECK(translation->get_message_count() == 1);93// Only the default context is taken into account.94// Since "Hello" is now only present in a non-default context, it is not counted in the list of messages.95CHECK(messages.size() == 0);9697translation->add_message("Hello2", "Bonjour2");98translation->add_message("Hello2", "Salut2", "friendly");99translation->add_message("Hello3", "Bonjour3");100messages.clear();101translation->get_message_list(&messages);102103// `get_message_count()` takes all contexts into account.104CHECK(translation->get_message_count() == 4);105// Only the default context is taken into account.106CHECK(messages.size() == 2);107// Messages are stored in a Map, don't assume ordering.108CHECK(messages.find("Hello2"));109CHECK(messages.find("Hello3"));110}111112TEST_CASE("[TranslationPO] Plural messages") {113Ref<TranslationPO> translation = memnew(TranslationPO);114translation->set_locale("fr");115translation->set_plural_rule("Plural-Forms: nplurals=2; plural=(n >= 2);");116CHECK(translation->get_plural_forms() == 2);117118PackedStringArray plurals;119plurals.push_back("Il y a %d pomme");120plurals.push_back("Il y a %d pommes");121translation->add_plural_message("There are %d apples", plurals);122ERR_PRINT_OFF;123// This is invalid, as the number passed to `get_plural_message()` may not be negative.124CHECK(vformat(translation->get_plural_message("There are %d apples", "", -1), -1) == "");125ERR_PRINT_ON;126CHECK(vformat(translation->get_plural_message("There are %d apples", "", 0), 0) == "Il y a 0 pomme");127CHECK(vformat(translation->get_plural_message("There are %d apples", "", 1), 1) == "Il y a 1 pomme");128CHECK(vformat(translation->get_plural_message("There are %d apples", "", 2), 2) == "Il y a 2 pommes");129}130131#ifdef TOOLS_ENABLED132TEST_CASE("[OptimizedTranslation] Generate from Translation and read messages") {133Ref<Translation> translation = memnew(Translation);134translation->set_locale("fr");135translation->add_message("Hello", "Bonjour");136translation->add_message("Hello2", "Bonjour2");137translation->add_message("Hello3", "Bonjour3");138139Ref<OptimizedTranslation> optimized_translation = memnew(OptimizedTranslation);140optimized_translation->generate(translation);141CHECK(optimized_translation->get_message("Hello") == "Bonjour");142CHECK(optimized_translation->get_message("Hello2") == "Bonjour2");143CHECK(optimized_translation->get_message("Hello3") == "Bonjour3");144CHECK(optimized_translation->get_message("DoesNotExist") == "");145146List<StringName> messages;147// `get_message_list()` can't return the list of messages stored in an OptimizedTranslation.148optimized_translation->get_message_list(&messages);149CHECK(optimized_translation->get_message_count() == 0);150CHECK(messages.size() == 0);151}152153TEST_CASE("[TranslationCSV] CSV import") {154Ref<ResourceImporterCSVTranslation> import_csv_translation = memnew(ResourceImporterCSVTranslation);155156HashMap<StringName, Variant> options;157options["compress"] = false;158options["delimiter"] = 0;159160List<String> gen_files;161162Error result = import_csv_translation->import(0, TestUtils::get_data_path("translations.csv"),163"", options, nullptr, &gen_files);164CHECK(result == OK);165CHECK(gen_files.size() == 4);166167TranslationServer *ts = TranslationServer::get_singleton();168169for (const String &file : gen_files) {170Ref<Translation> translation = ResourceLoader::load(file);171CHECK(translation.is_valid());172ts->add_translation(translation);173}174175ts->set_locale("en");176177// `tr` can be called on any Object, we reuse TranslationServer for convenience.178CHECK(ts->tr("GOOD_MORNING") == "Good Morning");179CHECK(ts->tr("GOOD_EVENING") == "Good Evening");180181ts->set_locale("de");182183CHECK(ts->tr("GOOD_MORNING") == "Guten Morgen");184CHECK(ts->tr("GOOD_EVENING") == "Good Evening"); // Left blank in CSV, should source from 'en'.185186ts->set_locale("ja");187188CHECK(ts->tr("GOOD_MORNING") == String::utf8("おはよう"));189CHECK(ts->tr("GOOD_EVENING") == String::utf8("こんばんは"));190191/* FIXME: This passes, but triggers a chain reaction that makes test_viewport192* and test_text_edit explode in a billion glittery Unicode particles.193ts->set_locale("fa");194195CHECK(ts->tr("GOOD_MORNING") == String::utf8("صبح بخیر"));196CHECK(ts->tr("GOOD_EVENING") == String::utf8("عصر بخیر"));197*/198}199#endif // TOOLS_ENABLED200201} // namespace TestTranslation202203204