Translate the Flash Message

落爺英雄遲暮 提交于 2019-12-13 12:26:34

问题


I'm trying to translate the flash message I sent, if a form is succesful. The normal request looks like this:

$request->getSession()->getFlashBag()->add(
            'notice',
            'Your E-Mail has been sent.'
        );

So I tried to translate the message with the following variable:

$request->getSession()->getFlashBag()->add(
            'notice',
            'contact.message.email_has_been_sent'
        );

After sending the form the message shows "contact.message.email_has_been_sent". So it didn't found the translation, but the variable is right. I tested it inside a template file. Has anyone an idea, how I could fix this? I didn't found anything useful yet.


回答1:


Presuming you are in a Controller:

$request->getSession()->getFlashBag()->add(
    'notice',
    $this->get('translator')->trans('contact.message.email_has_been_sent'));

Read how to handle Translations.




回答2:


Alternatively, in twig:

{% for flashMessage in app.session.flashbag.get('notice') %}
    <p>{{ flashMessage|trans }}</p>
{% endfor %}


来源:https://stackoverflow.com/questions/28588839/translate-the-flash-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!