Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Symfony\Component\Translation\Formatter; |
| 13 | |
| 14 | use Symfony\Component\Translation\IdentityTranslator; |
| 15 | use Symfony\Contracts\Translation\TranslatorInterface; |
| 16 | |
| 17 | // Help opcache.preload discover always-needed symbols |
| 18 | class_exists(IntlFormatter::class); |
| 19 | |
| 20 | /** |
| 21 | * @author Abdellatif Ait boudad <a.aitboudad@gmail.com> |
| 22 | */ |
| 23 | class MessageFormatter implements MessageFormatterInterface, IntlFormatterInterface |
| 24 | { |
| 25 | private $translator; |
| 26 | private $intlFormatter; |
| 27 | |
| 28 | /** |
| 29 | * @param TranslatorInterface|null $translator An identity translator to use as selector for pluralization |
| 30 | */ |
| 31 | public function __construct(TranslatorInterface $translator = null, IntlFormatterInterface $intlFormatter = null) |
| 32 | { |
| 33 | $this->translator = $translator ?? new IdentityTranslator(); |
| 34 | $this->intlFormatter = $intlFormatter ?? new IntlFormatter(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * {@inheritdoc} |
| 39 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 40 | public function format(string $message, string $locale, array $parameters = []): string |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 41 | { |
| 42 | if ($this->translator instanceof TranslatorInterface) { |
| 43 | return $this->translator->trans($message, $parameters, null, $locale); |
| 44 | } |
| 45 | |
| 46 | return strtr($message, $parameters); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * {@inheritdoc} |
| 51 | */ |
| 52 | public function formatIntl(string $message, string $locale, array $parameters = []): string |
| 53 | { |
| 54 | return $this->intlFormatter->formatIntl($message, $locale, $parameters); |
| 55 | } |
| 56 | } |