Browse Source

a parser that is able to deal with the right number of args

tags/2.0.0-beta
Carsten Brandt 11 years ago
parent
commit
b8099cbf28
  1. 43
      framework/yii/i18n/MessageFormatter.php

43
framework/yii/i18n/MessageFormatter.php

@ -64,16 +64,41 @@ class MessageFormatter extends \MessageFormatter
private static function replaceNamedArguments($pattern, $args)
{
$map = array_flip(array_keys($args));
return preg_replace_callback('~({\s*)([\d\w]+)(\s*[,}])~u', function ($input) use ($map) {
$name = $input[2];
if (isset($map[$name])) {
return $input[1] . $map[$name] . $input[3];
}
else {
//return $input[1] . $name . $input[3];
return "'" . $input[1] . $name . $input[3] . "'";
// parsing http://icu-project.org/apiref/icu4c/classMessageFormat.html#details
$parts = explode('{', $pattern);
$c = count($parts);
$pattern = $parts[0];
$d = 0;
$stack = array();
for($i = 1; $i < $c; $i++) {
if (preg_match('~^\A(\s*)([\d\w]+)(\s*)([},])(\s*)(.*)\z$~u', $parts[$i], $matches)) {
$d++;
// replace normal arg if it was set
if (isset($map[$matches[2]])) {
$q = '';
$pattern .= '{' . $matches[1] . $map[$matches[2]] . $matches[3];
} else {
// quote unused args
$q = '';//($matches[4] == '}' && isset($stack[$d]) && !($stack[$d] == 'plural' || $stack[$d] == 'select')) ? "'" : "";
$pattern .= "$q{" . $matches[1] . $matches[2] . $matches[3];
}
$pattern .= ($term = $matches[4] . $q . $matches[5] . $matches[6]);
// check type current level
$stack[$d] = ($matches[4] == ',') ? substr($matches[6], 0, 6) : 'none';
// if it's plural or select, the next bracket is NOT begin of a message then!
if ($stack[$d] == 'plural' || $stack[$d] == 'select') {
$i++;
$d -= substr_count($term, '}');
} else {
$d -= substr_count($term, '}');
continue;
}
}
}, $pattern);
$pattern .= '{' . $parts[$i];
$d += 1 - substr_count($parts[$i], '}');
}
return $pattern;
}
/**

Loading…
Cancel
Save