comparison vendor/symfony/translation/MessageSelector.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 use Symfony\Component\Translation\Exception\InvalidArgumentException;
15
16 /**
17 * MessageSelector.
18 *
19 * @author Fabien Potencier <fabien@symfony.com>
20 * @author Bernhard Schussek <bschussek@gmail.com>
21 */
22 class MessageSelector
23 {
24 /**
25 * Given a message with different plural translations separated by a
26 * pipe (|), this method returns the correct portion of the message based
27 * on the given number, locale and the pluralization rules in the message
28 * itself.
29 *
30 * The message supports two different types of pluralization rules:
31 *
32 * interval: {0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples
33 * indexed: There is one apple|There are %count% apples
34 *
35 * The indexed solution can also contain labels (e.g. one: There is one apple).
36 * This is purely for making the translations more clear - it does not
37 * affect the functionality.
38 *
39 * The two methods can also be mixed:
40 * {0} There are no apples|one: There is one apple|more: There are %count% apples
41 *
42 * @param string $message The message being translated
43 * @param int $number The number of items represented for the message
44 * @param string $locale The locale to use for choosing
45 *
46 * @return string
47 *
48 * @throws InvalidArgumentException
49 */
50 public function choose($message, $number, $locale)
51 {
52 preg_match_all('/(?:\|\||[^\|])++/', $message, $parts);
53 $explicitRules = array();
54 $standardRules = array();
55 foreach ($parts[0] as $part) {
56 $part = trim(str_replace('||', '|', $part));
57
58 if (preg_match('/^(?P<interval>'.Interval::getIntervalRegexp().')\s*(?P<message>.*?)$/xs', $part, $matches)) {
59 $explicitRules[$matches['interval']] = $matches['message'];
60 } elseif (preg_match('/^\w+\:\s*(.*?)$/', $part, $matches)) {
61 $standardRules[] = $matches[1];
62 } else {
63 $standardRules[] = $part;
64 }
65 }
66
67 // try to match an explicit rule, then fallback to the standard ones
68 foreach ($explicitRules as $interval => $m) {
69 if (Interval::test($number, $interval)) {
70 return $m;
71 }
72 }
73
74 $position = PluralizationRules::get($number, $locale);
75
76 if (!isset($standardRules[$position])) {
77 // when there's exactly one rule given, and that rule is a standard
78 // rule, use this rule
79 if (1 === count($parts[0]) && isset($standardRules[0])) {
80 return $standardRules[0];
81 }
82
83 throw new InvalidArgumentException(sprintf('Unable to choose a translation for "%s" with locale "%s" for value "%d". Double check that this translation has the correct plural options (e.g. "There is one apple|There are %%count%% apples").', $message, $locale, $number));
84 }
85
86 return $standardRules[$position];
87 }
88 }