TranslatableMessage.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. /**
  25. * @deprecated since Symfony 7.4
  26. */
  27. public function __toString(): string
  28. {
  29. trigger_deprecation('symfony/translation', '7.4', 'Method "%s()" is deprecated.', __METHOD__);
  30. return $this->getMessage();
  31. }
  32. public function getMessage(): string
  33. {
  34. return $this->message;
  35. }
  36. public function getParameters(): array
  37. {
  38. return $this->parameters;
  39. }
  40. public function getDomain(): ?string
  41. {
  42. return $this->domain;
  43. }
  44. public function trans(TranslatorInterface $translator, ?string $locale = null): string
  45. {
  46. $parameters = $this->getParameters();
  47. foreach ($parameters as $k => $v) {
  48. if ($v instanceof TranslatableInterface) {
  49. $parameters[$k] = $v->trans($translator, $locale);
  50. }
  51. }
  52. return $translator->trans($this->getMessage(), $parameters, $this->getDomain(), $locale);
  53. }
  54. }