Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ternary operators from converted .po plural condition #9882

Merged
merged 2 commits into from Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
84 changes: 81 additions & 3 deletions src/Console/PoToPhp.php
Expand Up @@ -103,14 +103,14 @@ protected function doExecute()

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]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this win the price of the longest function name you ever created? 😬

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly, although it's an open contest.

// 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";
}

Expand Down Expand Up @@ -212,4 +212,82 @@ private function escapeDollar($match)
{
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);
}
}
2 changes: 1 addition & 1 deletion view/lang/bg/strings.php
Expand Up @@ -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"] = "Добавяне на нов контакт";
Expand Down
2 changes: 1 addition & 1 deletion view/lang/ca/strings.php
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion view/lang/cs/strings.php
Expand Up @@ -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.";
Expand Down
2 changes: 1 addition & 1 deletion view/lang/de/strings.php
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion view/lang/en-gb/strings.php
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion view/lang/en-us/strings.php
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion view/lang/eo/strings.php
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion view/lang/es/strings.php
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion view/lang/et/strings.php
Expand Up @@ -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."] = [
Expand Down
2 changes: 1 addition & 1 deletion view/lang/fi-fi/strings.php
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion view/lang/fr/strings.php
Expand Up @@ -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."] = [
Expand Down
2 changes: 1 addition & 1 deletion view/lang/hu/strings.php
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion view/lang/is/strings.php
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion view/lang/it/strings.php
Expand Up @@ -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."] = [
Expand Down
2 changes: 1 addition & 1 deletion view/lang/ja/strings.php
Expand Up @@ -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."] = [
Expand Down
2 changes: 1 addition & 1 deletion view/lang/nb-no/strings.php
Expand Up @@ -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."] = [
Expand Down
2 changes: 1 addition & 1 deletion view/lang/nl/strings.php
Expand Up @@ -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."] = [
Expand Down
2 changes: 1 addition & 1 deletion view/lang/pl/strings.php
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion view/lang/pt-br/strings.php
Expand Up @@ -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."] = [
Expand Down
2 changes: 1 addition & 1 deletion view/lang/ro/strings.php
Expand Up @@ -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."] = [
Expand Down
2 changes: 1 addition & 1 deletion view/lang/ru/strings.php
Expand Up @@ -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."] = [
Expand Down
2 changes: 1 addition & 1 deletion view/lang/sv/strings.php
Expand Up @@ -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."] = [
Expand Down
2 changes: 1 addition & 1 deletion view/lang/zh-cn/strings.php
Expand Up @@ -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"] = "默认";
Expand Down