summaryrefslogtreecommitdiff
path: root/core/translation.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/translation.cpp')
-rw-r--r--core/translation.cpp72
1 files changed, 21 insertions, 51 deletions
diff --git a/core/translation.cpp b/core/translation.cpp
index 3f45bb17c9..4f835bd7b4 100644
--- a/core/translation.cpp
+++ b/core/translation.cpp
@@ -795,12 +795,10 @@ static const char *locale_renames[][2] = {
///////////////////////////////////////////////
Vector<String> Translation::_get_messages() const {
-
Vector<String> msgs;
msgs.resize(translation_map.size() * 2);
int idx = 0;
for (const Map<StringName, StringName>::Element *E = translation_map.front(); E; E = E->next()) {
-
msgs.set(idx + 0, E->key());
msgs.set(idx + 1, E->get());
idx += 2;
@@ -810,12 +808,10 @@ Vector<String> Translation::_get_messages() const {
}
Vector<String> Translation::_get_message_list() const {
-
Vector<String> msgs;
msgs.resize(translation_map.size());
int idx = 0;
for (const Map<StringName, StringName>::Element *E = translation_map.front(); E; E = E->next()) {
-
msgs.set(idx, E->key());
idx += 1;
}
@@ -824,20 +820,17 @@ Vector<String> Translation::_get_message_list() const {
}
void Translation::_set_messages(const Vector<String> &p_messages) {
-
int msg_count = p_messages.size();
ERR_FAIL_COND(msg_count % 2);
const String *r = p_messages.ptr();
for (int i = 0; i < msg_count; i += 2) {
-
add_message(r[i + 0], r[i + 1]);
}
}
void Translation::set_locale(const String &p_locale) {
-
String univ_locale = TranslationServer::standardize_locale(p_locale);
if (!TranslationServer::is_locale_valid(univ_locale)) {
@@ -856,38 +849,33 @@ void Translation::set_locale(const String &p_locale) {
}
void Translation::add_message(const StringName &p_src_text, const StringName &p_xlated_text) {
-
translation_map[p_src_text] = p_xlated_text;
}
-StringName Translation::get_message(const StringName &p_src_text) const {
+StringName Translation::get_message(const StringName &p_src_text) const {
const Map<StringName, StringName>::Element *E = translation_map.find(p_src_text);
- if (!E)
+ if (!E) {
return StringName();
+ }
return E->get();
}
void Translation::erase_message(const StringName &p_src_text) {
-
translation_map.erase(p_src_text);
}
void Translation::get_message_list(List<StringName> *r_messages) const {
-
for (const Map<StringName, StringName>::Element *E = translation_map.front(); E; E = E->next()) {
-
r_messages->push_back(E->key());
}
}
int Translation::get_message_count() const {
-
return translation_map.size();
-};
+}
void Translation::_bind_methods() {
-
ClassDB::bind_method(D_METHOD("set_locale", "locale"), &Translation::set_locale);
ClassDB::bind_method(D_METHOD("get_locale"), &Translation::get_locale);
ClassDB::bind_method(D_METHOD("add_message", "src_message", "xlated_message"), &Translation::add_message);
@@ -902,20 +890,15 @@ void Translation::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::STRING, "locale"), "set_locale", "get_locale");
}
-Translation::Translation() :
- locale("en") {
-}
-
///////////////////////////////////////////////
bool TranslationServer::is_locale_valid(const String &p_locale) {
-
const char **ptr = locale_list;
while (*ptr) {
-
- if (*ptr == p_locale)
+ if (*ptr == p_locale) {
return true;
+ }
ptr++;
}
@@ -923,7 +906,6 @@ bool TranslationServer::is_locale_valid(const String &p_locale) {
}
String TranslationServer::standardize_locale(const String &p_locale) {
-
// Replaces '-' with '_' for macOS Sierra-style locales
String univ_locale = p_locale.replace("-", "_");
@@ -941,7 +923,6 @@ String TranslationServer::standardize_locale(const String &p_locale) {
}
String TranslationServer::get_language_code(const String &p_locale) {
-
ERR_FAIL_COND_V_MSG(p_locale.length() < 2, p_locale, "Invalid locale '" + p_locale + "'.");
// Most language codes are two letters, but some are three,
// so we have to look for a regional code separator ('_' or '-')
@@ -958,7 +939,6 @@ String TranslationServer::get_language_code(const String &p_locale) {
}
void TranslationServer::set_locale(const String &p_locale) {
-
String univ_locale = standardize_locale(p_locale);
if (!is_locale_valid(univ_locale)) {
@@ -983,20 +963,19 @@ void TranslationServer::set_locale(const String &p_locale) {
}
String TranslationServer::get_locale() const {
-
return locale;
}
String TranslationServer::get_locale_name(const String &p_locale) const {
-
- if (!locale_name_map.has(p_locale)) return String();
+ if (!locale_name_map.has(p_locale)) {
+ return String();
+ }
return locale_name_map[p_locale];
}
Array TranslationServer::get_loaded_locales() const {
Array locales;
for (const Set<Ref<Translation>>::Element *E = translations.front(); E; E = E->next()) {
-
const Ref<Translation> &t = E->get();
ERR_FAIL_COND_V(t.is_null(), Array());
String l = t->get_locale();
@@ -1008,7 +987,6 @@ Array TranslationServer::get_loaded_locales() const {
}
Vector<String> TranslationServer::get_all_locales() {
-
Vector<String> locales;
const char **ptr = locale_list;
@@ -1022,7 +1000,6 @@ Vector<String> TranslationServer::get_all_locales() {
}
Vector<String> TranslationServer::get_all_locale_names() {
-
Vector<String> locales;
const char **ptr = locale_names;
@@ -1036,25 +1013,23 @@ Vector<String> TranslationServer::get_all_locale_names() {
}
void TranslationServer::add_translation(const Ref<Translation> &p_translation) {
-
translations.insert(p_translation);
}
-void TranslationServer::remove_translation(const Ref<Translation> &p_translation) {
+void TranslationServer::remove_translation(const Ref<Translation> &p_translation) {
translations.erase(p_translation);
}
void TranslationServer::clear() {
-
translations.clear();
-};
+}
StringName TranslationServer::translate(const StringName &p_message) const {
-
// Match given message against the translation catalog for the project locale.
- if (!enabled)
+ if (!enabled) {
return p_message;
+ }
ERR_FAIL_COND_V_MSG(locale.length() < 2, p_message, "Could not translate message as configured locale '" + locale + "' is invalid.");
@@ -1144,7 +1119,6 @@ StringName TranslationServer::translate(const StringName &p_message) const {
TranslationServer *TranslationServer::singleton = nullptr;
bool TranslationServer::_load_translations(const String &p_from) {
-
if (ProjectSettings::get_singleton()->has_setting(p_from)) {
Vector<String> translations = ProjectSettings::get_singleton()->get(p_from);
@@ -1154,10 +1128,10 @@ bool TranslationServer::_load_translations(const String &p_from) {
const String *r = translations.ptr();
for (int i = 0; i < tcount; i++) {
-
Ref<Translation> tr = ResourceLoader::load(r[i]);
- if (tr.is_valid())
+ if (tr.is_valid()) {
add_translation(tr);
+ }
}
}
return true;
@@ -1167,21 +1141,22 @@ bool TranslationServer::_load_translations(const String &p_from) {
}
void TranslationServer::setup() {
-
String test = GLOBAL_DEF("locale/test", "");
test = test.strip_edges();
- if (test != "")
+ if (test != "") {
set_locale(test);
- else
+ } else {
set_locale(OS::get_singleton()->get_locale());
+ }
fallback = GLOBAL_DEF("locale/fallback", "en");
#ifdef TOOLS_ENABLED
{
String options = "";
int idx = 0;
while (locale_list[idx]) {
- if (idx > 0)
+ if (idx > 0) {
options += ",";
+ }
options += locale_list[idx];
idx++;
}
@@ -1219,7 +1194,6 @@ StringName TranslationServer::doc_translate(const StringName &p_message) const {
}
void TranslationServer::_bind_methods() {
-
ClassDB::bind_method(D_METHOD("set_locale", "locale"), &TranslationServer::set_locale);
ClassDB::bind_method(D_METHOD("get_locale"), &TranslationServer::get_locale);
@@ -1236,7 +1210,6 @@ void TranslationServer::_bind_methods() {
}
void TranslationServer::load_translations() {
-
String locale = get_locale();
_load_translations("locale/translations"); //all
_load_translations("locale/translations_" + locale.substr(0, 2));
@@ -1246,13 +1219,10 @@ void TranslationServer::load_translations() {
}
}
-TranslationServer::TranslationServer() :
- locale("en"),
- enabled(true) {
+TranslationServer::TranslationServer() {
singleton = this;
for (int i = 0; locale_list[i]; ++i) {
-
locale_name_map.insert(locale_list[i], String::utf8(locale_names[i]));
}
}