annotate vendor/symfony/translation/MessageSelector.php @ 8:50b0d041100e

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