diff options
Diffstat (limited to 'core/string')
-rw-r--r-- | core/string/translation.cpp | 8 | ||||
-rw-r--r-- | core/string/translation.h | 2 | ||||
-rw-r--r-- | core/string/translation_po.cpp | 90 | ||||
-rw-r--r-- | core/string/translation_po.h | 14 |
4 files changed, 81 insertions, 33 deletions
diff --git a/core/string/translation.cpp b/core/string/translation.cpp index 344fe42fa0..432016284a 100644 --- a/core/string/translation.cpp +++ b/core/string/translation.cpp @@ -752,10 +752,10 @@ StringName TranslationServer::tool_translate(const StringName &p_message, const if (tool_translation.is_valid()) { StringName r = tool_translation->get_message(p_message, p_context); if (r) { - return editor_pseudolocalization ? tool_pseudolocalize(r) : r; + return r; } } - return editor_pseudolocalization ? tool_pseudolocalize(p_message) : p_message; + return p_message; } StringName TranslationServer::tool_translate_plural(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const { @@ -856,10 +856,6 @@ void TranslationServer::set_pseudolocalization_enabled(bool p_enabled) { } } -void TranslationServer::set_editor_pseudolocalization(bool p_enabled) { - editor_pseudolocalization = p_enabled; -} - void TranslationServer::reload_pseudolocalization() { pseudolocalization_accents_enabled = GLOBAL_GET("internationalization/pseudolocalization/replace_with_accents"); pseudolocalization_double_vowels_enabled = GLOBAL_GET("internationalization/pseudolocalization/double_vowels"); diff --git a/core/string/translation.h b/core/string/translation.h index 78d6721347..0a7eacc45f 100644 --- a/core/string/translation.h +++ b/core/string/translation.h @@ -94,7 +94,6 @@ class TranslationServer : public Object { bool pseudolocalization_fake_bidi_enabled = false; bool pseudolocalization_override_enabled = false; bool pseudolocalization_skip_placeholders_enabled = false; - bool editor_pseudolocalization = false; float expansion_ratio = 0.0; String pseudolocalization_prefix; String pseudolocalization_suffix; @@ -170,7 +169,6 @@ public: bool is_pseudolocalization_enabled() const; void set_pseudolocalization_enabled(bool p_enabled); - void set_editor_pseudolocalization(bool p_enabled); void reload_pseudolocalization(); String standardize_locale(const String &p_locale) const; diff --git a/core/string/translation_po.cpp b/core/string/translation_po.cpp index 06fd4717d7..8e275505b0 100644 --- a/core/string/translation_po.cpp +++ b/core/string/translation_po.cpp @@ -140,43 +140,87 @@ int TranslationPO::_get_plural_index(int p_n) const { input_val.clear(); input_val.push_back(p_n); - Variant result; - for (int i = 0; i < equi_tests.size(); i++) { - Error err = expr->parse(equi_tests[i], input_name); - ERR_FAIL_COND_V_MSG(err != OK, 0, "Cannot parse expression. Error: " + expr->get_error_text()); + return _eq_test(equi_tests, 0); +} - result = expr->execute(input_val); - ERR_FAIL_COND_V_MSG(expr->has_execute_failed(), 0, "Cannot evaluate expression."); +int TranslationPO::_eq_test(const Ref<EQNode> &p_node, const Variant &p_result) const { + if (p_node.is_valid()) { + Error err = expr->parse(p_node->regex, input_name); + ERR_FAIL_COND_V_MSG(err != OK, 0, vformat("Cannot parse expression \"%s\". Error: %s", p_node->regex, expr->get_error_text())); - // Last expression. Variant result will either map to a bool or an integer, in both cases returning it will give the correct plural index. - if (i + 1 == equi_tests.size()) { - return result; - } + Variant result = expr->execute(input_val); + ERR_FAIL_COND_V_MSG(expr->has_execute_failed(), 0, vformat("Cannot evaluate expression \"%s\".", p_node->regex)); if (bool(result)) { - return i; + return _eq_test(p_node->left, result); + } else { + return _eq_test(p_node->right, result); } + } else { + return p_result; + } +} + +int TranslationPO::_find_unquoted(const String &p_src, char32_t p_chr) const { + const int len = p_src.length(); + if (len == 0) { + return -1; } - ERR_FAIL_V_MSG(0, "Unexpected. Function should have returned. Please report this bug."); + const char32_t *src = p_src.get_data(); + bool in_quote = false; + for (int i = 0; i < len; i++) { + if (in_quote) { + if (src[i] == ')') { + in_quote = false; + } + } else { + if (src[i] == '(') { + in_quote = true; + } else if (src[i] == p_chr) { + return i; + } + } + } + + return -1; } -void TranslationPO::_cache_plural_tests(const String &p_plural_rule) { +void TranslationPO::_cache_plural_tests(const String &p_plural_rule, Ref<EQNode> &p_node) { // Some examples of p_plural_rule passed in can have the form: // "n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 && n%100<=99 ? 4 : 5" (Arabic) // "n >= 2" (French) // When evaluating the last, especially careful with this one. // "n != 1" (English) - int first_ques_mark = p_plural_rule.find("?"); + + String rule = p_plural_rule; + if (rule.begins_with("(") && rule.ends_with(")")) { + int bcount = 0; + for (int i = 1; i < rule.length() - 1 && bcount >= 0; i++) { + if (rule[i] == '(') { + bcount++; + } else if (rule[i] == ')') { + bcount--; + } + } + if (bcount == 0) { + rule = rule.substr(1, rule.length() - 2); + } + } + + int first_ques_mark = _find_unquoted(rule, '?'); + int first_colon = _find_unquoted(rule, ':'); + if (first_ques_mark == -1) { - equi_tests.push_back(p_plural_rule.strip_edges()); + p_node->regex = rule.strip_edges(); return; } - String equi_test = p_plural_rule.substr(0, first_ques_mark).strip_edges(); - equi_tests.push_back(equi_test); + p_node->regex = rule.substr(0, first_ques_mark).strip_edges(); - String after_colon = p_plural_rule.substr(p_plural_rule.find(":") + 1, p_plural_rule.length()); - _cache_plural_tests(after_colon); + p_node->left.instantiate(); + _cache_plural_tests(rule.substr(first_ques_mark + 1, first_colon - first_ques_mark - 1).strip_edges(), p_node->left); + p_node->right.instantiate(); + _cache_plural_tests(rule.substr(first_colon + 1).strip_edges(), p_node->right); } void TranslationPO::set_plural_rule(const String &p_plural_rule) { @@ -188,12 +232,12 @@ void TranslationPO::set_plural_rule(const String &p_plural_rule) { int expression_start = p_plural_rule.find("=", first_semi_col) + 1; int second_semi_col = p_plural_rule.rfind(";"); - plural_rule = p_plural_rule.substr(expression_start, second_semi_col - expression_start); + plural_rule = p_plural_rule.substr(expression_start, second_semi_col - expression_start).strip_edges(); // Setup the cache to make evaluating plural rule faster later on. - plural_rule = plural_rule.replacen("(", ""); - plural_rule = plural_rule.replacen(")", ""); - _cache_plural_tests(plural_rule); + equi_tests.instantiate(); + _cache_plural_tests(plural_rule, equi_tests); + expr.instantiate(); input_name.push_back("n"); } diff --git a/core/string/translation_po.h b/core/string/translation_po.h index 73f9b33a87..ba820c6ee4 100644 --- a/core/string/translation_po.h +++ b/core/string/translation_po.h @@ -50,7 +50,17 @@ class TranslationPO : public Translation { String plural_rule; // Cache temporary variables related to _get_plural_index() to make it faster - Vector<String> equi_tests; + class EQNode : public RefCounted { + public: + String regex; + Ref<EQNode> left; + Ref<EQNode> right; + }; + Ref<EQNode> equi_tests; + + int _find_unquoted(const String &p_src, char32_t p_chr) const; + int _eq_test(const Ref<EQNode> &p_node, const Variant &p_result) const; + Vector<String> input_name; mutable Ref<Expression> expr; mutable Array input_val; @@ -59,7 +69,7 @@ class TranslationPO : public Translation { mutable int last_plural_n = -1; // Set it to an impossible value at the beginning. mutable int last_plural_mapped_index = 0; - void _cache_plural_tests(const String &p_plural_rule); + void _cache_plural_tests(const String &p_plural_rule, Ref<EQNode> &p_node); int _get_plural_index(int p_n) const; Vector<String> _get_message_list() const override; |