53 lines
1.5 KiB
PHP
53 lines
1.5 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Symfony\Component\Translation;
|
||
|
|
||
|
/*
|
||
|
* This file is part of the Symfony framework.
|
||
|
*
|
||
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||
|
*
|
||
|
* This source file is subject to the MIT license that is bundled
|
||
|
* with this source code in the file LICENSE.
|
||
|
*/
|
||
|
|
||
|
/**
|
||
|
* MessageSelector.
|
||
|
*
|
||
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||
|
*/
|
||
|
class MessageSelector
|
||
|
{
|
||
|
public function choose($message, $number, $locale)
|
||
|
{
|
||
|
$parts = explode('|', $message);
|
||
|
$explicitRules = array();
|
||
|
$standardRules = array();
|
||
|
foreach ($parts as $part) {
|
||
|
$part = trim($part);
|
||
|
|
||
|
if (preg_match('/^(?<range>'.Range::getRangeRegexp().')\s+(?<message>.+?)$/x', $part, $matches)) {
|
||
|
$explicitRules[$matches['range']] = $matches['message'];
|
||
|
} elseif (preg_match('/^\w+\: +(.+)$/', $part, $matches)) {
|
||
|
$standardRules[] = $matches[1];
|
||
|
} else {
|
||
|
$standardRules[] = $part;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// try to match an explicit rule, then fallback to the standard ones
|
||
|
foreach ($explicitRules as $range => $m) {
|
||
|
if (Range::test($number, $range)) {
|
||
|
return $m;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$position = PluralizationRules::get($number, $locale);
|
||
|
if (!isset($standardRules[$position])) {
|
||
|
throw new \InvalidArgumentException('Unable to choose a translation.');
|
||
|
}
|
||
|
|
||
|
return $standardRules[$position];
|
||
|
}
|
||
|
}
|