MessageFormatter.php 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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\Formatter;
  11. use Symfony\Component\Translation\MessageSelector;
  12. /**
  13. * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  14. */
  15. class MessageFormatter implements MessageFormatterInterface, ChoiceMessageFormatterInterface
  16. {
  17. private $selector;
  18. /**
  19. * @param MessageSelector|null $selector The message selector for pluralization
  20. */
  21. public function __construct(MessageSelector $selector = null)
  22. {
  23. $this->selector = $selector ?: new MessageSelector();
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function format($message, $locale, array $parameters = array())
  29. {
  30. return strtr($message, $parameters);
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function choiceFormat($message, $number, $locale, array $parameters = array())
  36. {
  37. $parameters = array_merge(array('%count%' => $number), $parameters);
  38. return $this->format($this->selector->choose($message, (int) $number, $locale), $locale, $parameters);
  39. }
  40. }