From 3bb10b82a13691c9b4683ace53bab3a0b0347b04 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 29 Jan 2021 20:01:14 -0500 Subject: [PATCH 1/2] Remove ternary operators from converted .po plural condition - Thanks @sunchaserinfo for most of the work --- src/Console/PoToPhp.php | 84 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 3 deletions(-) diff --git a/src/Console/PoToPhp.php b/src/Console/PoToPhp.php index 85dc28cf4..ef2e2f738 100644 --- a/src/Console/PoToPhp.php +++ b/src/Console/PoToPhp.php @@ -103,14 +103,14 @@ HELP; if (substr($l, 0, 15) == '"Plural-Forms: ') { $match = []; - preg_match("|nplurals=([0-9]*); *plural=(.*)[;\\\\]|", $l, $match); - $cond = str_replace('n', '$n', $match[2]); + preg_match("|nplurals=([0-9]*); *plural=(.*?)[;\\\\]|", $l, $match); + $return = $this->convertCPluralConditionToPhpReturnStatement($match[2]); // define plural select function if not already defined $fnname = 'string_plural_select_' . $lang; $out .= 'if(! function_exists("' . $fnname . '")) {' . "\n"; $out .= 'function ' . $fnname . '($n){' . "\n"; $out .= ' $n = intval($n);' . "\n"; - $out .= ' return ' . $cond . ';' . "\n"; + $out .= ' ' . $return . "\n"; $out .= '}}' . "\n"; } @@ -212,4 +212,82 @@ HELP; { return str_replace('$', '\$', $match[0]); } + + /** + * Converts C-style plural condition in .po files to a PHP-style plural return statement + * + * Adapted from https://github.com/friendica/friendica/issues/9747#issuecomment-769604485 + * Many thanks to Christian Archer (https://github.com/sunchaserinfo) + * + * @param string $cond + * @return string + */ + private function convertCPluralConditionToPhpReturnStatement(string $cond) + { + $cond = str_replace('n', '$n', $cond); + + /** + * Parses the condition into an array if there's at least a ternary operator, to a string otherwise + * + * Warning: Black recursive magic + * + * @param string $string + * @param array|string $node + */ + function parse(string $string, &$node = []) + { + // Removes extra outward parentheses + if (strpos($string, '(') === 0 && strrpos($string, ')') === strlen($string) - 1) { + $string = substr($string, 1, -1); + } + + $q = strpos($string, '?'); + $s = strpos($string, ':'); + + if ($q === false && $s === false) { + $node = $string; + return; + } + + if ($q === false || $s < $q) { + list($then, $else) = explode(':', $string, 2); + $node['then'] = $then; + $parsedElse = []; + parse($else, $parsedElse); + $node['else'] = $parsedElse; + } else { + list($if, $thenelse) = explode('?', $string, 2); + $node['if'] = $if; + parse($thenelse, $node); + } + } + + /** + * Renders the parsed condition tree into a return statement + * + * Warning: Black recursive magic + * + * @param $tree + * @return string + */ + function render($tree) + { + if (is_array($tree)) { + $if = trim($tree['if']); + $then = trim($tree['then']); + $else = render($tree['else']); + + return "if ({$if}) { return {$then}; } else {$else}"; + } + + $tree = trim($tree); + + return " { return {$tree}; }"; + } + + $tree = []; + parse($cond, $tree); + + return is_string($tree) ? "return intval({$tree});" : render($tree); + } } From bcd3a274f1cc9af7fa16337577096199933e59f4 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 29 Jan 2021 20:02:54 -0500 Subject: [PATCH 2/2] Update plural condition in strings.php after converter update --- view/lang/bg/strings.php | 2 +- view/lang/ca/strings.php | 2 +- view/lang/cs/strings.php | 2 +- view/lang/de/strings.php | 2 +- view/lang/en-gb/strings.php | 2 +- view/lang/en-us/strings.php | 2 +- view/lang/eo/strings.php | 2 +- view/lang/es/strings.php | 2 +- view/lang/et/strings.php | 2 +- view/lang/fi-fi/strings.php | 2 +- view/lang/fr/strings.php | 2 +- view/lang/hu/strings.php | 2 +- view/lang/is/strings.php | 2 +- view/lang/it/strings.php | 2 +- view/lang/ja/strings.php | 2 +- view/lang/nb-no/strings.php | 2 +- view/lang/nl/strings.php | 2 +- view/lang/pl/strings.php | 2 +- view/lang/pt-br/strings.php | 2 +- view/lang/ro/strings.php | 2 +- view/lang/ru/strings.php | 2 +- view/lang/sv/strings.php | 2 +- view/lang/zh-cn/strings.php | 2 +- 23 files changed, 23 insertions(+), 23 deletions(-) diff --git a/view/lang/bg/strings.php b/view/lang/bg/strings.php index b70ac3b1a..9335b0677 100644 --- a/view/lang/bg/strings.php +++ b/view/lang/bg/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_bg")) { function string_plural_select_bg($n){ $n = intval($n); - return ($n != 1);; + return intval($n != 1); }} ; $a->strings["Add New Contact"] = "Добавяне на нов контакт"; diff --git a/view/lang/ca/strings.php b/view/lang/ca/strings.php index dbf3919c6..b17497210 100644 --- a/view/lang/ca/strings.php +++ b/view/lang/ca/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_ca")) { function string_plural_select_ca($n){ $n = intval($n); - return ($n != 1);; + return intval($n != 1); }} ; $a->strings["Add New Contact"] = "Afegir Nou Contacte"; diff --git a/view/lang/cs/strings.php b/view/lang/cs/strings.php index add1b3016..ba0ac4de3 100644 --- a/view/lang/cs/strings.php +++ b/view/lang/cs/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_cs")) { function string_plural_select_cs($n){ $n = intval($n); - return ($n == 1 && $n % 1 == 0) ? 0 : ($n >= 2 && $n <= 4 && $n % 1 == 0) ? 1: ($n % 1 != 0 ) ? 2 : 3;; + if (($n == 1 && $n % 1 == 0)) { return 0; } else if (($n >= 2 && $n <= 4 && $n % 1 == 0)) { return 1; } else if (($n % 1 != 0 )) { return 2; } else { return 3; } }} ; $a->strings["Item not found."] = "Položka nenalezena."; diff --git a/view/lang/de/strings.php b/view/lang/de/strings.php index a0e0cc345..46ab91e4d 100644 --- a/view/lang/de/strings.php +++ b/view/lang/de/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_de")) { function string_plural_select_de($n){ $n = intval($n); - return ($n != 1);; + return intval($n != 1); }} ; $a->strings["default"] = "Standard"; diff --git a/view/lang/en-gb/strings.php b/view/lang/en-gb/strings.php index 9e27e4f1d..1c85e5e84 100644 --- a/view/lang/en-gb/strings.php +++ b/view/lang/en-gb/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_en_gb")) { function string_plural_select_en_gb($n){ $n = intval($n); - return ($n != 1);; + return intval($n != 1); }} ; $a->strings["default"] = "default"; diff --git a/view/lang/en-us/strings.php b/view/lang/en-us/strings.php index 3bca900e2..fab25f75e 100644 --- a/view/lang/en-us/strings.php +++ b/view/lang/en-us/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_en_us")) { function string_plural_select_en_us($n){ $n = intval($n); - return ($n != 1);; + return intval($n != 1); }} ; $a->strings["default"] = "default"; diff --git a/view/lang/eo/strings.php b/view/lang/eo/strings.php index 3704fd32f..a81b4428e 100644 --- a/view/lang/eo/strings.php +++ b/view/lang/eo/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_eo")) { function string_plural_select_eo($n){ $n = intval($n); - return ($n != 1);; + return intval($n != 1); }} ; $a->strings["Add New Contact"] = "Aldonu Novan Kontakton"; diff --git a/view/lang/es/strings.php b/view/lang/es/strings.php index 6cb991e99..abb9993d1 100644 --- a/view/lang/es/strings.php +++ b/view/lang/es/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_es")) { function string_plural_select_es($n){ $n = intval($n); - return ($n != 1);; + return intval($n != 1); }} ; $a->strings["default"] = "predeterminado"; diff --git a/view/lang/et/strings.php b/view/lang/et/strings.php index 927528fe2..94cac7876 100644 --- a/view/lang/et/strings.php +++ b/view/lang/et/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_et")) { function string_plural_select_et($n){ $n = intval($n); - return ($n != 1);; + return intval($n != 1); }} ; $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ diff --git a/view/lang/fi-fi/strings.php b/view/lang/fi-fi/strings.php index a530cd8cf..40e1d39f4 100644 --- a/view/lang/fi-fi/strings.php +++ b/view/lang/fi-fi/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_fi_fi")) { function string_plural_select_fi_fi($n){ $n = intval($n); - return ($n != 1);; + return intval($n != 1); }} ; $a->strings["Friendica Notification"] = "Friendica-huomautus"; diff --git a/view/lang/fr/strings.php b/view/lang/fr/strings.php index 70c6542bd..517cd8b21 100644 --- a/view/lang/fr/strings.php +++ b/view/lang/fr/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_fr")) { function string_plural_select_fr($n){ $n = intval($n); - return ($n > 1);; + return intval($n > 1); }} ; $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ diff --git a/view/lang/hu/strings.php b/view/lang/hu/strings.php index c7387379d..1f7640eba 100644 --- a/view/lang/hu/strings.php +++ b/view/lang/hu/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_hu")) { function string_plural_select_hu($n){ $n = intval($n); - return ($n != 1);; + return intval($n != 1); }} ; $a->strings["default"] = "alapértelmezett"; diff --git a/view/lang/is/strings.php b/view/lang/is/strings.php index 44adc7c1f..9356efb4d 100644 --- a/view/lang/is/strings.php +++ b/view/lang/is/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_is")) { function string_plural_select_is($n){ $n = intval($n); - return ($n % 10 != 1 || $n % 100 == 11);; + return intval($n % 10 != 1 || $n % 100 == 11); }} ; $a->strings["Friendica Notification"] = "Friendica tilkynning"; diff --git a/view/lang/it/strings.php b/view/lang/it/strings.php index ad472eaa8..2b54469e9 100644 --- a/view/lang/it/strings.php +++ b/view/lang/it/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_it")) { function string_plural_select_it($n){ $n = intval($n); - return ($n != 1);; + return intval($n != 1); }} ; $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ diff --git a/view/lang/ja/strings.php b/view/lang/ja/strings.php index 89cd27cbb..9f5c0a844 100644 --- a/view/lang/ja/strings.php +++ b/view/lang/ja/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_ja")) { function string_plural_select_ja($n){ $n = intval($n); - return 0;; + return intval(0); }} ; $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ diff --git a/view/lang/nb-no/strings.php b/view/lang/nb-no/strings.php index 03af58cc2..a014b857c 100644 --- a/view/lang/nb-no/strings.php +++ b/view/lang/nb-no/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_nb_no")) { function string_plural_select_nb_no($n){ $n = intval($n); - return ($n != 1);; + return intval($n != 1); }} ; $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ diff --git a/view/lang/nl/strings.php b/view/lang/nl/strings.php index 2d206ec87..ee976c30f 100644 --- a/view/lang/nl/strings.php +++ b/view/lang/nl/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_nl")) { function string_plural_select_nl($n){ $n = intval($n); - return ($n != 1);; + return intval($n != 1); }} ; $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ diff --git a/view/lang/pl/strings.php b/view/lang/pl/strings.php index 84aa91b49..db4ca0e80 100644 --- a/view/lang/pl/strings.php +++ b/view/lang/pl/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_pl")) { function string_plural_select_pl($n){ $n = intval($n); - return ($n==1 ? 0 : ($n%10>=2 && $n%10<=4) && ($n%100<12 || $n%100>14) ? 1 : $n!=1 && ($n%10>=0 && $n%10<=1) || ($n%10>=5 && $n%10<=9) || ($n%100>=12 && $n%100<=14) ? 2 : 3);; + if ($n==1) { return 0; } else if (($n%10>=2 && $n%10<=4) && ($n%100<12 || $n%100>14)) { return 1; } else if ($n!=1 && ($n%10>=0 && $n%10<=1) || ($n%10>=5 && $n%10<=9) || ($n%100>=12 && $n%100<=14)) { return 2; } else { return 3; } }} ; $a->strings["default"] = "standardowe"; diff --git a/view/lang/pt-br/strings.php b/view/lang/pt-br/strings.php index b59835339..458a14e85 100644 --- a/view/lang/pt-br/strings.php +++ b/view/lang/pt-br/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_pt_br")) { function string_plural_select_pt_br($n){ $n = intval($n); - return ($n > 1);; + return intval($n > 1); }} ; $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ diff --git a/view/lang/ro/strings.php b/view/lang/ro/strings.php index 50e963b08..264c5a2bb 100644 --- a/view/lang/ro/strings.php +++ b/view/lang/ro/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_ro")) { function string_plural_select_ro($n){ $n = intval($n); - return ($n==1?0:((($n%100>19)||(($n%100==0)&&($n!=0)))?2:1));; + if ($n==1) { return 0; } else if ((($n%100>19)||(($n%100==0)&&($n!=0)))) { return 2; } else { return 1; } }} ; $a->strings["%d contact edited."] = [ diff --git a/view/lang/ru/strings.php b/view/lang/ru/strings.php index f61a2f5ce..4eea1a9d4 100644 --- a/view/lang/ru/strings.php +++ b/view/lang/ru/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_ru")) { function string_plural_select_ru($n){ $n = intval($n); - return ($n%10==1 && $n%100!=11 ? 0 : $n%10>=2 && $n%10<=4 && ($n%100<12 || $n%100>14) ? 1 : $n%10==0 || ($n%10>=5 && $n%10<=9) || ($n%100>=11 && $n%100<=14)? 2 : 3);; + if ($n%10==1 && $n%100!=11) { return 0; } else if ($n%10>=2 && $n%10<=4 && ($n%100<12 || $n%100>14)) { return 1; } else if ($n%10==0 || ($n%10>=5 && $n%10<=9) || ($n%100>=11 && $n%100<=14)) { return 2; } else { return 3; } }} ; $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ diff --git a/view/lang/sv/strings.php b/view/lang/sv/strings.php index 292fc5049..9d8b8d555 100644 --- a/view/lang/sv/strings.php +++ b/view/lang/sv/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_sv")) { function string_plural_select_sv($n){ $n = intval($n); - return ($n != 1);; + return intval($n != 1); }} ; $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ diff --git a/view/lang/zh-cn/strings.php b/view/lang/zh-cn/strings.php index d8b2c53e0..580cf33d0 100644 --- a/view/lang/zh-cn/strings.php +++ b/view/lang/zh-cn/strings.php @@ -3,7 +3,7 @@ if(! function_exists("string_plural_select_zh_cn")) { function string_plural_select_zh_cn($n){ $n = intval($n); - return 0;; + return intval(0); }} ; $a->strings["default"] = "默认";