Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Translation: curly braces not removed from parameters #36461

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

Closed
gnutix opened this issue Apr 16, 2020 · 3 comments
Closed

Translation: curly braces not removed from parameters #36461

gnutix opened this issue Apr 16, 2020 · 3 comments

Comments

@gnutix
Copy link
Contributor

gnutix commented Apr 16, 2020

Symfony version(s) affected: 5.0.7

Description
Given a translation defined in translations/messages.fr.yaml :

product:
    add:
        success: 'Le produit {id} a été ajouté!'

Translated using : $translator->trans('product.add.success', ['id' => 1234]);
I would except the message Le produit 1234 a été ajouté!', but the result was Le produit {1234} a été ajouté!`.

Apparently, I'm not the only one: https://stackoverflow.com/questions/59708205/symfony-translation-ignores-curly-braces-in-parameters

@xabbuh
Copy link
Member

xabbuh commented Apr 16, 2020

That's expected. The translator replaces placeholders as they are (i.e. in your case the call needs to be $translator->trans('product.add.success', ['{id}' => 1234]);). Note that there is not special meaning of the curly braces in regard to placeholders. You could also use the percent sign as often used too (i.e. having a message like 'Le produit %id% a été ajouté!' and then call the translator this way: $translator->trans('product.add.success', ['%id%' => 1234]);).

So I am going to close here as there is nothing to fix. Thank you for understanding.

@xabbuh xabbuh closed this as completed Apr 16, 2020
@stof
Copy link
Member

stof commented Apr 17, 2020

Note that curly braces do have a special meaning when using the new ICU format. But for that, you would have to put your translation in messages+intl-icu.fr.yaml instead, to opt-in for the new format.

@badmansan
Copy link

It is very strange behavior. Because in doc we see:

// translations/messages+intl-icu.en.php
return [
    'say_hello' => "Hello {name}!",
];
// prints "Hello Fabien!"
echo $translator->trans('say_hello', ['name' => 'Fabien']);

in fact we get Hello {Fabien}!
Moving on.

// translations/messages+intl-icu.en.php
return [
    'say_hello' => "You name {name}!",
];
echo $translator->trans('say_hello', ['name' => 'Fabien']);

and we get
You Fabien {Fabien}!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants