From dd883bebd40332c06eed59eadd2383d6f1d0bfc1 Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Mon, 17 Jun 2024 13:03:30 +0300 Subject: [PATCH] Improve PR plural translation rules handling. --- core/string/translation_po.cpp | 90 +++++++++++++++++++++++++--------- core/string/translation_po.h | 14 +++++- 2 files changed, 79 insertions(+), 25 deletions(-) diff --git a/core/string/translation_po.cpp b/core/string/translation_po.cpp index 06fd4717d76..8e275505b05 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 &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; + } + + 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; + } } } - ERR_FAIL_V_MSG(0, "Unexpected. Function should have returned. Please report this bug."); + return -1; } -void TranslationPO::_cache_plural_tests(const String &p_plural_rule) { +void TranslationPO::_cache_plural_tests(const String &p_plural_rule, Ref &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 73f9b33a872..ba820c6ee49 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 equi_tests; + class EQNode : public RefCounted { + public: + String regex; + Ref left; + Ref right; + }; + Ref equi_tests; + + int _find_unquoted(const String &p_src, char32_t p_chr) const; + int _eq_test(const Ref &p_node, const Variant &p_result) const; + Vector input_name; mutable Ref 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 &p_node); int _get_plural_index(int p_n) const; Vector _get_message_list() const override;