MessageFormatter.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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\IdentityTranslator;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. /**
  14. * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  15. */
  16. class MessageFormatter implements MessageFormatterInterface, IntlFormatterInterface
  17. {
  18. private $translator;
  19. private $intlFormatter;
  20. /**
  21. * @param TranslatorInterface|null $translator An identity translator to use as selector for pluralization
  22. */
  23. public function __construct(TranslatorInterface $translator = null, IntlFormatterInterface $intlFormatter = null)
  24. {
  25. $this->translator = $translator ?? new IdentityTranslator();
  26. $this->intlFormatter = $intlFormatter ?? new IntlFormatter();
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function format(string $message, string $locale, array $parameters = [])
  32. {
  33. if ($this->translator instanceof TranslatorInterface) {
  34. return $this->translator->trans($message, $parameters, null, $locale);
  35. }
  36. return strtr($message, $parameters);
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function formatIntl(string $message, string $locale, array $parameters = []): string
  42. {
  43. return $this->intlFormatter->formatIntl($message, $locale, $parameters);
  44. }
  45. }