-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Translation] Translate translatable parameters #41858
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
[Translation] Translate translatable parameters #41858
Conversation
@kylekatarnls I'm wondering if using a placeholder for "awesome" is a good practice in the first place for this example 🤔 A placeholder in a translation string is basically a parameter which place varies from one language to another. And it may be formatted differently too (like m/d/y vs d/m/y, or the decimal separator). IMO, if the placeholder requires translation (and not just formatting) then you are actually splitting a sentence. You'll hit grammar issues (like plural issues) at some point. I do believe that this unit test is misleading. But it should use another value for the placeholder like a name. For instance, turn => Do you have a real-world example where you would need to translate the placeholder? cc @ro0NL |
Yes I have real-world examples where sub-sentences grammar is handled well (we have no issue because translators are aware, can use ICU format messages and context, number of languages is limited, etc.):
Yes, and |
@kylekatarnls Thank you for the real-world use-cases. I get your point and it makes sense 👍 I think your PR should be merged first, and then I'll update mine #41136 to use |
IIUC we don't nessecarily aim for eg. |
@ro0NL My point is: if it's a "translatable message" as the class name stand for and we pass it as a parameter of an other translatable message, one may fairly expect:
But what is the point is just casting it to string silently (so you get the message ID)? What is the real case where you could expect the current behavior for |
same as #41136, to me this is just another form of formatting parameters i'd say as such the feature to me sounds like; translator supporting translatable parameters. or put different, im not sure |
@ro0NL I had the same first reaction as you: That's why I asked @kylekatarnls for other use cases and it turns out that placeholder values fall into 3 categories:
Think of "Hello Mr Sylvain, it is 8AM so get up!" in en_US, and "Bonjour M. Sylvain, il est 8:00 alors lève-toi !" in fr_FR The en_US translation string with its placeholders is:
|
well my first reaction was #41858 (comment) :D my point is, shouldnt these 2-ways-of-doing-things be equivalent semantically spoken? $message = new TranslatableMessage('Symfony is %what%!', ['%what%' => new TranslatableMessage('awesome')]);
echo $message->trans($translator, 'fr');
// vs
echo $translator->trans('Symfony is %what%!', ['%what%' => new TranslatableMessage('awesome')]); |
Formatting (like in #41136) in a single way (having only the locale) is different from translating using a translator service (so you can mock it, customize, having dynamic translations). So with the current implementation for parameter formatting, no it does not solve the issue about translating sub-strings. See the following: return t('You will get %something% and %otherthing% when %event%', [
'something' => t('a T-shirt'),
'otherthing' => t('a cup'),
'event' => t('you will finish the contest'),
]); (returned value above to be translated later with some translator service and locale calculated somehow from user or any other way) If something is 10 possible solutions, otherthing 10, and event 100, sorry for the "bad practice" but we won't send 10 000 strings to our translators for every single possible output. That's the clear and intuitive way to build the message, and assuming #41136 is merged, you still have to wrap each About your second question, I'm not sure what you mean, currently both are possible and behave the same ( |
they do not. The casting is a side-effect, if we pass specific parameters and/or domain to I think #41136 is on the right track, but instead of |
Absolutely, if #41136 handles |
@ro0NL @kylekatarnls Are you guys working together to give me some work? 😅 More seriously, I'm okay with refactoring my PR to drop |
Hi guys, want to mention that my PR only add support for the interface in arguments, that's quite scoped to the strict minimum to support any implementation of That's why I think it would be nice to have an other review from @nicolas-grekas now I fixed the first review's comments. And maybe him and @fabpot could give an opinion about this feature, says if it goes to 5.4 or 6 (then I would re-ajust the closure style). The @sylfabre #41136 PR is going quite further as it also provides implementations for precise cases, add a new dependency, so before refactoring it to use |
I think it's important to keep $translator->trans the main entry point. Contract defines |
Hello, |
Hello @ro0NL @kylekatarnls
I'm 100% aligned with this ☝️ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nobody can prevent splitting translatable strings into chunks that lead to grammar issues. This PR is not needed to follow that bad practice :)
Since there are legit use cases that have been described, and since this makes perfect sense from a technical pov (the raw message is dumped instead right now, which is worse), I'm good with the proposal.
Thank you @kylekatarnls. |
37d52df
to
a31dff4
Compare
This PR was merged into the 6.1 branch. Discussion ---------- [Translation] Translatable parameters | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | TBD ✅ The TwigExtension now supports messages implementing `TranslatableInterface` (https://symfony.com/blog/new-in-symfony-5-2-translatable-objects). ✅ Thanks to #41858, `TranslatableMessage` now supports recursive `TranslatableInterface` as params. ❌ But using `TranslatableInterface` as params with regular messages is not supported yet. 💡 This PR addresses this issue and makes it possible to create dedicated `TranslatableInterface` implementations like the one from #41136 _Note: I would like also to add `TranslatableInterface` to the `trans(?string $id)` signature method like with the one from the [TwigExtension](https://github.com/symfony/symfony/blob/6.1/src/Symfony/Bridge/Twig/Extension/TranslationExtension.php#L111) but I don't know how to do it without breaking BC. Any advice is welcome!_ Commits ------- 5beaee8 [Translation] Translatable parameters
Considering unit test expectation showing
Symfony est awesome !
there is something quite not so awesome in the fact that "awesome" is not translated. As it looks like we're trying to make a french sentence, it sounds to me that what we actually expect is to have the whole sentence translated including the parameters if they are themselves translatable.So with the following code:
And with all translations properly provided to the
$translator
, I expect:Currently, we get: