MessageSelector.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. @trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2, use IdentityTranslator instead.', MessageSelector::class), E_USER_DEPRECATED);
  12. use Symfony\Component\Translation\Exception\InvalidArgumentException;
  13. /**
  14. * MessageSelector.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. *
  19. * @deprecated since Symfony 4.2, use IdentityTranslator instead.
  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 are no apples|{1} There is one apple|]1,Inf] There are %count% apples
  32. * indexed: There is one apple|There are %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 are no apples|one: There is one apple|more: There are %count% apples
  40. *
  41. * @param string $message The message being translated
  42. * @param int|float $number The number of items represented for the message
  43. * @param string $locale The locale to use for choosing
  44. *
  45. * @return string
  46. *
  47. * @throws InvalidArgumentException
  48. */
  49. public function choose($message, $number, $locale)
  50. {
  51. $parts = [];
  52. if (preg_match('/^\|++$/', $message)) {
  53. $parts = explode('|', $message);
  54. } elseif (preg_match_all('/(?:\|\||[^\|])++/', $message, $matches)) {
  55. $parts = $matches[0];
  56. }
  57. $explicitRules = [];
  58. $standardRules = [];
  59. foreach ($parts as $part) {
  60. $part = trim(str_replace('||', '|', $part));
  61. if (preg_match('/^(?P<interval>'.Interval::getIntervalRegexp().')\s*(?P<message>.*?)$/xs', $part, $matches)) {
  62. $explicitRules[$matches['interval']] = $matches['message'];
  63. } elseif (preg_match('/^\w+\:\s*(.*?)$/', $part, $matches)) {
  64. $standardRules[] = $matches[1];
  65. } else {
  66. $standardRules[] = $part;
  67. }
  68. }
  69. // try to match an explicit rule, then fallback to the standard ones
  70. foreach ($explicitRules as $interval => $m) {
  71. if (Interval::test($number, $interval)) {
  72. return $m;
  73. }
  74. }
  75. $position = PluralizationRules::get($number, $locale);
  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) && isset($standardRules[0])) {
  80. return $standardRules[0];
  81. }
  82. 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));
  83. }
  84. return $standardRules[$position];
  85. }
  86. }