TranslatableMessage.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. private $message;
  19. private $parameters;
  20. private $domain;
  21. public function __construct(string $message, array $parameters = [], string $domain = null)
  22. {
  23. $this->message = $message;
  24. $this->parameters = $parameters;
  25. $this->domain = $domain;
  26. }
  27. public function __toString(): string
  28. {
  29. return $this->getMessage();
  30. }
  31. public function getMessage(): string
  32. {
  33. return $this->message;
  34. }
  35. public function getParameters(): array
  36. {
  37. return $this->parameters;
  38. }
  39. public function getDomain(): ?string
  40. {
  41. return $this->domain;
  42. }
  43. public function trans(TranslatorInterface $translator, string $locale = null): string
  44. {
  45. return $translator->trans($this->getMessage(), array_map(
  46. static function ($parameter) use ($translator, $locale) {
  47. return $parameter instanceof TranslatableInterface ? $parameter->trans($translator, $locale) : $parameter;
  48. },
  49. $this->getParameters()
  50. ), $this->getDomain(), $locale);
  51. }
  52. }