symfony / symfony-docs

The Symfony documentation
https://symfony.com/doc
Other
2.16k stars 5.11k forks source link

[Translator] Unclear "Translate ICU messages directly in code" #15402

Closed ogizanagi closed 1 year ago

ogizanagi commented 3 years ago

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?

94noni commented 2 years ago

@ogizanagi according to https://github.com/symfony/symfony/pull/37371 and related doc PR https://github.com/symfony/symfony-docs/pull/13875 I think you are right, this need to have the MessageCatalogueInterface::INTL_DOMAIN_SUFFIX = '+intl-icu'; in it to work in code