Merge pull request #9882 from MrPetovan/bug/po2php-plural-conversion

Remove ternary operators from converted .po plural condition
This commit is contained in:
Michael Vogel 2021-01-31 11:43:25 +01:00 committed by GitHub
commit 56a77f5275
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 104 additions and 26 deletions

View file

@ -103,14 +103,14 @@ HELP;
if (substr($l, 0, 15) == '"Plural-Forms: ') { if (substr($l, 0, 15) == '"Plural-Forms: ') {
$match = []; $match = [];
preg_match("|nplurals=([0-9]*); *plural=(.*)[;\\\\]|", $l, $match); preg_match("|nplurals=([0-9]*); *plural=(.*?)[;\\\\]|", $l, $match);
$cond = str_replace('n', '$n', $match[2]); $return = $this->convertCPluralConditionToPhpReturnStatement($match[2]);
// define plural select function if not already defined // define plural select function if not already defined
$fnname = 'string_plural_select_' . $lang; $fnname = 'string_plural_select_' . $lang;
$out .= 'if(! function_exists("' . $fnname . '")) {' . "\n"; $out .= 'if(! function_exists("' . $fnname . '")) {' . "\n";
$out .= 'function ' . $fnname . '($n){' . "\n"; $out .= 'function ' . $fnname . '($n){' . "\n";
$out .= ' $n = intval($n);' . "\n"; $out .= ' $n = intval($n);' . "\n";
$out .= ' return ' . $cond . ';' . "\n"; $out .= ' ' . $return . "\n";
$out .= '}}' . "\n"; $out .= '}}' . "\n";
} }
@ -212,4 +212,82 @@ HELP;
{ {
return str_replace('$', '\$', $match[0]); 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);
}
} }

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_bg")) { if(! function_exists("string_plural_select_bg")) {
function string_plural_select_bg($n){ function string_plural_select_bg($n){
$n = intval($n); $n = intval($n);
return ($n != 1);; return intval($n != 1);
}} }}
; ;
$a->strings["Add New Contact"] = "Добавяне на нов контакт"; $a->strings["Add New Contact"] = "Добавяне на нов контакт";

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_ca")) { if(! function_exists("string_plural_select_ca")) {
function string_plural_select_ca($n){ function string_plural_select_ca($n){
$n = intval($n); $n = intval($n);
return ($n != 1);; return intval($n != 1);
}} }}
; ;
$a->strings["Add New Contact"] = "Afegir Nou Contacte"; $a->strings["Add New Contact"] = "Afegir Nou Contacte";

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_cs")) { if(! function_exists("string_plural_select_cs")) {
function string_plural_select_cs($n){ function string_plural_select_cs($n){
$n = intval($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."; $a->strings["Item not found."] = "Položka nenalezena.";

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_de")) { if(! function_exists("string_plural_select_de")) {
function string_plural_select_de($n){ function string_plural_select_de($n){
$n = intval($n); $n = intval($n);
return ($n != 1);; return intval($n != 1);
}} }}
; ;
$a->strings["default"] = "Standard"; $a->strings["default"] = "Standard";

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_en_gb")) { if(! function_exists("string_plural_select_en_gb")) {
function string_plural_select_en_gb($n){ function string_plural_select_en_gb($n){
$n = intval($n); $n = intval($n);
return ($n != 1);; return intval($n != 1);
}} }}
; ;
$a->strings["default"] = "default"; $a->strings["default"] = "default";

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_en_us")) { if(! function_exists("string_plural_select_en_us")) {
function string_plural_select_en_us($n){ function string_plural_select_en_us($n){
$n = intval($n); $n = intval($n);
return ($n != 1);; return intval($n != 1);
}} }}
; ;
$a->strings["default"] = "default"; $a->strings["default"] = "default";

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_eo")) { if(! function_exists("string_plural_select_eo")) {
function string_plural_select_eo($n){ function string_plural_select_eo($n){
$n = intval($n); $n = intval($n);
return ($n != 1);; return intval($n != 1);
}} }}
; ;
$a->strings["Add New Contact"] = "Aldonu Novan Kontakton"; $a->strings["Add New Contact"] = "Aldonu Novan Kontakton";

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_es")) { if(! function_exists("string_plural_select_es")) {
function string_plural_select_es($n){ function string_plural_select_es($n){
$n = intval($n); $n = intval($n);
return ($n != 1);; return intval($n != 1);
}} }}
; ;
$a->strings["default"] = "predeterminado"; $a->strings["default"] = "predeterminado";

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_et")) { if(! function_exists("string_plural_select_et")) {
function string_plural_select_et($n){ function string_plural_select_et($n){
$n = intval($n); $n = intval($n);
return ($n != 1);; return intval($n != 1);
}} }}
; ;
$a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_fi_fi")) { if(! function_exists("string_plural_select_fi_fi")) {
function string_plural_select_fi_fi($n){ function string_plural_select_fi_fi($n){
$n = intval($n); $n = intval($n);
return ($n != 1);; return intval($n != 1);
}} }}
; ;
$a->strings["Friendica Notification"] = "Friendica-huomautus"; $a->strings["Friendica Notification"] = "Friendica-huomautus";

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_fr")) { if(! function_exists("string_plural_select_fr")) {
function string_plural_select_fr($n){ function string_plural_select_fr($n){
$n = intval($n); $n = intval($n);
return ($n > 1);; return intval($n > 1);
}} }}
; ;
$a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_hu")) { if(! function_exists("string_plural_select_hu")) {
function string_plural_select_hu($n){ function string_plural_select_hu($n){
$n = intval($n); $n = intval($n);
return ($n != 1);; return intval($n != 1);
}} }}
; ;
$a->strings["default"] = "alapértelmezett"; $a->strings["default"] = "alapértelmezett";

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_is")) { if(! function_exists("string_plural_select_is")) {
function string_plural_select_is($n){ function string_plural_select_is($n){
$n = intval($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"; $a->strings["Friendica Notification"] = "Friendica tilkynning";

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_it")) { if(! function_exists("string_plural_select_it")) {
function string_plural_select_it($n){ function string_plural_select_it($n){
$n = intval($n); $n = intval($n);
return ($n != 1);; return intval($n != 1);
}} }}
; ;
$a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_ja")) { if(! function_exists("string_plural_select_ja")) {
function string_plural_select_ja($n){ function string_plural_select_ja($n){
$n = intval($n); $n = intval($n);
return 0;; return intval(0);
}} }}
; ;
$a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_nb_no")) { if(! function_exists("string_plural_select_nb_no")) {
function string_plural_select_nb_no($n){ function string_plural_select_nb_no($n){
$n = intval($n); $n = intval($n);
return ($n != 1);; return intval($n != 1);
}} }}
; ;
$a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_nl")) { if(! function_exists("string_plural_select_nl")) {
function string_plural_select_nl($n){ function string_plural_select_nl($n){
$n = intval($n); $n = intval($n);
return ($n != 1);; return intval($n != 1);
}} }}
; ;
$a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_pl")) { if(! function_exists("string_plural_select_pl")) {
function string_plural_select_pl($n){ function string_plural_select_pl($n){
$n = intval($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"; $a->strings["default"] = "standardowe";

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_pt_br")) { if(! function_exists("string_plural_select_pt_br")) {
function string_plural_select_pt_br($n){ function string_plural_select_pt_br($n){
$n = intval($n); $n = intval($n);
return ($n > 1);; return intval($n > 1);
}} }}
; ;
$a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_ro")) { if(! function_exists("string_plural_select_ro")) {
function string_plural_select_ro($n){ function string_plural_select_ro($n){
$n = intval($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."] = [ $a->strings["%d contact edited."] = [

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_ru")) { if(! function_exists("string_plural_select_ru")) {
function string_plural_select_ru($n){ function string_plural_select_ru($n){
$n = intval($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."] = [ $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_sv")) { if(! function_exists("string_plural_select_sv")) {
function string_plural_select_sv($n){ function string_plural_select_sv($n){
$n = intval($n); $n = intval($n);
return ($n != 1);; return intval($n != 1);
}} }}
; ;
$a->strings["Daily posting limit of %d post reached. The post was rejected."] = [ $a->strings["Daily posting limit of %d post reached. The post was rejected."] = [

View file

@ -3,7 +3,7 @@
if(! function_exists("string_plural_select_zh_cn")) { if(! function_exists("string_plural_select_zh_cn")) {
function string_plural_select_zh_cn($n){ function string_plural_select_zh_cn($n){
$n = intval($n); $n = intval($n);
return 0;; return intval(0);
}} }}
; ;
$a->strings["default"] = "默认"; $a->strings["default"] = "默认";