MessageFormatter.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\Component\Translation\MessageSelector;
  13. use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. /**
  16. * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  17. */
  18. class MessageFormatter implements MessageFormatterInterface, IntlFormatterInterface, ChoiceMessageFormatterInterface
  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($translator = null, IntlFormatterInterface $intlFormatter = null)
  26. {
  27. if ($translator instanceof MessageSelector) {
  28. $translator = new IdentityTranslator($translator);
  29. } elseif (null !== $translator && !$translator instanceof TranslatorInterface && !$translator instanceof LegacyTranslatorInterface) {
  30. throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
  31. }
  32. $this->translator = $translator ?? new IdentityTranslator();
  33. $this->intlFormatter = $intlFormatter ?? new IntlFormatter();
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function format($message, $locale, array $parameters = [])
  39. {
  40. if ($this->translator instanceof TranslatorInterface) {
  41. return $this->translator->trans($message, $parameters, null, $locale);
  42. }
  43. return strtr($message, $parameters);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function formatIntl(string $message, string $locale, array $parameters = []): string
  49. {
  50. return $this->intlFormatter->formatIntl($message, $locale, $parameters);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. *
  55. * @deprecated since Symfony 4.2, use format() with a %count% parameter instead
  56. */
  57. public function choiceFormat($message, $number, $locale, array $parameters = [])
  58. {
  59. @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the format() one instead with a %%count%% parameter.', __METHOD__), E_USER_DEPRECATED);
  60. $parameters = ['%count%' => $number] + $parameters;
  61. if ($this->translator instanceof TranslatorInterface) {
  62. return $this->format($message, $locale, $parameters);
  63. }
  64. return $this->format($this->translator->transChoice($message, $number, [], null, $locale), $locale, $parameters);
  65. }
  66. }