PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 22, 2022

[FIXED] Remove % on symfony translated text

 January 22, 2022     php, symfony, symfony-3.4     No comments   

Issue

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


Solution

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'
);


Answered By - β.εηοιτ.βε
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing