Drupal investigation

MessageSelector.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. class MessageSelector
  18. {
  19. /**
  20. * Given a message with different plural translations separated by a
  21. * pipe (|), this method returns the correct portion of the message based
  22. * on the given number, locale and the pluralization rules in the message
  23. * itself.
  24. *
  25. * The message supports two different types of pluralization rules:
  26. *
  27. * interval: {0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples
  28. * indexed: There is one apple|There are %count% apples
  29. *
  30. * The indexed solution can also contain labels (e.g. one: There is one apple).
  31. * This is purely for making the translations more clear - it does not
  32. * affect the functionality.
  33. *
  34. * The two methods can also be mixed:
  35. * {0} There are no apples|one: There is one apple|more: There are %count% apples
  36. *
  37. * @param string $message The message being translated
  38. * @param int $number The number of items represented for the message
  39. * @param string $locale The locale to use for choosing
  40. *
  41. * @return string
  42. *
  43. * @throws \InvalidArgumentException
  44. */
  45. public function choose($message, $number, $locale)
  46. {
  47. $parts = explode('|', $message);
  48. $explicitRules = array();
  49. $standardRules = array();
  50. foreach ($parts as $part) {
  51. $part = trim($part);
  52. if (preg_match('/^(?P<interval>'.Interval::getIntervalRegexp().')\s*(?P<message>.*?)$/xs', $part, $matches)) {
  53. $explicitRules[$matches['interval']] = $matches['message'];
  54. } elseif (preg_match('/^\w+\:\s*(.*?)$/', $part, $matches)) {
  55. $standardRules[] = $matches[1];
  56. } else {
  57. $standardRules[] = $part;
  58. }
  59. }
  60. // try to match an explicit rule, then fallback to the standard ones
  61. foreach ($explicitRules as $interval => $m) {
  62. if (Interval::test($number, $interval)) {
  63. return $m;
  64. }
  65. }
  66. $position = PluralizationRules::get($number, $locale);
  67. if (!isset($standardRules[$position])) {
  68. // when there's exactly one rule given, and that rule is a standard
  69. // rule, use this rule
  70. if (1 === count($parts) && isset($standardRules[0])) {
  71. return $standardRules[0];
  72. }
  73. 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));
  74. }
  75. return $standardRules[$position];
  76. }
  77. }