0
0

MessageFormatter.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // Help opcache.preload discover always-needed symbols
  16. class_exists(IntlFormatter::class);
  17. /**
  18. * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  19. */
  20. class MessageFormatter implements MessageFormatterInterface, IntlFormatterInterface, ChoiceMessageFormatterInterface
  21. {
  22. private $translator;
  23. private $intlFormatter;
  24. /**
  25. * @param TranslatorInterface|null $translator An identity translator to use as selector for pluralization
  26. */
  27. public function __construct($translator = null, IntlFormatterInterface $intlFormatter = null)
  28. {
  29. if ($translator instanceof MessageSelector) {
  30. $translator = new IdentityTranslator($translator);
  31. } elseif (null !== $translator && !$translator instanceof TranslatorInterface && !$translator instanceof LegacyTranslatorInterface) {
  32. 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)));
  33. }
  34. $this->translator = $translator ?? new IdentityTranslator();
  35. $this->intlFormatter = $intlFormatter ?? new IntlFormatter();
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function format($message, $locale, array $parameters = [])
  41. {
  42. if ($this->translator instanceof TranslatorInterface) {
  43. return $this->translator->trans($message, $parameters, null, $locale);
  44. }
  45. return strtr($message, $parameters);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function formatIntl(string $message, string $locale, array $parameters = []): string
  51. {
  52. return $this->intlFormatter->formatIntl($message, $locale, $parameters);
  53. }
  54. /**
  55. * {@inheritdoc}
  56. *
  57. * @deprecated since Symfony 4.2, use format() with a %count% parameter instead
  58. */
  59. public function choiceFormat($message, $number, $locale, array $parameters = [])
  60. {
  61. @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);
  62. $parameters = ['%count%' => $number] + $parameters;
  63. if ($this->translator instanceof TranslatorInterface) {
  64. return $this->format($message, $locale, $parameters);
  65. }
  66. return $this->format($this->translator->transChoice($message, $number, [], null, $locale), $locale, $parameters);
  67. }
  68. }