-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
[Translator][TwigBridge] allow using trans() with a list of messages to use as fallbacks #35745
Conversation
0321ec8
to
7fc6701
Compare
d922594
to
1b63852
Compare
|
||
$translator = $this->getTranslator(); | ||
|
||
foreach ($messages as $msg) { |
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.
- This does not mean the message is not translated
- I'm not sure the $arguments, $domain, $locale will be the same in every situation
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.
- right now, passing an array is not allowed. So yes, it means the message is not translated, because we just defined it that way
- then you'd need another interface, which this PR doesn't aim to solve.
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.
- Oups, I commented on the wrong line :/ It should have been on the next line.
$msg !== $message = $translator->trans()
because we just defined it that way
ATM, this behavior is not documented
- OK
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.
Explanation added in the docblock.
…to use as fallbacks
1b63852
to
c32265d
Compare
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.
in general, if the translator is type TranslatorBagInterface
, we could do a better check.
i guess for translating purposes it doesnt matter too much (assuming the translation differs from its ID), but still ..conceptually we cannot break the fallback chain with a literal ID translation.
/** | ||
* {@inheritdoc} | ||
* | ||
* @param string|string[]|null $id |
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.
why the null case?
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.
because it's already allowed in the main translator, there is no reason to be stricter here, especially when that means less code.
this PR just did, with no practical drawback :) |
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.
another thing, wont this try all fallback locales per message ID first? Instead of trying all message IDs in the current locale, and then try them all for a fallback locale.
Not sure what's the expected behavior, i tend to believe we should try all message IDs first before trying a fallback locale.
I would expect the most specific id to have higher precedence, no matter the locale. Eg. I'm fine with the generic french version of some sentence when the Canadian-french is the first locale. |
The implemented behavior here does not match the linked issue as spotted by @ro0NL Let's say I have the following translation
With the following code: {{ ['contact.lost_object.url', 'contact.url']|trans }} When the current locale is It will display:
instead of
The implementation in #35636 is correct. You can see the test suite. |
I can very easily construct another example where the expected precedence is the one implemented here. Any graph of possibilities has several ways to be linearized into a single list of possibilities, and there is a use case for every possible way. We will have to favor one way vs another, and this preference will not fit everyone's needs. We may just close for this reason actually. |
i tend to favor translating all message IDs in the active locale first. Then, if still unavailable, try another round for a fallback locale. Given the fallback locale may be a different language, i think it makes sense to try all message IDs in our own language first. |
OK, works for me. Signature-wise, I think this PR is what we need. On the behavior side (fallback order), I've no specific opinion. I'm closing so that someone else can take over. |
I really like this feature and want to use it ! I presented my use case in #35745 |
@walva please, open a new issue |
Alternative of #35636
In Twig:
{{ ["bar", "fallback"]|trans }}'
In PHP:
A message is considered not translated when its translation is the same as its id.