Skip to content

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

Closed
@ogizanagi

Description

@ogizanagi

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    TranslationhasPRA Pull Request has already been submitted for this issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions