MessageSelector.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /**
  12. * MessageSelector.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. *
  17. * @api
  18. */
  19. class MessageSelector
  20. {
  21. /**
  22. * Given a message with different plural translations separated by a
  23. * pipe (|), this method returns the correct portion of the message based
  24. * on the given number, locale and the pluralization rules in the message
  25. * itself.
  26. *
  27. * The message supports two different types of pluralization rules:
  28. *
  29. * interval: {0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples
  30. * indexed: There is one apple|There are %count% apples
  31. *
  32. * The indexed solution can also contain labels (e.g. one: There is one apple).
  33. * This is purely for making the translations more clear - it does not
  34. * affect the functionality.
  35. *
  36. * The two methods can also be mixed:
  37. * {0} There are no apples|one: There is one apple|more: There are %count% apples
  38. *
  39. * @param string $message The message being translated
  40. * @param integer $number The number of items represented for the message
  41. * @param string $locale The locale to use for choosing
  42. *
  43. * @return string
  44. *
  45. * @throws \InvalidArgumentException
  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. if (preg_match('/^(?P<interval>'.Interval::getIntervalRegexp().')\s*(?P<message>.*?)$/x', $part, $matches)) {
  57. $explicitRules[$matches['interval']] = $matches['message'];
  58. } elseif (preg_match('/^\w+\:\s*(.*?)$/', $part, $matches)) {
  59. $standardRules[] = $matches[1];
  60. } else {
  61. $standardRules[] = $part;
  62. }
  63. }
  64. // try to match an explicit rule, then fallback to the standard ones
  65. foreach ($explicitRules as $interval => $m) {
  66. if (Interval::test($number, $interval)) {
  67. return $m;
  68. }
  69. }
  70. $position = PluralizationRules::get($number, $locale);
  71. if (!isset($standardRules[$position])) {
  72. // when there's exactly one rule given, and that rule is a standard
  73. // rule, use this rule
  74. if (1 === count($parts) && isset($standardRules[0])) {
  75. return $standardRules[0];
  76. }
  77. throw new \InvalidArgumentException(sprintf('Unable to choose a translation for "%s" with locale "%s". Double check that this translation has the correct plural options (e.g. "There is one apple|There are %%count%% apples").', $message, $locale));
  78. }
  79. return $standardRules[$position];
  80. }
  81. }