Reputation: 295
How can I have the translations in variable.yml ? translation for subject Hello
notification:
agency.dpae.error:
mail:
subject: "Hello %name%" # translation subject
template: "client/mail/dpae_error.html.twig"
log:
type: TYPE_DPAE
from: FROM_AGENCY
messages.fr.yml
salut: "Hello"
namespace Di\NotificationBundle\Manager;
class SMSManager
{
$test= 'salut'
$name= 'fabien'
// translation salut to Hello
// display hello fabien
}
Upvotes: 2
Views: 2060
Reputation: 157839
You should put there not a translation but a key for translation:
notification:
agency.dpae.error:
mail:
subject: salut # translation subject
and translate it when used.
Given notification
is a part of parameters.yml, you can get value with
$subject = $this->container->getParameter('notification')["agency.dpae.error"]["mail"]["subject"];
Then you have to define a translation in a translation file
salut: "Hello %name%"
And then translate it using translator
public function sendAction()
{
$name= 'fabien';
$subject = $this->container->getParameter('notification')["agency.dpae.error"]["mail"]["subject"]; // salut
$subject = $this->get('translator')->trans($subject,['%name%' => $name]);
echo $subject; // "Hello fabien";
}
the point here is that you will have different translation files, where will be different translations for the same key. So you will have to store translations in a messages
file, while in configuration options you should use a key from a translation.
Upvotes: 1
Reputation: 39380
You should use a Message Placeholders in the translation yaml files:
notification:
agency.dpae.error:
mail:
subject: salut %key% translation subject
and pass the translation service to your class as dependency as example:
class SMSManager
{
public function __construct(
TranslatorInterface $translator
) {
$this->translator = $translator;
}
defined as example as:
acme_sms_manager:
class: SMSManager
arguments:
- '@translator'
and use as follow:
$translated = $this->translator->trans(
'notification.agency.dpae.error.mail.subject',
array('%key%' => 'Fabien')
);
PS: if the name of the file of the translation is variable.yml, you should pass the catalog as third argument, as example:
$translated = $translator->trans(
'notification.agency.dpae.error.mail.subject',
array('%key%' => 'Fabien'),
'variable'
);
hope this help
Upvotes: 1