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; |
| 13 | |
| 14 | use Symfony\Contracts\Translation\TranslatableInterface; |
| 15 | use Symfony\Contracts\Translation\TranslatorInterface; |
| 16 | |
| 17 | /** |
| 18 | * @author Nate Wiebe <nate@northern.co> |
| 19 | */ |
| 20 | class TranslatableMessage implements TranslatableInterface |
| 21 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 22 | private string $message; |
| 23 | private array $parameters; |
| 24 | private ?string $domain; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 25 | |
| 26 | public function __construct(string $message, array $parameters = [], string $domain = null) |
| 27 | { |
| 28 | $this->message = $message; |
| 29 | $this->parameters = $parameters; |
| 30 | $this->domain = $domain; |
| 31 | } |
| 32 | |
| 33 | public function __toString(): string |
| 34 | { |
| 35 | return $this->getMessage(); |
| 36 | } |
| 37 | |
| 38 | public function getMessage(): string |
| 39 | { |
| 40 | return $this->message; |
| 41 | } |
| 42 | |
| 43 | public function getParameters(): array |
| 44 | { |
| 45 | return $this->parameters; |
| 46 | } |
| 47 | |
| 48 | public function getDomain(): ?string |
| 49 | { |
| 50 | return $this->domain; |
| 51 | } |
| 52 | |
| 53 | public function trans(TranslatorInterface $translator, string $locale = null): string |
| 54 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 55 | return $translator->trans($this->getMessage(), array_map( |
| 56 | static function ($parameter) use ($translator, $locale) { |
| 57 | return $parameter instanceof TranslatableInterface ? $parameter->trans($translator, $locale) : $parameter; |
| 58 | }, |
| 59 | $this->getParameters() |
| 60 | ), $this->getDomain(), $locale); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 61 | } |
| 62 | } |