| 148 |
lars |
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 TranslatorInterface $translator;
|
|
|
26 |
private IntlFormatterInterface $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 |
public function format(string $message, string $locale, array $parameters = []): string
|
|
|
38 |
{
|
|
|
39 |
if ($this->translator instanceof TranslatorInterface) {
|
|
|
40 |
return $this->translator->trans($message, $parameters, null, $locale);
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
return strtr($message, $parameters);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
public function formatIntl(string $message, string $locale, array $parameters = []): string
|
|
|
47 |
{
|
|
|
48 |
return $this->intlFormatter->formatIntl($message, $locale, $parameters);
|
|
|
49 |
}
|
|
|
50 |
}
|