MessageSelector.php 3.4KB

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