| 1 | /**************************************************************************/ |
| 2 | /* translation_server.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "translation_server.h" |
| 32 | #include "translation_server.compat.inc" |
| 33 | |
| 34 | #include "core/config/project_settings.h" |
| 35 | #include "core/io/resource_loader.h" |
| 36 | #include "core/os/main_loop.h" |
| 37 | #include "core/os/os.h" |
| 38 | #include "core/string/locales.h" |
| 39 | #include "core/variant/typed_array.h" |
| 40 | |
| 41 | void TranslationServer::init_locale_info() { |
| 42 | // Init locale info. |
| 43 | language_map.clear(); |
| 44 | int idx = 0; |
| 45 | while (language_list[idx][0] != nullptr) { |
| 46 | language_map[language_list[idx][0]] = String::utf8(language_list[idx][1]); |
| 47 | idx++; |
| 48 | } |
| 49 | |
| 50 | // Init locale-script map. |
| 51 | locale_script_info.clear(); |
| 52 | idx = 0; |
| 53 | while (locale_scripts[idx][0] != nullptr) { |
| 54 | LocaleScriptInfo info; |
| 55 | info.name = locale_scripts[idx][0]; |
| 56 | info.script = locale_scripts[idx][1]; |
| 57 | info.default_country = locale_scripts[idx][2]; |
| 58 | Vector<String> supported_countries = String(locale_scripts[idx][3]).split(",", false); |
| 59 | for (int i = 0; i < supported_countries.size(); i++) { |
| 60 | info.supported_countries.insert(supported_countries[i]); |
| 61 | } |
| 62 | locale_script_info.push_back(info); |
| 63 | idx++; |
| 64 | } |
| 65 | |
| 66 | // Init supported script list. |
| 67 | script_map.clear(); |
| 68 | idx = 0; |
| 69 | while (script_list[idx][0] != nullptr) { |
| 70 | script_map[script_list[idx][1]] = String::utf8(script_list[idx][0]); |
| 71 | idx++; |
| 72 | } |
| 73 | |
| 74 | // Init regional variant map. |
| 75 | variant_map.clear(); |
| 76 | idx = 0; |
| 77 | while (locale_variants[idx][0] != nullptr) { |
| 78 | variant_map[locale_variants[idx][0]] = locale_variants[idx][1]; |
| 79 | idx++; |
| 80 | } |
| 81 | |
| 82 | // Init locale renames. |
| 83 | locale_rename_map.clear(); |
| 84 | idx = 0; |
| 85 | while (locale_renames[idx][0] != nullptr) { |
| 86 | if (!String(locale_renames[idx][1]).is_empty()) { |
| 87 | locale_rename_map[locale_renames[idx][0]] = locale_renames[idx][1]; |
| 88 | } |
| 89 | idx++; |
| 90 | } |
| 91 | |
| 92 | // Init locale scripts. |
| 93 | language_script_map.clear(); |
| 94 | idx = 0; |
| 95 | while (language_script_list[idx][0] != nullptr) { |
| 96 | HashSet<String> &set = language_script_map[language_script_list[idx][0]]; |
| 97 | Vector<String> scripts = String(language_script_list[idx][1]).split(" "); |
| 98 | for (const String &s : scripts) { |
| 99 | set.insert(s); |
| 100 | } |
| 101 | idx++; |
| 102 | } |
| 103 | |
| 104 | // Init country names. |
| 105 | country_name_map.clear(); |
| 106 | idx = 0; |
| 107 | while (country_names[idx][0] != nullptr) { |
| 108 | country_name_map[String(country_names[idx][0])] = String::utf8(country_names[idx][1]); |
| 109 | idx++; |
| 110 | } |
| 111 | |
| 112 | // Init country renames. |
| 113 | country_rename_map.clear(); |
| 114 | idx = 0; |
| 115 | while (country_renames[idx][0] != nullptr) { |
| 116 | if (!String(country_renames[idx][1]).is_empty()) { |
| 117 | country_rename_map[country_renames[idx][0]] = country_renames[idx][1]; |
| 118 | } |
| 119 | idx++; |
| 120 | } |
| 121 | |
| 122 | // Init plural rules. |
| 123 | plural_rules_map.clear(); |
| 124 | idx = 0; |
| 125 | while (plural_rules[idx][0] != nullptr) { |
| 126 | const Vector<String> rule_locs = String(plural_rules[idx][0]).split(" "); |
| 127 | const String rule = String(plural_rules[idx][1]); |
| 128 | for (const String &l : rule_locs) { |
| 129 | plural_rules_map[l] = rule; |
| 130 | } |
| 131 | idx++; |
| 132 | } |
| 133 | |
| 134 | // Init number systems. |
| 135 | num_system_map.clear(); |
| 136 | idx = 0; |
| 137 | while (num_system_data[idx].locales != nullptr) { |
| 138 | const NumSystemData &nsd = num_system_data[idx]; |
| 139 | |
| 140 | // These fields must not be empty. |
| 141 | DEV_ASSERT(nsd.percent_sign && nsd.percent_sign[0] != '\0'); |
| 142 | DEV_ASSERT(nsd.digits && nsd.digits[0] != '\0'); |
| 143 | DEV_ASSERT(nsd.exp_l && nsd.exp_l[0] != '\0'); |
| 144 | DEV_ASSERT(nsd.exp_u && nsd.exp_u[0] != '\0'); |
| 145 | DEV_ASSERT(strlen(nsd.digits) == 11); |
| 146 | |
| 147 | const Vector<String> locales = String(nsd.locales).split(" "); |
| 148 | for (const String &l : locales) { |
| 149 | num_system_map[l] = idx; |
| 150 | } |
| 151 | idx++; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | TranslationServer::Locale::operator String() const { |
| 156 | String out = language; |
| 157 | if (!script.is_empty()) { |
| 158 | out = out + "_" + script; |
| 159 | } |
| 160 | if (!country.is_empty()) { |
| 161 | out = out + "_" + country; |
| 162 | } |
| 163 | if (!variant.is_empty()) { |
| 164 | out = out + "_" + variant; |
| 165 | } |
| 166 | return out; |
| 167 | } |
| 168 | |
| 169 | TranslationServer::Locale::Locale(const TranslationServer &p_server, const String &p_locale, bool p_add_defaults) { |
| 170 | // Replaces '-' with '_' for macOS style locales. |
| 171 | String univ_locale = p_locale.replace_char('-', '_'); |
| 172 | |
| 173 | // Extract locale elements. |
| 174 | Vector<String> locale_elements = univ_locale.get_slicec('@', 0).split("_"); |
| 175 | language = locale_elements[0]; |
| 176 | if (locale_elements.size() >= 2) { |
| 177 | if (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])) { |
| 178 | script = locale_elements[1]; |
| 179 | } |
| 180 | if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) { |
| 181 | country = locale_elements[1]; |
| 182 | } |
| 183 | } |
| 184 | if (locale_elements.size() >= 3) { |
| 185 | if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) { |
| 186 | country = locale_elements[2]; |
| 187 | } else if (p_server.variant_map.has(locale_elements[2].to_lower()) && p_server.variant_map[locale_elements[2].to_lower()] == language) { |
| 188 | variant = locale_elements[2].to_lower(); |
| 189 | } |
| 190 | } |
| 191 | if (locale_elements.size() >= 4) { |
| 192 | if (p_server.variant_map.has(locale_elements[3].to_lower()) && p_server.variant_map[locale_elements[3].to_lower()] == language) { |
| 193 | variant = locale_elements[3].to_lower(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // Try extract script and variant from the extra part. |
| 198 | Vector<String> script_extra = univ_locale.get_slicec('@', 1).split(";"); |
| 199 | for (int i = 0; i < script_extra.size(); i++) { |
| 200 | if (script_extra[i].to_lower() == "cyrillic") { |
| 201 | script = "Cyrl"; |
| 202 | break; |
| 203 | } else if (script_extra[i].to_lower() == "latin") { |
| 204 | script = "Latn"; |
| 205 | break; |
| 206 | } else if (script_extra[i].to_lower() == "devanagari") { |
| 207 | script = "Deva"; |
| 208 | break; |
| 209 | } else if (p_server.variant_map.has(script_extra[i].to_lower()) && p_server.variant_map[script_extra[i].to_lower()] == language) { |
| 210 | variant = script_extra[i].to_lower(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // Handles known non-ISO language names used e.g. on Windows. |
| 215 | if (p_server.locale_rename_map.has(language)) { |
| 216 | language = p_server.locale_rename_map[language]; |
| 217 | } |
| 218 | |
| 219 | // Handle country renames. |
| 220 | if (p_server.country_rename_map.has(country)) { |
| 221 | country = p_server.country_rename_map[country]; |
| 222 | } |
| 223 | |
| 224 | // Remove unsupported script codes. |
| 225 | if (!p_server.script_map.has(script)) { |
| 226 | script = ""; |
| 227 | } |
| 228 | |
| 229 | // Add script code base on language and country codes for some ambiguous cases. |
| 230 | if (p_add_defaults) { |
| 231 | if (script.is_empty()) { |
| 232 | for (int i = 0; i < p_server.locale_script_info.size(); i++) { |
| 233 | const LocaleScriptInfo &info = p_server.locale_script_info[i]; |
| 234 | if (info.name == language) { |
| 235 | if (country.is_empty() || info.supported_countries.has(country)) { |
| 236 | script = info.script; |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | if (!script.is_empty() && country.is_empty()) { |
| 243 | // Add conntry code based on script for some ambiguous cases. |
| 244 | for (int i = 0; i < p_server.locale_script_info.size(); i++) { |
| 245 | const LocaleScriptInfo &info = p_server.locale_script_info[i]; |
| 246 | if (info.name == language && info.script == script) { |
| 247 | country = info.default_country; |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | String TranslationServer::format_number(const String &p_string, const String &p_locale) const { |
| 256 | ERR_FAIL_COND_V(p_locale.is_empty(), p_string); |
| 257 | if (!num_system_map.has(p_locale)) { |
| 258 | return p_string; |
| 259 | } |
| 260 | |
| 261 | int index = num_system_map[p_locale]; |
| 262 | const NumSystemData &nsd = num_system_data[index]; |
| 263 | |
| 264 | String res = p_string; |
| 265 | res = res.replace("e", nsd.exp_l); |
| 266 | res = res.replace("E", nsd.exp_u); |
| 267 | char32_t *data = res.ptrw(); |
| 268 | for (int j = 0; j < res.length(); j++) { |
| 269 | if (data[j] >= 0x30 && data[j] <= 0x39) { |
| 270 | data[j] = nsd.digits[data[j] - 0x30]; |
| 271 | } else if (data[j] == '.' || data[j] == ',') { |
| 272 | data[j] = nsd.digits[10]; |
| 273 | } |
| 274 | } |
| 275 | return res; |
| 276 | } |
| 277 | |
| 278 | String TranslationServer::parse_number(const String &p_string, const String &p_locale) const { |
| 279 | ERR_FAIL_COND_V(p_locale.is_empty(), p_string); |
| 280 | if (!num_system_map.has(p_locale)) { |
| 281 | return p_string; |
| 282 | } |
| 283 | |
| 284 | int index = num_system_map[p_locale]; |
| 285 | const NumSystemData &nsd = num_system_data[index]; |
| 286 | |
| 287 | String res = p_string; |
| 288 | res = res.replace(nsd.exp_l, "e"); |
| 289 | res = res.replace(nsd.exp_u, "E"); |
| 290 | char32_t *data = res.ptrw(); |
| 291 | for (int j = 0; j < res.length(); j++) { |
| 292 | if (data[j] == nsd.digits[10]) { |
| 293 | data[j] = '.'; |
| 294 | } else { |
| 295 | for (int k = 0; k < 10; k++) { |
| 296 | if (data[j] == nsd.digits[k]) { |
| 297 | data[j] = 0x30 + k; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | return res; |
| 303 | } |
| 304 | |
| 305 | String TranslationServer::get_percent_sign(const String &p_locale) const { |
| 306 | ERR_FAIL_COND_V(p_locale.is_empty(), "%"); |
| 307 | if (!num_system_map.has(p_locale)) { |
| 308 | return "%"; |
| 309 | } |
| 310 | |
| 311 | int index = num_system_map[p_locale]; |
| 312 | return num_system_data[index].percent_sign; |
| 313 | } |
| 314 | |
| 315 | String TranslationServer::standardize_locale(const String &p_locale, bool p_add_defaults) const { |
| 316 | return Locale(*this, p_locale, p_add_defaults).operator String(); |
| 317 | } |
| 318 | |
| 319 | int TranslationServer::compare_locales(const String &p_locale_a, const String &p_locale_b) const { |
| 320 | if (p_locale_a == p_locale_b) { |
| 321 | // Exact match. |
| 322 | return 10; |
| 323 | } |
| 324 | |
| 325 | const String cache_key = p_locale_a + "|" + p_locale_b; |
| 326 | const int *cached_result = locale_compare_cache.getptr(cache_key); |
| 327 | if (cached_result) { |
| 328 | return *cached_result; |
| 329 | } |
| 330 | |
| 331 | Locale locale_a = Locale(*this, p_locale_a, true); |
| 332 | Locale locale_b = Locale(*this, p_locale_b, true); |
| 333 | |
| 334 | if (locale_a == locale_b) { |
| 335 | // Exact match. |
| 336 | locale_compare_cache.insert(cache_key, 10); |
| 337 | return 10; |
| 338 | } |
| 339 | |
| 340 | if (locale_a.language != locale_b.language) { |
| 341 | // No match. |
| 342 | locale_compare_cache.insert(cache_key, 0); |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | // Matching language, both locales have extra parts. Compare the |
| 347 | // remaining elements. If both elements are non-empty, check the |
| 348 | // match to increase or decrease the score. If either element or |
| 349 | // both are empty, leave the score as is. |
| 350 | int score = 5; |
| 351 | if (!locale_a.script.is_empty() && !locale_b.script.is_empty()) { |
| 352 | if (locale_a.script == locale_b.script) { |
| 353 | score++; |
| 354 | } else { |
| 355 | score--; |
| 356 | } |
| 357 | } |
| 358 | if (!locale_a.country.is_empty() && !locale_b.country.is_empty()) { |
| 359 | if (locale_a.country == locale_b.country) { |
| 360 | score++; |
| 361 | } else { |
| 362 | score--; |
| 363 | } |
| 364 | } |
| 365 | if (!locale_a.variant.is_empty() && !locale_b.variant.is_empty()) { |
| 366 | if (locale_a.variant == locale_b.variant) { |
| 367 | score++; |
| 368 | } else { |
| 369 | score--; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | locale_compare_cache.insert(cache_key, score); |
| 374 | return score; |
| 375 | } |
| 376 | |
| 377 | String TranslationServer::get_locale_name(const String &p_locale) const { |
| 378 | String lang_name, script_name, country_name; |
| 379 | Vector<String> locale_elements = standardize_locale(p_locale).split("_"); |
| 380 | lang_name = locale_elements[0]; |
| 381 | if (locale_elements.size() >= 2) { |
| 382 | if (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])) { |
| 383 | script_name = locale_elements[1]; |
| 384 | } |
| 385 | if (locale_elements[1].length() == 2 && is_ascii_upper_case(locale_elements[1][0]) && is_ascii_upper_case(locale_elements[1][1])) { |
| 386 | country_name = locale_elements[1]; |
| 387 | } |
| 388 | } |
| 389 | if (locale_elements.size() >= 3) { |
| 390 | if (locale_elements[2].length() == 2 && is_ascii_upper_case(locale_elements[2][0]) && is_ascii_upper_case(locale_elements[2][1])) { |
| 391 | country_name = locale_elements[2]; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | String name = get_language_name(lang_name); |
| 396 | if (!script_name.is_empty()) { |
| 397 | name = name + " (" + get_script_name(script_name) + ")"; |
| 398 | } |
| 399 | if (!country_name.is_empty()) { |
| 400 | name = name + ", " + get_country_name(country_name); |
| 401 | } |
| 402 | return name; |
| 403 | } |
| 404 | |
| 405 | String TranslationServer::get_plural_rules(const String &p_locale) const { |
| 406 | const String *rule = plural_rules_map.getptr(p_locale); |
| 407 | if (rule) { |
| 408 | return *rule; |
| 409 | } |
| 410 | |
| 411 | Locale l = Locale(*this, p_locale, false); |
| 412 | if (!l.country.is_empty()) { |
| 413 | rule = plural_rules_map.getptr(l.language + "_" + l.country); |
| 414 | if (rule) { |
| 415 | return *rule; |
| 416 | } |
| 417 | } |
| 418 | rule = plural_rules_map.getptr(l.language); |
| 419 | if (rule) { |
| 420 | return *rule; |
| 421 | } |
| 422 | return String(); |
| 423 | } |
| 424 | |
| 425 | Vector<String> TranslationServer::get_all_languages() const { |
| 426 | Vector<String> languages; |
| 427 | |
| 428 | for (const KeyValue<String, String> &E : language_map) { |
| 429 | languages.push_back(E.key); |
| 430 | } |
| 431 | |
| 432 | return languages; |
| 433 | } |
| 434 | |
| 435 | String TranslationServer::get_language_name(const String &p_language) const { |
| 436 | if (language_map.has(p_language)) { |
| 437 | return language_map[p_language]; |
| 438 | } else { |
| 439 | return p_language; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | Vector<String> TranslationServer::get_all_scripts() const { |
| 444 | Vector<String> scripts; |
| 445 | |
| 446 | for (const KeyValue<String, String> &E : script_map) { |
| 447 | scripts.push_back(E.key); |
| 448 | } |
| 449 | |
| 450 | return scripts; |
| 451 | } |
| 452 | |
| 453 | String TranslationServer::get_script_name(const String &p_script) const { |
| 454 | if (script_map.has(p_script)) { |
| 455 | return script_map[p_script]; |
| 456 | } else { |
| 457 | return p_script; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | Vector<String> TranslationServer::get_all_countries() const { |
| 462 | Vector<String> countries; |
| 463 | |
| 464 | for (const KeyValue<String, String> &E : country_name_map) { |
| 465 | countries.push_back(E.key); |
| 466 | } |
| 467 | |
| 468 | return countries; |
| 469 | } |
| 470 | |
| 471 | String TranslationServer::get_country_name(const String &p_country) const { |
| 472 | if (country_name_map.has(p_country)) { |
| 473 | return country_name_map[p_country]; |
| 474 | } else { |
| 475 | return p_country; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | void TranslationServer::set_locale(const String &p_locale) { |
| 480 | ERR_FAIL_COND_MSG(p_locale.is_empty(), "Locale cannot be an empty string."); |
| 481 | |
| 482 | String new_locale = standardize_locale(p_locale); |
| 483 | if (locale == new_locale) { |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | locale = new_locale; |
| 488 | ResourceLoader::reload_translation_remaps(); |
| 489 | |
| 490 | if (OS::get_singleton()->get_main_loop()) { |
| 491 | OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | String TranslationServer::get_locale() const { |
| 496 | return locale; |
| 497 | } |
| 498 | |
| 499 | void TranslationServer::set_fallback_locale(const String &p_locale) { |
| 500 | fallback = p_locale; |
| 501 | } |
| 502 | |
| 503 | String TranslationServer::get_fallback_locale() const { |
| 504 | return fallback; |
| 505 | } |
| 506 | |
| 507 | bool TranslationServer::is_script_suppored_by_locale(const String &p_locale, const String &p_script) const { |
| 508 | Locale l = Locale(*this, p_locale, true); |
| 509 | if (l.script == p_script) { |
| 510 | return true; |
| 511 | } |
| 512 | if (!language_script_map.has(l.language)) { |
| 513 | return false; |
| 514 | } |
| 515 | return language_script_map[l.language].has(p_script); |
| 516 | } |
| 517 | |
| 518 | PackedStringArray TranslationServer::get_loaded_locales() const { |
| 519 | return main_domain->get_loaded_locales(); |
| 520 | } |
| 521 | |
| 522 | void TranslationServer::add_translation(const Ref<Translation> &p_translation) { |
| 523 | main_domain->add_translation(p_translation); |
| 524 | } |
| 525 | |
| 526 | void TranslationServer::remove_translation(const Ref<Translation> &p_translation) { |
| 527 | main_domain->remove_translation(p_translation); |
| 528 | } |
| 529 | |
| 530 | #ifndef DISABLE_DEPRECATED |
| 531 | Ref<Translation> TranslationServer::get_translation_object(const String &p_locale) { |
| 532 | return main_domain->get_translation_object(p_locale); |
| 533 | } |
| 534 | #endif |
| 535 | |
| 536 | TypedArray<Translation> TranslationServer::get_translations() const { |
| 537 | return main_domain->get_translations_bind(); |
| 538 | } |
| 539 | |
| 540 | TypedArray<Translation> TranslationServer::find_translations(const String &p_locale, bool p_exact) const { |
| 541 | return main_domain->find_translations_bind(p_locale, p_exact); |
| 542 | } |
| 543 | |
| 544 | bool TranslationServer::has_translation(const Ref<Translation> &p_translation) const { |
| 545 | return main_domain->has_translation(p_translation); |
| 546 | } |
| 547 | |
| 548 | bool TranslationServer::has_translation_for_locale(const String &p_locale, bool p_exact) const { |
| 549 | return main_domain->has_translation_for_locale(p_locale, p_exact); |
| 550 | } |
| 551 | |
| 552 | void TranslationServer::clear() { |
| 553 | main_domain->clear(); |
| 554 | } |
| 555 | |
| 556 | StringName TranslationServer::translate(const StringName &p_message, const StringName &p_context) const { |
| 557 | return main_domain->translate(p_message, p_context); |
| 558 | } |
| 559 | |
| 560 | StringName TranslationServer::translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const { |
| 561 | return main_domain->translate_plural(p_message, p_message_plural, p_n, p_context); |
| 562 | } |
| 563 | |
| 564 | bool TranslationServer::has_domain(const StringName &p_domain) const { |
| 565 | if (p_domain == StringName()) { |
| 566 | return true; |
| 567 | } |
| 568 | return custom_domains.has(p_domain); |
| 569 | } |
| 570 | |
| 571 | Ref<TranslationDomain> TranslationServer::get_or_add_domain(const StringName &p_domain) { |
| 572 | if (p_domain == StringName()) { |
| 573 | return main_domain; |
| 574 | } |
| 575 | const Ref<TranslationDomain> *domain = custom_domains.getptr(p_domain); |
| 576 | if (domain) { |
| 577 | if (domain->is_valid()) { |
| 578 | return *domain; |
| 579 | } |
| 580 | ERR_PRINT("Bug (please report): Found invalid translation domain."); |
| 581 | } |
| 582 | Ref<TranslationDomain> new_domain = memnew(TranslationDomain); |
| 583 | custom_domains[p_domain] = new_domain; |
| 584 | return new_domain; |
| 585 | } |
| 586 | |
| 587 | void TranslationServer::remove_domain(const StringName &p_domain) { |
| 588 | ERR_FAIL_COND_MSG(p_domain == StringName(), "Cannot remove main translation domain."); |
| 589 | custom_domains.erase(p_domain); |
| 590 | } |
| 591 | |
| 592 | void TranslationServer::setup() { |
| 593 | String test = GLOBAL_DEF("internationalization/locale/test", ""); |
| 594 | test = test.strip_edges(); |
| 595 | if (!test.is_empty()) { |
| 596 | set_locale(test); |
| 597 | } else { |
| 598 | set_locale(OS::get_singleton()->get_locale()); |
| 599 | } |
| 600 | |
| 601 | fallback = GLOBAL_DEF("internationalization/locale/fallback", "en"); |
| 602 | main_domain->set_pseudolocalization_enabled(GLOBAL_DEF("internationalization/pseudolocalization/use_pseudolocalization", false)); |
| 603 | main_domain->set_pseudolocalization_accents_enabled(GLOBAL_DEF("internationalization/pseudolocalization/replace_with_accents", true)); |
| 604 | main_domain->set_pseudolocalization_double_vowels_enabled(GLOBAL_DEF("internationalization/pseudolocalization/double_vowels", false)); |
| 605 | main_domain->set_pseudolocalization_fake_bidi_enabled(GLOBAL_DEF("internationalization/pseudolocalization/fake_bidi", false)); |
| 606 | main_domain->set_pseudolocalization_override_enabled(GLOBAL_DEF("internationalization/pseudolocalization/override", false)); |
| 607 | main_domain->set_pseudolocalization_expansion_ratio(GLOBAL_DEF("internationalization/pseudolocalization/expansion_ratio", 0.0)); |
| 608 | main_domain->set_pseudolocalization_prefix(GLOBAL_DEF("internationalization/pseudolocalization/prefix", "[")); |
| 609 | main_domain->set_pseudolocalization_suffix(GLOBAL_DEF("internationalization/pseudolocalization/suffix", "]")); |
| 610 | main_domain->set_pseudolocalization_skip_placeholders_enabled(GLOBAL_DEF("internationalization/pseudolocalization/skip_placeholders", true)); |
| 611 | |
| 612 | #ifdef TOOLS_ENABLED |
| 613 | ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "internationalization/locale/test", PROPERTY_HINT_LOCALE_ID, "")); |
| 614 | ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "internationalization/locale/fallback", PROPERTY_HINT_LOCALE_ID, "")); |
| 615 | #endif |
| 616 | } |
| 617 | |
| 618 | String TranslationServer::get_tool_locale() { |
| 619 | #ifdef TOOLS_ENABLED |
| 620 | if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) { |
| 621 | if (editor_domain->has_translation_for_locale(locale, true)) { |
| 622 | return locale; |
| 623 | } |
| 624 | return "en"; |
| 625 | } |
| 626 | #endif |
| 627 | |
| 628 | Ref<Translation> res; |
| 629 | int best_score = 0; |
| 630 | |
| 631 | for (const Ref<Translation> &E : main_domain->get_translations()) { |
| 632 | int score = TranslationServer::get_singleton()->compare_locales(locale, E->get_locale()); |
| 633 | if (score > 0 && score >= best_score) { |
| 634 | res = E; |
| 635 | best_score = score; |
| 636 | if (score == 10) { |
| 637 | return locale; // Exact match. |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | return res.is_valid() ? res->get_locale() : fallback; |
| 642 | } |
| 643 | |
| 644 | bool TranslationServer::is_pseudolocalization_enabled() const { |
| 645 | return main_domain->is_pseudolocalization_enabled(); |
| 646 | } |
| 647 | |
| 648 | void TranslationServer::set_pseudolocalization_enabled(bool p_enabled) { |
| 649 | main_domain->set_pseudolocalization_enabled(p_enabled); |
| 650 | |
| 651 | ResourceLoader::reload_translation_remaps(); |
| 652 | |
| 653 | if (OS::get_singleton()->get_main_loop()) { |
| 654 | OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED); |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | void TranslationServer::reload_pseudolocalization() { |
| 659 | main_domain->set_pseudolocalization_accents_enabled(GLOBAL_GET("internationalization/pseudolocalization/replace_with_accents")); |
| 660 | main_domain->set_pseudolocalization_double_vowels_enabled(GLOBAL_GET("internationalization/pseudolocalization/double_vowels")); |
| 661 | main_domain->set_pseudolocalization_fake_bidi_enabled(GLOBAL_GET("internationalization/pseudolocalization/fake_bidi")); |
| 662 | main_domain->set_pseudolocalization_override_enabled(GLOBAL_GET("internationalization/pseudolocalization/override")); |
| 663 | main_domain->set_pseudolocalization_expansion_ratio(GLOBAL_GET("internationalization/pseudolocalization/expansion_ratio")); |
| 664 | main_domain->set_pseudolocalization_prefix(GLOBAL_GET("internationalization/pseudolocalization/prefix")); |
| 665 | main_domain->set_pseudolocalization_suffix(GLOBAL_GET("internationalization/pseudolocalization/suffix")); |
| 666 | main_domain->set_pseudolocalization_skip_placeholders_enabled(GLOBAL_GET("internationalization/pseudolocalization/skip_placeholders")); |
| 667 | |
| 668 | ResourceLoader::reload_translation_remaps(); |
| 669 | |
| 670 | if (OS::get_singleton()->get_main_loop()) { |
| 671 | OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | StringName TranslationServer::pseudolocalize(const StringName &p_message) const { |
| 676 | return main_domain->pseudolocalize(p_message); |
| 677 | } |
| 678 | |
| 679 | #ifdef TOOLS_ENABLED |
| 680 | void TranslationServer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const { |
| 681 | const String pf = p_function; |
| 682 | if (p_idx == 0) { |
| 683 | HashMap<String, String> *target_hash_map = nullptr; |
| 684 | if (pf == "get_language_name") { |
| 685 | target_hash_map = &language_map; |
| 686 | } else if (pf == "get_script_name") { |
| 687 | target_hash_map = &script_map; |
| 688 | } else if (pf == "get_country_name") { |
| 689 | target_hash_map = &country_name_map; |
| 690 | } |
| 691 | |
| 692 | if (target_hash_map) { |
| 693 | for (const KeyValue<String, String> &E : *target_hash_map) { |
| 694 | r_options->push_back(E.key.quote()); |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | Object::get_argument_options(p_function, p_idx, r_options); |
| 699 | } |
| 700 | #endif // TOOLS_ENABLED |
| 701 | |
| 702 | void TranslationServer::_bind_methods() { |
| 703 | ClassDB::bind_method(D_METHOD("set_locale", "locale"), &TranslationServer::set_locale); |
| 704 | ClassDB::bind_method(D_METHOD("get_locale"), &TranslationServer::get_locale); |
| 705 | ClassDB::bind_method(D_METHOD("get_tool_locale"), &TranslationServer::get_tool_locale); |
| 706 | |
| 707 | ClassDB::bind_method(D_METHOD("compare_locales", "locale_a", "locale_b"), &TranslationServer::compare_locales); |
| 708 | ClassDB::bind_method(D_METHOD("standardize_locale", "locale", "add_defaults"), &TranslationServer::standardize_locale, DEFVAL(false)); |
| 709 | |
| 710 | ClassDB::bind_method(D_METHOD("get_all_languages"), &TranslationServer::get_all_languages); |
| 711 | ClassDB::bind_method(D_METHOD("get_language_name", "language"), &TranslationServer::get_language_name); |
| 712 | |
| 713 | ClassDB::bind_method(D_METHOD("get_all_scripts"), &TranslationServer::get_all_scripts); |
| 714 | ClassDB::bind_method(D_METHOD("get_script_name", "script"), &TranslationServer::get_script_name); |
| 715 | |
| 716 | ClassDB::bind_method(D_METHOD("get_all_countries"), &TranslationServer::get_all_countries); |
| 717 | ClassDB::bind_method(D_METHOD("get_country_name", "country"), &TranslationServer::get_country_name); |
| 718 | |
| 719 | ClassDB::bind_method(D_METHOD("get_locale_name", "locale"), &TranslationServer::get_locale_name); |
| 720 | ClassDB::bind_method(D_METHOD("get_plural_rules", "locale"), &TranslationServer::get_plural_rules); |
| 721 | |
| 722 | ClassDB::bind_method(D_METHOD("translate", "message", "context"), &TranslationServer::translate, DEFVAL(StringName())); |
| 723 | ClassDB::bind_method(D_METHOD("translate_plural", "message", "plural_message", "n", "context"), &TranslationServer::translate_plural, DEFVAL(StringName())); |
| 724 | |
| 725 | ClassDB::bind_method(D_METHOD("add_translation", "translation"), &TranslationServer::add_translation); |
| 726 | ClassDB::bind_method(D_METHOD("remove_translation", "translation"), &TranslationServer::remove_translation); |
| 727 | |
| 728 | #ifndef DISABLE_DEPRECATED |
| 729 | ClassDB::bind_method(D_METHOD("get_translation_object", "locale"), &TranslationServer::get_translation_object); |
| 730 | #endif |
| 731 | |
| 732 | ClassDB::bind_method(D_METHOD("get_translations"), &TranslationServer::get_translations); |
| 733 | ClassDB::bind_method(D_METHOD("find_translations", "locale", "exact"), &TranslationServer::find_translations); |
| 734 | ClassDB::bind_method(D_METHOD("has_translation_for_locale", "locale", "exact"), &TranslationServer::has_translation_for_locale); |
| 735 | ClassDB::bind_method(D_METHOD("has_translation", "translation"), &TranslationServer::has_translation); |
| 736 | |
| 737 | ClassDB::bind_method(D_METHOD("has_domain", "domain"), &TranslationServer::has_domain); |
| 738 | ClassDB::bind_method(D_METHOD("get_or_add_domain", "domain"), &TranslationServer::get_or_add_domain); |
| 739 | ClassDB::bind_method(D_METHOD("remove_domain", "domain"), &TranslationServer::remove_domain); |
| 740 | |
| 741 | ClassDB::bind_method(D_METHOD("clear"), &TranslationServer::clear); |
| 742 | |
| 743 | ClassDB::bind_method(D_METHOD("get_loaded_locales"), &TranslationServer::get_loaded_locales); |
| 744 | |
| 745 | ClassDB::bind_method(D_METHOD("format_number", "number", "locale"), &TranslationServer::format_number); |
| 746 | ClassDB::bind_method(D_METHOD("get_percent_sign", "locale"), &TranslationServer::get_percent_sign); |
| 747 | ClassDB::bind_method(D_METHOD("parse_number", "number", "locale"), &TranslationServer::parse_number); |
| 748 | |
| 749 | ClassDB::bind_method(D_METHOD("is_pseudolocalization_enabled"), &TranslationServer::is_pseudolocalization_enabled); |
| 750 | ClassDB::bind_method(D_METHOD("set_pseudolocalization_enabled", "enabled"), &TranslationServer::set_pseudolocalization_enabled); |
| 751 | ClassDB::bind_method(D_METHOD("reload_pseudolocalization"), &TranslationServer::reload_pseudolocalization); |
| 752 | ClassDB::bind_method(D_METHOD("pseudolocalize", "message"), &TranslationServer::pseudolocalize); |
| 753 | ADD_PROPERTY(PropertyInfo(Variant::Type::BOOL, "pseudolocalization_enabled"), "set_pseudolocalization_enabled", "is_pseudolocalization_enabled"); |
| 754 | } |
| 755 | |
| 756 | void TranslationServer::load_project_translations(Ref<TranslationDomain> p_domain) { |
| 757 | DEV_ASSERT(p_domain.is_valid()); |
| 758 | |
| 759 | p_domain->clear(); |
| 760 | const String prop = "internationalization/locale/translations"; |
| 761 | if (!ProjectSettings::get_singleton()->has_setting(prop)) { |
| 762 | return; |
| 763 | } |
| 764 | const Vector<String> &translations = GLOBAL_GET(prop); |
| 765 | for (const String &path : translations) { |
| 766 | Ref<Translation> tr = ResourceLoader::load(path); |
| 767 | if (tr.is_valid()) { |
| 768 | p_domain->add_translation(tr); |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | TranslationServer::TranslationServer() { |
| 774 | singleton = this; |
| 775 | |
| 776 | main_domain.instantiate(); |
| 777 | |
| 778 | #ifdef TOOLS_ENABLED |
| 779 | editor_domain = get_or_add_domain("godot.editor"); |
| 780 | property_domain = get_or_add_domain("godot.properties"); |
| 781 | doc_domain = get_or_add_domain("godot.documentation"); |
| 782 | #endif // TOOLS_ENABLED |
| 783 | |
| 784 | init_locale_info(); |
| 785 | } |