diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/io/translation_loader_po.cpp | 27 | ||||
-rw-r--r-- | core/io/translation_loader_po.h | 2 | ||||
-rw-r--r-- | core/translation.cpp | 17 | ||||
-rw-r--r-- | core/translation.h | 3 | ||||
-rw-r--r-- | core/ustring.cpp | 11 | ||||
-rw-r--r-- | core/ustring.h | 16 |
6 files changed, 52 insertions, 24 deletions
diff --git a/core/io/translation_loader_po.cpp b/core/io/translation_loader_po.cpp index 4051bf2947..5da236d029 100644 --- a/core/io/translation_loader_po.cpp +++ b/core/io/translation_loader_po.cpp @@ -33,7 +33,7 @@ #include "core/os/file_access.h" #include "core/translation.h" -RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const String &p_path) { +RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) { enum Status { @@ -67,7 +67,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S if (status == STATUS_READING_ID) { memdelete(f); - ERR_FAIL_V_MSG(RES(), p_path + ":" + itos(line) + " Unexpected EOF while reading 'msgid' at file: "); + ERR_FAIL_V_MSG(RES(), f->get_path() + ":" + itos(line) + " Unexpected EOF while reading 'msgid' at file: "); } else { break; } @@ -78,7 +78,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S if (status == STATUS_READING_ID) { memdelete(f); - ERR_FAIL_V_MSG(RES(), p_path + ":" + itos(line) + " Unexpected 'msgid', was expecting 'msgstr' while parsing: "); + ERR_FAIL_V_MSG(RES(), f->get_path() + ":" + itos(line) + " Unexpected 'msgid', was expecting 'msgstr' while parsing: "); } if (msg_id != "") { @@ -100,7 +100,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S if (status != STATUS_READING_ID) { memdelete(f); - ERR_FAIL_V_MSG(RES(), p_path + ":" + itos(line) + " Unexpected 'msgstr', was expecting 'msgid' while parsing: "); + ERR_FAIL_V_MSG(RES(), f->get_path() + ":" + itos(line) + " Unexpected 'msgstr', was expecting 'msgid' while parsing: "); } l = l.substr(6, l.length()).strip_edges(); @@ -115,20 +115,29 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S continue; //nothing to read or comment } - ERR_FAIL_COND_V_MSG(!l.begins_with("\"") || status == STATUS_NONE, RES(), p_path + ":" + itos(line) + " Invalid line '" + l + "' while parsing: "); + ERR_FAIL_COND_V_MSG(!l.begins_with("\"") || status == STATUS_NONE, RES(), f->get_path() + ":" + itos(line) + " Invalid line '" + l + "' while parsing: "); l = l.substr(1, l.length()); - //find final quote + // Find final quote, ignoring escaped ones (\"). + // The escape_next logic is necessary to properly parse things like \\" + // where the blackslash is the one being escaped, not the quote. int end_pos = -1; + bool escape_next = false; for (int i = 0; i < l.length(); i++) { + if (l[i] == '\\' && !escape_next) { + escape_next = true; + continue; + } - if (l[i] == '"' && (i == 0 || l[i - 1] != '\\')) { + if (l[i] == '"' && !escape_next) { end_pos = i; break; } + + escape_next = false; } - ERR_FAIL_COND_V_MSG(end_pos == -1, RES(), p_path + ":" + itos(line) + " Expected '\"' at end of message while parsing file: "); + ERR_FAIL_COND_V_MSG(end_pos == -1, RES(), f->get_path() + ":" + itos(line) + ": Expected '\"' at end of message while parsing file."); l = l.substr(0, end_pos); l = l.c_unescape(); @@ -153,7 +162,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error, const S config = msg_str; } - ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + p_path + "."); + ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + f->get_path() + "."); Vector<String> configs = config.split("\n"); for (int i = 0; i < configs.size(); i++) { diff --git a/core/io/translation_loader_po.h b/core/io/translation_loader_po.h index fe3a75e5eb..d280729e94 100644 --- a/core/io/translation_loader_po.h +++ b/core/io/translation_loader_po.h @@ -37,7 +37,7 @@ class TranslationLoaderPO : public ResourceFormatLoader { public: - static RES load_translation(FileAccess *f, Error *r_error, const String &p_path = String()); + static RES load_translation(FileAccess *f, Error *r_error = NULL); virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL, bool p_use_sub_threads = false, float *r_progress = nullptr); virtual void get_recognized_extensions(List<String> *p_extensions) const; virtual bool handles_type(const String &p_type) const; diff --git a/core/translation.cpp b/core/translation.cpp index 7399fa5619..df3661e5d0 100644 --- a/core/translation.cpp +++ b/core/translation.cpp @@ -1176,7 +1176,6 @@ void TranslationServer::setup() { set_locale(OS::get_singleton()->get_locale()); fallback = GLOBAL_DEF("locale/fallback", "en"); #ifdef TOOLS_ENABLED - { String options = ""; int idx = 0; @@ -1189,7 +1188,6 @@ void TranslationServer::setup() { ProjectSettings::get_singleton()->set_custom_property_info("locale/fallback", PropertyInfo(Variant::STRING, "locale/fallback", PROPERTY_HINT_ENUM, options)); } #endif - //load translations } void TranslationServer::set_tool_translation(const Ref<Translation> &p_translation) { @@ -1197,15 +1195,26 @@ void TranslationServer::set_tool_translation(const Ref<Translation> &p_translati } StringName TranslationServer::tool_translate(const StringName &p_message) const { - if (tool_translation.is_valid()) { StringName r = tool_translation->get_message(p_message); - if (r) { return r; } } + return p_message; +} +void TranslationServer::set_doc_translation(const Ref<Translation> &p_translation) { + doc_translation = p_translation; +} + +StringName TranslationServer::doc_translate(const StringName &p_message) const { + if (doc_translation.is_valid()) { + StringName r = doc_translation->get_message(p_message); + if (r) { + return r; + } + } return p_message; } diff --git a/core/translation.h b/core/translation.h index b9e07fa2b1..29a068f450 100644 --- a/core/translation.h +++ b/core/translation.h @@ -73,6 +73,7 @@ class TranslationServer : public Object { Set<Ref<Translation>> translations; Ref<Translation> tool_translation; + Ref<Translation> doc_translation; Map<String, String> locale_name_map; @@ -109,6 +110,8 @@ public: void set_tool_translation(const Ref<Translation> &p_translation); StringName tool_translate(const StringName &p_message) const; + void set_doc_translation(const Ref<Translation> &p_translation); + StringName doc_translate(const StringName &p_message) const; void setup(); diff --git a/core/ustring.cpp b/core/ustring.cpp index da089dce40..8027ae29b5 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -4412,7 +4412,6 @@ String String::unquote() const { #ifdef TOOLS_ENABLED String TTR(const String &p_text) { - if (TranslationServer::get_singleton()) { return TranslationServer::get_singleton()->tool_translate(p_text); } @@ -4420,10 +4419,18 @@ String TTR(const String &p_text) { return p_text; } +String DTR(const String &p_text) { + if (TranslationServer::get_singleton()) { + // Comes straight from the XML, so remove indentation and any trailing whitespace. + const String text = p_text.dedent().strip_edges(); + return TranslationServer::get_singleton()->doc_translate(text); + } + + return p_text; +} #endif String RTR(const String &p_text) { - if (TranslationServer::get_singleton()) { String rtr = TranslationServer::get_singleton()->tool_translate(p_text); if (rtr == String() || rtr == p_text) { diff --git a/core/ustring.h b/core/ustring.h index e70b2bfe27..33320a0a49 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -415,25 +415,25 @@ _FORCE_INLINE_ bool is_str_less(const L *l_ptr, const R *r_ptr) { /* end of namespace */ -//tool translate +// Tool translate (TTR and variants) for the editor UI, +// and doc translate for the class reference (DTR). #ifdef TOOLS_ENABLED - -//gets parsed +// Gets parsed. String TTR(const String &); -//use for C strings +String DTR(const String &); +// Use for C strings. #define TTRC(m_value) (m_value) -//use to avoid parsing (for use later with C strings) +// Use to avoid parsing (for use later with C strings). #define TTRGET(m_value) TTR(m_value) #else - #define TTR(m_value) (String()) +#define DTR(m_value) (String()) #define TTRC(m_value) (m_value) #define TTRGET(m_value) (m_value) - #endif -//tool or regular translate +// Runtime translate for the public node API. String RTR(const String &); bool is_symbol(CharType c); |