You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.6 KiB

3 years ago
  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 $translator;
  21. private $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. /**
  31. * {@inheritdoc}
  32. */
  33. public function format(string $message, string $locale, array $parameters = [])
  34. {
  35. if ($this->translator instanceof TranslatorInterface) {
  36. return $this->translator->trans($message, $parameters, null, $locale);
  37. }
  38. return strtr($message, $parameters);
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function formatIntl(string $message, string $locale, array $parameters = []): string
  44. {
  45. return $this->intlFormatter->formatIntl($message, $locale, $parameters);
  46. }
  47. }