Path: blob/master/core/string/translation_server.cpp
10277 views
/**************************************************************************/1/* 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 "translation_server.h"31#include "translation_server.compat.inc"3233#include "core/config/project_settings.h"34#include "core/io/resource_loader.h"35#include "core/os/os.h"36#include "core/string/locales.h"3738Vector<TranslationServer::LocaleScriptInfo> TranslationServer::locale_script_info;3940HashMap<String, String> TranslationServer::language_map;41HashMap<String, String> TranslationServer::script_map;42HashMap<String, String> TranslationServer::locale_rename_map;43HashMap<String, String> TranslationServer::country_name_map;44HashMap<String, String> TranslationServer::variant_map;45HashMap<String, String> TranslationServer::country_rename_map;4647void TranslationServer::init_locale_info() {48// Init locale info.49language_map.clear();50int idx = 0;51while (language_list[idx][0] != nullptr) {52language_map[language_list[idx][0]] = String::utf8(language_list[idx][1]);53idx++;54}5556// Init locale-script map.57locale_script_info.clear();58idx = 0;59while (locale_scripts[idx][0] != nullptr) {60LocaleScriptInfo info;61info.name = locale_scripts[idx][0];62info.script = locale_scripts[idx][1];63info.default_country = locale_scripts[idx][2];64Vector<String> supported_countries = String(locale_scripts[idx][3]).split(",", false);65for (int i = 0; i < supported_countries.size(); i++) {66info.supported_countries.insert(supported_countries[i]);67}68locale_script_info.push_back(info);69idx++;70}7172// Init supported script list.73script_map.clear();74idx = 0;75while (script_list[idx][0] != nullptr) {76script_map[script_list[idx][1]] = String::utf8(script_list[idx][0]);77idx++;78}7980// Init regional variant map.81variant_map.clear();82idx = 0;83while (locale_variants[idx][0] != nullptr) {84variant_map[locale_variants[idx][0]] = locale_variants[idx][1];85idx++;86}8788// Init locale renames.89locale_rename_map.clear();90idx = 0;91while (locale_renames[idx][0] != nullptr) {92if (!String(locale_renames[idx][1]).is_empty()) {93locale_rename_map[locale_renames[idx][0]] = locale_renames[idx][1];94}95idx++;96}9798// Init country names.99country_name_map.clear();100idx = 0;101while (country_names[idx][0] != nullptr) {102country_name_map[String(country_names[idx][0])] = String::utf8(country_names[idx][1]);103idx++;104}105106// Init country renames.107country_rename_map.clear();108idx = 0;109while (country_renames[idx][0] != nullptr) {110if (!String(country_renames[idx][1]).is_empty()) {111country_rename_map[country_renames[idx][0]] = country_renames[idx][1];112}113idx++;114}115}116117TranslationServer::Locale::operator String() const {118String out = language;119if (!script.is_empty()) {120out = out + "_" + script;121}122if (!country.is_empty()) {123out = out + "_" + country;124}125if (!variant.is_empty()) {126out = out + "_" + variant;127}128return out;129}130131TranslationServer::Locale::Locale(const TranslationServer &p_server, const String &p_locale, bool p_add_defaults) {132// Replaces '-' with '_' for macOS style locales.133String univ_locale = p_locale.replace_char('-', '_');134135// Extract locale elements.136Vector<String> locale_elements = univ_locale.get_slicec('@', 0).split("_");137language = locale_elements[0];138if (locale_elements.size() >= 2) {139if (locale_elements[1].length() == 4 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_lower_case(locale_elements[1][1]) && is_ascii_lower_case(locale_elements[1][2]) && is_ascii_lower_case(locale_elements[1][3])) {140script = locale_elements[1];141}142if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) {143country = locale_elements[1];144}145}146if (locale_elements.size() >= 3) {147if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) {148country = locale_elements[2];149} else if (p_server.variant_map.has(locale_elements[2].to_lower()) && p_server.variant_map[locale_elements[2].to_lower()] == language) {150variant = locale_elements[2].to_lower();151}152}153if (locale_elements.size() >= 4) {154if (p_server.variant_map.has(locale_elements[3].to_lower()) && p_server.variant_map[locale_elements[3].to_lower()] == language) {155variant = locale_elements[3].to_lower();156}157}158159// Try extract script and variant from the extra part.160Vector<String> script_extra = univ_locale.get_slicec('@', 1).split(";");161for (int i = 0; i < script_extra.size(); i++) {162if (script_extra[i].to_lower() == "cyrillic") {163script = "Cyrl";164break;165} else if (script_extra[i].to_lower() == "latin") {166script = "Latn";167break;168} else if (script_extra[i].to_lower() == "devanagari") {169script = "Deva";170break;171} else if (p_server.variant_map.has(script_extra[i].to_lower()) && p_server.variant_map[script_extra[i].to_lower()] == language) {172variant = script_extra[i].to_lower();173}174}175176// Handles known non-ISO language names used e.g. on Windows.177if (p_server.locale_rename_map.has(language)) {178language = p_server.locale_rename_map[language];179}180181// Handle country renames.182if (p_server.country_rename_map.has(country)) {183country = p_server.country_rename_map[country];184}185186// Remove unsupported script codes.187if (!p_server.script_map.has(script)) {188script = "";189}190191// Add script code base on language and country codes for some ambiguous cases.192if (p_add_defaults) {193if (script.is_empty()) {194for (int i = 0; i < p_server.locale_script_info.size(); i++) {195const LocaleScriptInfo &info = p_server.locale_script_info[i];196if (info.name == language) {197if (country.is_empty() || info.supported_countries.has(country)) {198script = info.script;199break;200}201}202}203}204if (!script.is_empty() && country.is_empty()) {205// Add conntry code based on script for some ambiguous cases.206for (int i = 0; i < p_server.locale_script_info.size(); i++) {207const LocaleScriptInfo &info = p_server.locale_script_info[i];208if (info.name == language && info.script == script) {209country = info.default_country;210break;211}212}213}214}215}216217String TranslationServer::standardize_locale(const String &p_locale, bool p_add_defaults) const {218return Locale(*this, p_locale, p_add_defaults).operator String();219}220221int TranslationServer::compare_locales(const String &p_locale_a, const String &p_locale_b) const {222if (p_locale_a == p_locale_b) {223// Exact match.224return 10;225}226227const String cache_key = p_locale_a + "|" + p_locale_b;228const int *cached_result = locale_compare_cache.getptr(cache_key);229if (cached_result) {230return *cached_result;231}232233Locale locale_a = Locale(*this, p_locale_a, true);234Locale locale_b = Locale(*this, p_locale_b, true);235236if (locale_a == locale_b) {237// Exact match.238locale_compare_cache.insert(cache_key, 10);239return 10;240}241242if (locale_a.language != locale_b.language) {243// No match.244locale_compare_cache.insert(cache_key, 0);245return 0;246}247248// Matching language, both locales have extra parts. Compare the249// remaining elements. If both elements are non-empty, check the250// match to increase or decrease the score. If either element or251// both are empty, leave the score as is.252int score = 5;253if (!locale_a.script.is_empty() && !locale_b.script.is_empty()) {254if (locale_a.script == locale_b.script) {255score++;256} else {257score--;258}259}260if (!locale_a.country.is_empty() && !locale_b.country.is_empty()) {261if (locale_a.country == locale_b.country) {262score++;263} else {264score--;265}266}267if (!locale_a.variant.is_empty() && !locale_b.variant.is_empty()) {268if (locale_a.variant == locale_b.variant) {269score++;270} else {271score--;272}273}274275locale_compare_cache.insert(cache_key, score);276return score;277}278279String TranslationServer::get_locale_name(const String &p_locale) const {280String lang_name, script_name, country_name;281Vector<String> locale_elements = standardize_locale(p_locale).split("_");282lang_name = locale_elements[0];283if (locale_elements.size() >= 2) {284if (locale_elements[1].length() == 4 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_lower_case(locale_elements[1][1]) && is_ascii_lower_case(locale_elements[1][2]) && is_ascii_lower_case(locale_elements[1][3])) {285script_name = locale_elements[1];286}287if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) {288country_name = locale_elements[1];289}290}291if (locale_elements.size() >= 3) {292if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) {293country_name = locale_elements[2];294}295}296297String name = get_language_name(lang_name);298if (!script_name.is_empty()) {299name = name + " (" + get_script_name(script_name) + ")";300}301if (!country_name.is_empty()) {302name = name + ", " + get_country_name(country_name);303}304return name;305}306307Vector<String> TranslationServer::get_all_languages() const {308Vector<String> languages;309310for (const KeyValue<String, String> &E : language_map) {311languages.push_back(E.key);312}313314return languages;315}316317String TranslationServer::get_language_name(const String &p_language) const {318if (language_map.has(p_language)) {319return language_map[p_language];320} else {321return p_language;322}323}324325Vector<String> TranslationServer::get_all_scripts() const {326Vector<String> scripts;327328for (const KeyValue<String, String> &E : script_map) {329scripts.push_back(E.key);330}331332return scripts;333}334335String TranslationServer::get_script_name(const String &p_script) const {336if (script_map.has(p_script)) {337return script_map[p_script];338} else {339return p_script;340}341}342343Vector<String> TranslationServer::get_all_countries() const {344Vector<String> countries;345346for (const KeyValue<String, String> &E : country_name_map) {347countries.push_back(E.key);348}349350return countries;351}352353String TranslationServer::get_country_name(const String &p_country) const {354if (country_name_map.has(p_country)) {355return country_name_map[p_country];356} else {357return p_country;358}359}360361void TranslationServer::set_locale(const String &p_locale) {362String new_locale = standardize_locale(p_locale);363if (locale == new_locale) {364return;365}366367locale = new_locale;368ResourceLoader::reload_translation_remaps();369370if (OS::get_singleton()->get_main_loop()) {371OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);372}373}374375String TranslationServer::get_locale() const {376return locale;377}378379void TranslationServer::set_fallback_locale(const String &p_locale) {380fallback = p_locale;381}382383String TranslationServer::get_fallback_locale() const {384return fallback;385}386387PackedStringArray TranslationServer::get_loaded_locales() const {388return main_domain->get_loaded_locales();389}390391void TranslationServer::add_translation(const Ref<Translation> &p_translation) {392main_domain->add_translation(p_translation);393}394395void TranslationServer::remove_translation(const Ref<Translation> &p_translation) {396main_domain->remove_translation(p_translation);397}398399Ref<Translation> TranslationServer::get_translation_object(const String &p_locale) {400return main_domain->get_translation_object(p_locale);401}402403void TranslationServer::clear() {404main_domain->clear();405}406407StringName TranslationServer::translate(const StringName &p_message, const StringName &p_context) const {408return main_domain->translate(p_message, p_context);409}410411StringName TranslationServer::translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {412return main_domain->translate_plural(p_message, p_message_plural, p_n, p_context);413}414415bool TranslationServer::has_domain(const StringName &p_domain) const {416if (p_domain == StringName()) {417return true;418}419return custom_domains.has(p_domain);420}421422Ref<TranslationDomain> TranslationServer::get_or_add_domain(const StringName &p_domain) {423if (p_domain == StringName()) {424return main_domain;425}426const Ref<TranslationDomain> *domain = custom_domains.getptr(p_domain);427if (domain) {428if (domain->is_valid()) {429return *domain;430}431ERR_PRINT("Bug (please report): Found invalid translation domain.");432}433Ref<TranslationDomain> new_domain = memnew(TranslationDomain);434custom_domains[p_domain] = new_domain;435return new_domain;436}437438void TranslationServer::remove_domain(const StringName &p_domain) {439ERR_FAIL_COND_MSG(p_domain == StringName(), "Cannot remove main translation domain.");440custom_domains.erase(p_domain);441}442443void TranslationServer::setup() {444String test = GLOBAL_DEF("internationalization/locale/test", "");445test = test.strip_edges();446if (!test.is_empty()) {447set_locale(test);448} else {449set_locale(OS::get_singleton()->get_locale());450}451452fallback = GLOBAL_DEF("internationalization/locale/fallback", "en");453main_domain->set_pseudolocalization_enabled(GLOBAL_DEF("internationalization/pseudolocalization/use_pseudolocalization", false));454main_domain->set_pseudolocalization_accents_enabled(GLOBAL_DEF("internationalization/pseudolocalization/replace_with_accents", true));455main_domain->set_pseudolocalization_double_vowels_enabled(GLOBAL_DEF("internationalization/pseudolocalization/double_vowels", false));456main_domain->set_pseudolocalization_fake_bidi_enabled(GLOBAL_DEF("internationalization/pseudolocalization/fake_bidi", false));457main_domain->set_pseudolocalization_override_enabled(GLOBAL_DEF("internationalization/pseudolocalization/override", false));458main_domain->set_pseudolocalization_expansion_ratio(GLOBAL_DEF("internationalization/pseudolocalization/expansion_ratio", 0.0));459main_domain->set_pseudolocalization_prefix(GLOBAL_DEF("internationalization/pseudolocalization/prefix", "["));460main_domain->set_pseudolocalization_suffix(GLOBAL_DEF("internationalization/pseudolocalization/suffix", "]"));461main_domain->set_pseudolocalization_skip_placeholders_enabled(GLOBAL_DEF("internationalization/pseudolocalization/skip_placeholders", true));462463#ifdef TOOLS_ENABLED464ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "internationalization/locale/test", PROPERTY_HINT_LOCALE_ID, ""));465ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "internationalization/locale/fallback", PROPERTY_HINT_LOCALE_ID, ""));466#endif467}468469String TranslationServer::get_tool_locale() {470#ifdef TOOLS_ENABLED471if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) {472const PackedStringArray &locales = editor_domain->get_loaded_locales();473if (locales.has(locale)) {474return locale;475}476return "en";477} else {478#else479{480#endif481// Look for best matching loaded translation.482Ref<Translation> t = main_domain->get_translation_object(locale);483if (t.is_null()) {484return fallback;485}486return t->get_locale();487}488}489490StringName TranslationServer::tool_translate(const StringName &p_message, const StringName &p_context) const {491return editor_domain->translate(p_message, p_context);492}493494StringName TranslationServer::tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {495return editor_domain->translate_plural(p_message, p_message_plural, p_n, p_context);496}497498StringName TranslationServer::property_translate(const StringName &p_message, const StringName &p_context) const {499return property_domain->translate(p_message, p_context);500}501502StringName TranslationServer::doc_translate(const StringName &p_message, const StringName &p_context) const {503return doc_domain->translate(p_message, p_context);504}505506StringName TranslationServer::doc_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {507return doc_domain->translate_plural(p_message, p_message_plural, p_n, p_context);508}509510bool TranslationServer::is_pseudolocalization_enabled() const {511return main_domain->is_pseudolocalization_enabled();512}513514void TranslationServer::set_pseudolocalization_enabled(bool p_enabled) {515main_domain->set_pseudolocalization_enabled(p_enabled);516517ResourceLoader::reload_translation_remaps();518519if (OS::get_singleton()->get_main_loop()) {520OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);521}522}523524void TranslationServer::reload_pseudolocalization() {525main_domain->set_pseudolocalization_accents_enabled(GLOBAL_GET("internationalization/pseudolocalization/replace_with_accents"));526main_domain->set_pseudolocalization_double_vowels_enabled(GLOBAL_GET("internationalization/pseudolocalization/double_vowels"));527main_domain->set_pseudolocalization_fake_bidi_enabled(GLOBAL_GET("internationalization/pseudolocalization/fake_bidi"));528main_domain->set_pseudolocalization_override_enabled(GLOBAL_GET("internationalization/pseudolocalization/override"));529main_domain->set_pseudolocalization_expansion_ratio(GLOBAL_GET("internationalization/pseudolocalization/expansion_ratio"));530main_domain->set_pseudolocalization_prefix(GLOBAL_GET("internationalization/pseudolocalization/prefix"));531main_domain->set_pseudolocalization_suffix(GLOBAL_GET("internationalization/pseudolocalization/suffix"));532main_domain->set_pseudolocalization_skip_placeholders_enabled(GLOBAL_GET("internationalization/pseudolocalization/skip_placeholders"));533534ResourceLoader::reload_translation_remaps();535536if (OS::get_singleton()->get_main_loop()) {537OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);538}539}540541StringName TranslationServer::pseudolocalize(const StringName &p_message) const {542return main_domain->pseudolocalize(p_message);543}544545#ifdef TOOLS_ENABLED546void TranslationServer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {547const String pf = p_function;548if (p_idx == 0) {549HashMap<String, String> *target_hash_map = nullptr;550if (pf == "get_language_name") {551target_hash_map = &language_map;552} else if (pf == "get_script_name") {553target_hash_map = &script_map;554} else if (pf == "get_country_name") {555target_hash_map = &country_name_map;556}557558if (target_hash_map) {559for (const KeyValue<String, String> &E : *target_hash_map) {560r_options->push_back(E.key.quote());561}562}563}564Object::get_argument_options(p_function, p_idx, r_options);565}566#endif // TOOLS_ENABLED567568void TranslationServer::_bind_methods() {569ClassDB::bind_method(D_METHOD("set_locale", "locale"), &TranslationServer::set_locale);570ClassDB::bind_method(D_METHOD("get_locale"), &TranslationServer::get_locale);571ClassDB::bind_method(D_METHOD("get_tool_locale"), &TranslationServer::get_tool_locale);572573ClassDB::bind_method(D_METHOD("compare_locales", "locale_a", "locale_b"), &TranslationServer::compare_locales);574ClassDB::bind_method(D_METHOD("standardize_locale", "locale", "add_defaults"), &TranslationServer::standardize_locale, DEFVAL(false));575576ClassDB::bind_method(D_METHOD("get_all_languages"), &TranslationServer::get_all_languages);577ClassDB::bind_method(D_METHOD("get_language_name", "language"), &TranslationServer::get_language_name);578579ClassDB::bind_method(D_METHOD("get_all_scripts"), &TranslationServer::get_all_scripts);580ClassDB::bind_method(D_METHOD("get_script_name", "script"), &TranslationServer::get_script_name);581582ClassDB::bind_method(D_METHOD("get_all_countries"), &TranslationServer::get_all_countries);583ClassDB::bind_method(D_METHOD("get_country_name", "country"), &TranslationServer::get_country_name);584585ClassDB::bind_method(D_METHOD("get_locale_name", "locale"), &TranslationServer::get_locale_name);586587ClassDB::bind_method(D_METHOD("translate", "message", "context"), &TranslationServer::translate, DEFVAL(StringName()));588ClassDB::bind_method(D_METHOD("translate_plural", "message", "plural_message", "n", "context"), &TranslationServer::translate_plural, DEFVAL(StringName()));589590ClassDB::bind_method(D_METHOD("add_translation", "translation"), &TranslationServer::add_translation);591ClassDB::bind_method(D_METHOD("remove_translation", "translation"), &TranslationServer::remove_translation);592ClassDB::bind_method(D_METHOD("get_translation_object", "locale"), &TranslationServer::get_translation_object);593594ClassDB::bind_method(D_METHOD("has_domain", "domain"), &TranslationServer::has_domain);595ClassDB::bind_method(D_METHOD("get_or_add_domain", "domain"), &TranslationServer::get_or_add_domain);596ClassDB::bind_method(D_METHOD("remove_domain", "domain"), &TranslationServer::remove_domain);597598ClassDB::bind_method(D_METHOD("clear"), &TranslationServer::clear);599600ClassDB::bind_method(D_METHOD("get_loaded_locales"), &TranslationServer::get_loaded_locales);601602ClassDB::bind_method(D_METHOD("is_pseudolocalization_enabled"), &TranslationServer::is_pseudolocalization_enabled);603ClassDB::bind_method(D_METHOD("set_pseudolocalization_enabled", "enabled"), &TranslationServer::set_pseudolocalization_enabled);604ClassDB::bind_method(D_METHOD("reload_pseudolocalization"), &TranslationServer::reload_pseudolocalization);605ClassDB::bind_method(D_METHOD("pseudolocalize", "message"), &TranslationServer::pseudolocalize);606ADD_PROPERTY(PropertyInfo(Variant::Type::BOOL, "pseudolocalization_enabled"), "set_pseudolocalization_enabled", "is_pseudolocalization_enabled");607}608609void TranslationServer::load_translations() {610const String prop = "internationalization/locale/translations";611if (!ProjectSettings::get_singleton()->has_setting(prop)) {612return;613}614const Vector<String> &translations = GLOBAL_GET(prop);615for (const String &path : translations) {616Ref<Translation> tr = ResourceLoader::load(path);617if (tr.is_valid()) {618add_translation(tr);619}620}621}622623TranslationServer::TranslationServer() {624singleton = this;625main_domain.instantiate();626editor_domain = get_or_add_domain("godot.editor");627property_domain = get_or_add_domain("godot.properties");628doc_domain = get_or_add_domain("godot.documentation");629init_locale_info();630}631632633