Skip to content

[Translation] Introduce NonTranslatableMessage #60935

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

Open
wants to merge 5 commits into
base: 7.4
Choose a base branch
from

Conversation

VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Jun 28, 2025

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

When working with translation, it's "classic" to have library with the following logic:

if ($message instanceof Translatable) {
    $message->trans($translator);
} else {
    $translator->trans($message, domain: 'OpenSourceBundle');
}

Then if the user doesn't want the text to be translated, he cannot.

A useful and simple way would be to support new TranslatableMessage($text, domain: false) but as recommended, it might be better to introduce a special class for this, the NonTranslatableMessage (name to be challenged)

@carsonbot

This comment has been minimized.

@VincentLanglet VincentLanglet marked this pull request as ready for review June 28, 2025 07:15
@carsonbot carsonbot added this to the 7.4 milestone Jun 28, 2025
@carsonbot carsonbot changed the title Allow to use false as domain for translatableMessage [Translation] Allow to use false as domain for translatableMessage Jun 29, 2025
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.

can you please add a line in the changelog ?

@mdeboer
Copy link
Contributor

mdeboer commented Jun 29, 2025

Just out of curiosity, do you have any examples where you have a Translatable message, that shouldn't be translated?

Don't get me wrong but to me it really smells. A Translatable object should inherently be translatable. Setting the domain to false does not really tell me that it shouldn't be translated, rather I would expect a "default" domain to be used like "messages".

But maybe some examples would clear things up for me why this path was chosen.

Thanks in advance! 🙏🏻

@VincentLanglet
Copy link
Contributor Author

Setting the domain to false does not really tell me that it shouldn't be translated, rather I would expect a "default" domain to be used like "messages".

Let's just answer this point first @mdeboer, it's the purpose of the NULL domain, you can find it in a lot of "domain" options like https://symfony.com/doc/current/reference/forms/types/form.html#translation-domain

Just out of curiosity, do you have any examples where you have a Translatable message, that shouldn't be translated?

Don't get me wrong but to me it really smells. A Translatable object should inherently be translatable.

Sure, for instance EasyAdmin always translate his labels, title, flash message, and more
https://github.com/EasyCorp/EasyAdminBundle/blob/4.x/templates/flash_messages.html.twig#L12
It use messages or EasyAdminBundle as default translation domains based on the context, but support using Translatable object in order to use another domain but doesn't support disabling the translation.

Maybe the right behavior would be:

  • to not translate by default
  • to ask users to pass translatableMessage instead

but:

  • it's a big BC break
  • when 99% of labels asked to be translated it ask adding t() everywhere

I find it simpler to allow false as translation_domain for such library ; especially because it's not the first library I saw with this "translate by default"...

@OskarStark OskarStark changed the title [Translation] Allow to use false as domain for translatableMessage [Translation] Allow to use false as domain for translatableMessage Jun 29, 2025
@OskarStark OskarStark changed the title [Translation] Allow to use false as domain for translatableMessage [Translation] Allow to use false as domain for TranslatableMessage Jun 29, 2025
@mdeboer
Copy link
Contributor

mdeboer commented Jun 29, 2025

Thanks for your reply @VincentLanglet

I understand that in case of EasyAdmin it is easier to allow this change, but imho this is a design flaw in EasyAdmin. Why should the framework bend the rules and accommodate this bad design pattern?

Wouldn't something like Rector be of use to refactor EasyAdmin?

Or maybe introduce something like an UntranslatableMessage?

@VincentLanglet
Copy link
Contributor Author

VincentLanglet commented Jun 29, 2025

Thanks for your reply @VincentLanglet

I understand that in case of EasyAdmin it is easier to allow this change, but imho this is a design flaw in EasyAdmin. Why should the framework bend the rules and accommodate this bad design pattern?

Wouldn't something like Rector be of use to refactor EasyAdmin?

No, because

  • it's mainly translated in the view which are twig templates
  • such change would be an HARD BC-break

And while it could be considered as a design flaw, be aware that

  • such libs was implemented before the introduction of translatable interface
  • it could also be considered as a useful shortcut to automatically translate text when it's the case in 99%.

For instance, form labels are translated by default by Symfony, and they allow a false translation domain now, but you could use the same argument to say it shouldn't translate and wait for TranslateMessage in order to produce a translation, or you should pass the label already-translated.

Or maybe introduce something like an UntranslatableMessage?

Might be better indeed
I update the PR with a NonTranslatableMessage @mdeboer

(cc @nicolas-grekas if you want to re-review since the whole PR changed).

@VincentLanglet VincentLanglet changed the title [Translation] Allow to use false as domain for TranslatableMessage [Translation] Introduce UntranslatableMessage Jun 29, 2025
@VincentLanglet VincentLanglet changed the title [Translation] Introduce UntranslatableMessage [Translation] Introduce NonTranslatableMessage Jun 29, 2025
Copy link
Contributor

@mdeboer mdeboer left a comment

Choose a reason for hiding this comment

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

Nice, looks good to me 👍🏻 Now it is a clear indicator that the message should not be translated and it fixes the original problem.

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.

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy