blob: 04079648338c8c8b8dd93e382a5060f97ff6fb18 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?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
12namespace Symfony\Component\Translation\Formatter;
13
14use Symfony\Component\Translation\IdentityTranslator;
15use Symfony\Contracts\Translation\TranslatorInterface;
16
17// Help opcache.preload discover always-needed symbols
18class_exists(IntlFormatter::class);
19
20/**
21 * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
22 */
23class 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 */
40 public function format(string $message, string $locale, array $parameters = [])
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}