Description
There is something unclear with this tip in https://symfony.com/doc/current/translation/message_format.html#selecting-different-messages-based-on-a-condition :
It’s possible to translate ICU MessageFormat messages directly in code, without having to define them in any file:
$invitation = '{organizer_gender, select,
female {{organizer_name} has invited you for her party!}
male {{organizer_name} has invited you for his party!}
other {{organizer_name} have invited you for their party!}
}';
// prints "Ryan has invited you for his party!"
echo $translator->trans($invitation, [
'organizer_name' => 'Ryan',
'organizer_gender' => 'male',
]);
but it won't work, unless I'm missing something, since there is no indication to use the icu format, on contrary of translations defined in a messages+intl-icu.fr.yaml
.
A solution is to provide the domain
argument with an arbitrary domain and append +intl-icu
:
// prints "Ryan has invited you for his party!"
echo $translator->trans($invitation, [
'organizer_name' => 'Ryan',
'organizer_gender' => 'male',
], 'message+intl-icu'); # <-arbitrary domain with `+intl-icu` appended for ICU format detection
I'm currently trying to use this directly in Twig, more for pluralization & other selectors reasons than for translations for which I would have defined translation keys in files:
<span>
{{ '{organizer_gender, select,
female {{organizer_name} has invited you for her party!}
male {{organizer_name} has invited you for his party!}
other {{organizer_name} have invited you for their party!}
}'|trans({
organizer_name: organizer_name,
organizer_gender: organizer_gender
}, domain="messages+intl-icu") }}
</span>
The intent is unclear behind this tip, but seems to legitimate this use-case.
Did I misunderstood something? How to clarify?