|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Translation; |
|
|
13 |
|
|
|
14 |
/** |
|
|
15 |
* MessageSelector. |
|
|
16 |
* |
|
|
17 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
18 |
* |
|
|
19 |
* @api |
|
|
20 |
*/ |
|
|
21 |
class MessageSelector |
|
|
22 |
{ |
|
|
23 |
/** |
|
|
24 |
* Given a message with different plural translations separated by a |
|
|
25 |
* pipe (|), this method returns the correct portion of the message based |
|
|
26 |
* on the given number, locale and the pluralization rules in the message |
|
|
27 |
* itself. |
|
|
28 |
* |
|
|
29 |
* The message supports two different types of pluralization rules: |
|
|
30 |
* |
|
|
31 |
* interval: {0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples |
|
|
32 |
* indexed: There is one apple|There is %count% apples |
|
|
33 |
* |
|
|
34 |
* The indexed solution can also contain labels (e.g. one: There is one apple). |
|
|
35 |
* This is purely for making the translations more clear - it does not |
|
|
36 |
* affect the functionality. |
|
|
37 |
* |
|
|
38 |
* The two methods can also be mixed: |
|
|
39 |
* {0} There is no apples|one: There is one apple|more: There is %count% apples |
|
|
40 |
* |
|
|
41 |
* @throws InvalidArgumentException |
|
|
42 |
* @param string $message The message being translated |
|
|
43 |
* @param integer $number The number of items represented for the message |
|
|
44 |
* @param string $locale The locale to use for choosing |
|
|
45 |
* @return string |
|
|
46 |
* |
|
|
47 |
* @api |
|
|
48 |
*/ |
|
|
49 |
public function choose($message, $number, $locale) |
|
|
50 |
{ |
|
|
51 |
$parts = explode('|', $message); |
|
|
52 |
$explicitRules = array(); |
|
|
53 |
$standardRules = array(); |
|
|
54 |
foreach ($parts as $part) { |
|
|
55 |
$part = trim($part); |
|
|
56 |
|
|
|
57 |
if (preg_match('/^(?<interval>'.Interval::getIntervalRegexp().')\s+(?<message>.+?)$/x', $part, $matches)) { |
|
|
58 |
$explicitRules[$matches['interval']] = $matches['message']; |
|
|
59 |
} elseif (preg_match('/^\w+\: +(.+)$/', $part, $matches)) { |
|
|
60 |
$standardRules[] = $matches[1]; |
|
|
61 |
} else { |
|
|
62 |
$standardRules[] = $part; |
|
|
63 |
} |
|
|
64 |
} |
|
|
65 |
|
|
|
66 |
// try to match an explicit rule, then fallback to the standard ones |
|
|
67 |
foreach ($explicitRules as $interval => $m) { |
|
|
68 |
if (Interval::test($number, $interval)) { |
|
|
69 |
return $m; |
|
|
70 |
} |
|
|
71 |
} |
|
|
72 |
|
|
|
73 |
$position = PluralizationRules::get($number, $locale); |
|
|
74 |
if (!isset($standardRules[$position])) { |
|
|
75 |
throw new \InvalidArgumentException('Unable to choose a translation.'); |
|
|
76 |
} |
|
|
77 |
|
|
|
78 |
return $standardRules[$position]; |
|
|
79 |
} |
|
|
80 |
} |