[I18N] Fix implementation of I18nHelper::formatICU

This commit is contained in:
Hugo Sales 2020-06-23 12:05:38 +00:00 committed by Hugo Sales
parent 491e82f94e
commit 753f852941
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
1 changed files with 17 additions and 17 deletions

View File

@ -217,38 +217,38 @@ abstract class I18nHelper
public static function formatICU(array $messages, array $params): string public static function formatICU(array $messages, array $params): string
{ {
$msg = ''; $res = '';
foreach ($params as $var => $val) { foreach (array_slice($params, 0, 1, true) as $var => $type) {
if (is_int($val)) { if (is_int($type)) {
$pref = '='; $pref = '=';
$op = 'plural'; $op = 'plural';
} elseif (is_string($var)) { } elseif (is_string($type)) {
$pref = ''; $pref = '';
$op = 'select'; $op = 'select';
} else { } else {
throw new Exception('Invalid key type. (int|string) only'); throw new Exception('Invalid variable type. (int|string) only');
} }
$res = "{{$var}, {$op}, "; $res = "{$var}, {$op}, ";
$i = 1; $i = 0;
$cnt = count($messages); $cnt = count($messages) - 1;
foreach ($messages as $val => $m) { foreach ($messages as $val => $m) {
if ($i !== $cnt) {
$res .= "{$pref}{$val}";
} else {
$res .= 'other';
}
if (is_array($m)) { if (is_array($m)) {
array_shift($params); $res .= ' {' . self::formatICU($m, array_slice($params, 1, null, true)) . '} ';
$res .= "{$pref}{$val} {" . self::formatICU($m, $params) . '} ';
} elseif (is_string($m)) { } elseif (is_string($m)) {
if ($i !== $cnt) { $res .= " {{$m}} ";
$res .= "{$pref}{$val} {{$m}} ";
} else {
$res .= "other {{$m}}}";
}
} else { } else {
throw new Exception('Invalid message array'); throw new Exception('Invalid message array');
} }
++$i; ++$i;
} }
$msg .= $res;
} }
return $msg; return "{{$res}}";
} }
} }