简体   繁体   中英

Remove % on symfony translated text

I'm translating some texts on symfony this way

expired.password.body: 'Dear %name% %surname%,<br><br>Your password has expired.'

and this is the translation code

$email_params = [
                'name' => $user_to_change_password->getName(),
                'surname' => $user_to_change_password->getSurname()
            ];

$body = $this->translator->trans('expired.password.body', $email_params, 'emails');

then the text is translated and the parameters are correct but the % are still in the trandlated text

Dear %foo_name% %bar_surname%

I easy to solve with str_replace but I think that I should making something wrong on the translation

In Symfony's translator documentation, they are pointing at the fact that the keys of the argument defining the translation parameters should actually contains the percentage:

use Symfony\Component\Translation\TranslatableMessage;

// the first argument is required and it's the original message
$message = new TranslatableMessage('Symfony is great!');
// the optional second argument defines the translation parameters and
// the optional third argument is the translation domain
$status = new TranslatableMessage('order.status', ['%status%' => $order->getStatus()], 'store');

Source: https://symfony.com/doc/current/translation.html#translatable-objects


So, your code should read:

$email_params = [
  '%name%' => $user_to_change_password->getName(),
  '%surname%' => $user_to_change_password->getSurname()
];

$body = $this->translator->trans(
  'expired.password.body', 
  $email_params, 
  'emails'
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM