TranslatableMessage.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. return $translator->trans($this->getMessage(), array_map(
  43. static fn ($parameter) => $parameter instanceof TranslatableInterface ? $parameter->trans($translator, $locale) : $parameter,
  44. $this->getParameters()
  45. ), $this->getDomain(), $locale);
  46. }
  47. }