blob: b1a3b6b137e4df0f0dc2661774ac7e1ceca8c4cb [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;
13
14use Symfony\Contracts\Translation\TranslatableInterface;
15use Symfony\Contracts\Translation\TranslatorInterface;
16
17/**
18 * @author Nate Wiebe <nate@northern.co>
19 */
20class TranslatableMessage implements TranslatableInterface
21{
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010022 private string $message;
23 private array $parameters;
24 private ?string $domain;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020025
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 Benkard1ba53812022-12-27 17:32:58 +010055 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 Benkard7b2a3a12021-08-16 10:57:25 +020061 }
62}