MessageFormatter.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. // Help opcache.preload discover always-needed symbols
  14. class_exists(IntlFormatter::class);
  15. /**
  16. * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  17. */
  18. class MessageFormatter implements MessageFormatterInterface, IntlFormatterInterface
  19. {
  20. private TranslatorInterface $translator;
  21. private IntlFormatterInterface $intlFormatter;
  22. /**
  23. * @param TranslatorInterface|null $translator An identity translator to use as selector for pluralization
  24. */
  25. public function __construct(?TranslatorInterface $translator = null, ?IntlFormatterInterface $intlFormatter = null)
  26. {
  27. $this->translator = $translator ?? new IdentityTranslator();
  28. $this->intlFormatter = $intlFormatter ?? new IntlFormatter();
  29. }
  30. public function format(string $message, string $locale, array $parameters = []): string
  31. {
  32. return $this->translator->trans($message, $parameters, null, $locale);
  33. }
  34. public function formatIntl(string $message, string $locale, array $parameters = []): string
  35. {
  36. return $this->intlFormatter->formatIntl($message, $locale, $parameters);
  37. }
  38. }