TranslatableMessage.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation;
  11. use Symfony\Contracts\Translation\TranslatableInterface;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. /**
  14. * @author Nate Wiebe <nate@northern.co>
  15. */
  16. class TranslatableMessage implements TranslatableInterface
  17. {
  18. public function __construct(
  19. private string $message,
  20. private array $parameters = [],
  21. private ?string $domain = null,
  22. ) {
  23. }
  24. public function __toString(): string
  25. {
  26. return $this->getMessage();
  27. }
  28. public function getMessage(): string
  29. {
  30. return $this->message;
  31. }
  32. public function getParameters(): array
  33. {
  34. return $this->parameters;
  35. }
  36. public function getDomain(): ?string
  37. {
  38. return $this->domain;
  39. }
  40. public function trans(TranslatorInterface $translator, ?string $locale = null): string
  41. {
  42. $parameters = $this->getParameters();
  43. foreach ($parameters as $k => $v) {
  44. if ($v instanceof TranslatableInterface) {
  45. $parameters[$k] = $v->trans($translator, $locale);
  46. }
  47. }
  48. return $translator->trans($this->getMessage(), $parameters, $this->getDomain(), $locale);
  49. }
  50. }