-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Mailer][TwigBridge] Add support for translatable subject #59967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,10 @@ | |
use Symfony\Component\Serializer\Normalizer\MimeMessageNormalizer; | ||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; | ||
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; | ||
use Symfony\Component\Serializer\Normalizer\TranslatableNormalizer; | ||
use Symfony\Component\Serializer\Serializer; | ||
use Symfony\Component\Translation\IdentityTranslator; | ||
use Symfony\Component\Translation\TranslatableMessage; | ||
|
||
class TemplatedEmailTest extends TestCase | ||
{ | ||
|
@@ -44,19 +47,22 @@ public function testSerialize() | |
->htmlTemplate('text.html.twig') | ||
->context($context = ['a' => 'b']) | ||
->locale($locale = 'fr_FR') | ||
->subject($subject = new TranslatableMessage('hello {{ name }}', ['name' => 'John'], 'greetings')) | ||
; | ||
|
||
$email = unserialize(serialize($email)); | ||
$this->assertEquals('text.txt.twig', $email->getTextTemplate()); | ||
$this->assertEquals('text.html.twig', $email->getHtmlTemplate()); | ||
$this->assertEquals($context, $email->getContext()); | ||
$this->assertEquals($locale, $email->getLocale()); | ||
$this->assertEquals($subject, $email->getTranslatableSubject()); | ||
} | ||
|
||
public function testSymfonySerialize() | ||
{ | ||
// we don't add from/sender to check that validation is not triggered to serialize an email | ||
$e = new TemplatedEmail(); | ||
$e->subject(new TranslatableMessage('hello.world')); | ||
$e->to('[email protected]'); | ||
$e->textTemplate('email.txt.twig'); | ||
$e->htmlTemplate('email.html.twig'); | ||
|
@@ -67,6 +73,7 @@ public function testSymfonySerialize() | |
|
||
$expectedJson = <<<EOF | ||
{ | ||
"subject": "hello.world", | ||
"htmlTemplate": "email.html.twig", | ||
"textTemplate": "email.txt.twig", | ||
"locale": "en", | ||
|
@@ -84,6 +91,15 @@ public function testSymfonySerialize() | |
} | ||
], | ||
"headers": { | ||
"subject": [ | ||
{ | ||
"value": "hello.world", | ||
"name": "Subject", | ||
"lineLength": 76, | ||
"lang": null, | ||
"charset": "utf-8" | ||
} | ||
], | ||
"to": [ | ||
{ | ||
"addresses": [ | ||
|
@@ -108,6 +124,7 @@ public function testSymfonySerialize() | |
$serializer = new Serializer([ | ||
new ArrayDenormalizer(), | ||
new MimeMessageNormalizer($propertyNormalizer), | ||
new TranslatableNormalizer(new IdentityTranslator()), | ||
new ObjectNormalizer(null, null, null, $extractor), | ||
$propertyNormalizer, | ||
], [new JsonEncoder()]); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
namespace Symfony\Component\Mailer\EventListener; | ||
|
||
use Symfony\Bridge\Twig\Mime\TemplatedEmail; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Mailer\Event\MessageEvent; | ||
use Symfony\Component\Mailer\Exception\InvalidArgumentException; | ||
|
@@ -19,9 +20,11 @@ | |
use Symfony\Component\Mime\Header\Headers; | ||
use Symfony\Component\Mime\Header\MailboxListHeader; | ||
use Symfony\Component\Mime\Message; | ||
use Symfony\Component\Translation\TranslatableMessage; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
/** | ||
* Manipulates the headers and the body of a Message. | ||
* Manipulates the headers, subject, and the body of a Message. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
|
@@ -45,6 +48,7 @@ | |
private ?Headers $headers = null, | ||
private ?BodyRendererInterface $renderer = null, | ||
array $headerRules = self::DEFAULT_RULES, | ||
private ?TranslatorInterface $translator = null, | ||
) { | ||
foreach ($headerRules as $headerName => $rule) { | ||
$this->addHeaderRule($headerName, $rule); | ||
|
@@ -68,6 +72,7 @@ | |
} | ||
|
||
$this->setHeaders($message); | ||
$this->translateSubject($message); | ||
$this->renderMessage($message); | ||
} | ||
|
||
|
@@ -115,6 +120,15 @@ | |
} | ||
} | ||
|
||
private function translateSubject(Message $message): void | ||
{ | ||
if (!$message instanceof TemplatedEmail || !$this->translator || !$message->getTranslatableSubject() instanceof TranslatableMessage) { | ||
norkunas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
} | ||
|
||
$message->subject($message->getTranslatableSubject()->trans($this->translator, $message->getLocale())); | ||
Check failure on line 129 in src/Symfony/Component/Mailer/EventListener/MessageListener.php
|
||
} | ||
|
||
private function renderMessage(Message $message): void | ||
{ | ||
if (!$this->renderer) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Symfony\Component\Mailer\Tests\EventListener; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bridge\Twig\Mime\TemplatedEmail; | ||
use Symfony\Component\Mailer\Envelope; | ||
use Symfony\Component\Mailer\Event\MessageEvent; | ||
use Symfony\Component\Mailer\EventListener\MessageListener; | ||
|
@@ -20,6 +21,8 @@ | |
use Symfony\Component\Mime\Header\MailboxListHeader; | ||
use Symfony\Component\Mime\Header\UnstructuredHeader; | ||
use Symfony\Component\Mime\Message; | ||
use Symfony\Component\Translation\TranslatableMessage; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
class MessageListenerTest extends TestCase | ||
{ | ||
|
@@ -114,4 +117,25 @@ public static function provideHeaders(): iterable | |
]; | ||
yield 'Capitalized header rule (case-insensitive), replace if set' => [$initialHeaders, $defaultHeaders, $defaultHeaders, $rules]; | ||
} | ||
|
||
public function testTranslatableSubject() | ||
{ | ||
$message = new TemplatedEmail(); | ||
$message->subject(new TranslatableMessage('hello.world')); | ||
$listener = new MessageListener(translator: new class implements TranslatorInterface { | ||
public function trans(string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string | ||
{ | ||
return 'Hello World'; | ||
} | ||
|
||
public function getLocale(): string | ||
{ | ||
return 'en'; | ||
} | ||
}); | ||
$event = new MessageEvent($message, new Envelope(new Address('[email protected]'), [new Address('[email protected]')]), 'smtp'); | ||
$listener->onMessage($event); | ||
|
||
$this->assertSame('Hello World', $message->getSubject()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,9 +58,9 @@ class Email extends Message | |
/** | ||
* @return $this | ||
*/ | ||
public function subject(string $subject): static | ||
public function subject(string|\Stringable $subject): static | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may be a breaking change for apps that extend this class and override this method (https://3v4l.org/uuJj9) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Supporting a TranslatableInterface is absolutely not the same than supporting Stringable. There is no requirement for TranslatableInterface to be Stringable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, my idea was off the cuff. Needs to be thought out more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. btw, even for the core TranslatableMessage (that is stringable), casting it to string does not translate it. It returns the translation key. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, I thought that'd be fine as that's the behavior of using keys that don't have a translation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My thinking was to allow the subject to be any stringable object allowing a listener to intercept. If none does, it would just be cast to a string. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding support for Stringable and then doing weird magic to support TranslatableMessage while the changelog says it adds support for translatable subject (which would be understood as being the interface by many people) looks wrong to me (and limiting as there are valid use cases for other implementations of TranslatableInterface) |
||
{ | ||
return $this->setHeaderBody('Text', 'Subject', $subject); | ||
return $this->setHeaderBody('Text', 'Subject', (string) $subject); | ||
} | ||
|
||
public function getSubject(): ?string | ||
|
Uh oh!
There was an error while loading. Please reload this page.