Add methods for querying loaded Translation instances
This commit is contained in:
@@ -36,6 +36,7 @@
|
||||
#include "core/os/main_loop.h"
|
||||
#include "core/os/os.h"
|
||||
#include "core/string/locales.h"
|
||||
#include "core/variant/typed_array.h"
|
||||
|
||||
void TranslationServer::init_locale_info() {
|
||||
// Init locale info.
|
||||
@@ -503,9 +504,27 @@ void TranslationServer::remove_translation(const Ref<Translation> &p_translation
|
||||
main_domain->remove_translation(p_translation);
|
||||
}
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
Ref<Translation> TranslationServer::get_translation_object(const String &p_locale) {
|
||||
return main_domain->get_translation_object(p_locale);
|
||||
}
|
||||
#endif
|
||||
|
||||
TypedArray<Translation> TranslationServer::get_translations() const {
|
||||
return main_domain->get_translations_bind();
|
||||
}
|
||||
|
||||
TypedArray<Translation> TranslationServer::find_translations(const String &p_locale, bool p_exact) const {
|
||||
return main_domain->find_translations_bind(p_locale, p_exact);
|
||||
}
|
||||
|
||||
bool TranslationServer::has_translation(const Ref<Translation> &p_translation) const {
|
||||
return main_domain->has_translation(p_translation);
|
||||
}
|
||||
|
||||
bool TranslationServer::has_translation_for_locale(const String &p_locale, bool p_exact) const {
|
||||
return main_domain->has_translation_for_locale(p_locale, p_exact);
|
||||
}
|
||||
|
||||
void TranslationServer::clear() {
|
||||
main_domain->clear();
|
||||
@@ -576,21 +595,27 @@ void TranslationServer::setup() {
|
||||
String TranslationServer::get_tool_locale() {
|
||||
#ifdef TOOLS_ENABLED
|
||||
if (Engine::get_singleton()->is_editor_hint() || Engine::get_singleton()->is_project_manager_hint()) {
|
||||
if (editor_domain->has_translation_for_locale(locale)) {
|
||||
if (editor_domain->has_translation_for_locale(locale, true)) {
|
||||
return locale;
|
||||
}
|
||||
return "en";
|
||||
} else {
|
||||
#else
|
||||
{
|
||||
#endif
|
||||
// Look for best matching loaded translation.
|
||||
Ref<Translation> t = main_domain->get_translation_object(locale);
|
||||
if (t.is_null()) {
|
||||
return fallback;
|
||||
}
|
||||
return t->get_locale();
|
||||
}
|
||||
#endif
|
||||
|
||||
Ref<Translation> res;
|
||||
int best_score = 0;
|
||||
|
||||
for (const Ref<Translation> &E : main_domain->get_translations()) {
|
||||
int score = TranslationServer::get_singleton()->compare_locales(locale, E->get_locale());
|
||||
if (score > 0 && score >= best_score) {
|
||||
res = E;
|
||||
best_score = score;
|
||||
if (score == 10) {
|
||||
return locale; // Exact match.
|
||||
}
|
||||
}
|
||||
}
|
||||
return res.is_valid() ? res->get_locale() : fallback;
|
||||
}
|
||||
|
||||
bool TranslationServer::is_pseudolocalization_enabled() const {
|
||||
@@ -676,7 +701,15 @@ void TranslationServer::_bind_methods() {
|
||||
|
||||
ClassDB::bind_method(D_METHOD("add_translation", "translation"), &TranslationServer::add_translation);
|
||||
ClassDB::bind_method(D_METHOD("remove_translation", "translation"), &TranslationServer::remove_translation);
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
ClassDB::bind_method(D_METHOD("get_translation_object", "locale"), &TranslationServer::get_translation_object);
|
||||
#endif
|
||||
|
||||
ClassDB::bind_method(D_METHOD("get_translations"), &TranslationServer::get_translations);
|
||||
ClassDB::bind_method(D_METHOD("find_translations", "locale", "exact"), &TranslationServer::find_translations);
|
||||
ClassDB::bind_method(D_METHOD("has_translation_for_locale", "locale", "exact"), &TranslationServer::has_translation_for_locale);
|
||||
ClassDB::bind_method(D_METHOD("has_translation", "translation"), &TranslationServer::has_translation);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("has_domain", "domain"), &TranslationServer::has_domain);
|
||||
ClassDB::bind_method(D_METHOD("get_or_add_domain", "domain"), &TranslationServer::get_or_add_domain);
|
||||
|
||||
Reference in New Issue
Block a user