Skip to content

[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

Merged
merged 1 commit into from
Mar 26, 2025

Conversation

norkunas
Copy link
Contributor

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #54203
License MIT

@norkunas
Copy link
Contributor Author

@stof can you review this?

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this, which adds cross-cutting concerns to the base class (= combinatory explosion), what about allowing Email objects to implement TranslatableInterface?

@norkunas
Copy link
Contributor Author

Instead of this, which adds cross-cutting concerns to the base class (= combinatory explosion), what about allowing Email objects to implement TranslatableInterface?

So the trans method would act as a subject getter?

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Mar 17, 2025

Hum, maybe not, I thing I had something else in mind ("static" instead of "string" as return type):

interface TranslatableInterface
{
    public function trans(TranslatorInterface $translator, ?string $locale = null): static;
}

🤔

@norkunas
Copy link
Contributor Author

For me it doesn't matter if I'd need to call trans or translatableSubject, still it will need to be stored as it's done now, no?

@kbond
Copy link
Member

kbond commented Mar 25, 2025

What about having the existing subject accept a stringable object? Then, in the listener (or a translation specific one), check for an instance of TranslatableMessage and translate?

@norkunas
Copy link
Contributor Author

What about having the existing subject accept a stringable object? Then, in the listener (or a translation specific one), check for an instance of TranslatableMessage and translate?

If argument widening is not a BC break then sure

@norkunas norkunas force-pushed the translatable-mail-subject branch from a9b69b4 to fb59ac0 Compare March 26, 2025 05:18
@norkunas
Copy link
Contributor Author

What about having the existing subject accept a stringable object?

Done.

@norkunas norkunas force-pushed the translatable-mail-subject branch from fb59ac0 to 97eb49b Compare March 26, 2025 08:36
@fabpot
Copy link
Member

fabpot commented Mar 26, 2025

Thank you @norkunas.

@fabpot fabpot merged commit fcdc0cf into symfony:7.3 Mar 26, 2025
3 of 11 checks passed
@norkunas norkunas deleted the translatable-mail-subject branch March 26, 2025 08:53
@@ -58,9 +58,9 @@ class Email extends Message
/**
* @return $this
*/
public function subject(string $subject): static
public function subject(string|\Stringable $subject): static
Copy link
Member

Choose a reason for hiding this comment

The 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)

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

@kbond kbond Mar 26, 2025

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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)

@stof
Copy link
Member

stof commented Mar 26, 2025

Hum, maybe not, I thing I had something else in mind ("static" instead of "string" as return type):

interface TranslatableInterface
{
    public function trans(TranslatorInterface $translator, ?string $locale = null): static;
}

This does not make sense. The return value of TranslatableInterface::trans is the translated text, which must be a string, not an instance of the current class (it would be impossible to implement that interface properly)

@kbond
Copy link
Member

kbond commented Mar 26, 2025

@norkunas, without this pr, you could achieve subject translation in the template I think?

{% do email.setSubject(...translate...) %}

@norkunas
Copy link
Contributor Author

@norkunas, without this pr, you could achieve subject translation in the template I think?

{% do email.setSubject(...translate...) %}

This looks like a workaround :)

fabpot added a commit that referenced this pull request Mar 26, 2025
…ble objects" (kbond)

This PR was squashed before being merged into the 7.3 branch.

Discussion
----------

[Mailer][TwigBridge] Revert "Add support for translatable objects"

| Q             | A
| ------------- | ---
| Branch?       | 7.3
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | Reverts #60047 & #59967
| License       | MIT

#59967 introduced a BC break so we need to revert and find another solution.

Commits
-------

41cc1d6 code review
f638a6a Revert "[Mailer][TwigBridge] Add support for translatable subject"
e51607b Revert "fix compatibility with TwigBridge < 7.3"
@fabpot fabpot mentioned this pull request May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Subject of TemplatedEmail should also be in a template
7 participants