Created
February 16, 2015 11:26
-
-
Save maximecolin/de101ad4aa19fe7585bb to your computer and use it in GitHub Desktop.
Symfony2 : add translated flash with parameters, domain or locale
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% for message in app.session.flashbag.get('success') %} | |
{% if message is iterable %} | |
{{ message.id|trans(message.parameters|default({}), message.domain|default(null), message.locale|default(null)) }} | |
{% else %} | |
{{ message|trans }} | |
{% endif %} | |
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\DemoBundle; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller | |
class MyController extends Controller | |
{ | |
/** | |
* @Template() | |
*/ | |
public function indexAction() | |
{ | |
// Before 2.6 | |
$this->container->get('session')->getFlashBag()->add('success', [ | |
'id' => 'flash.my_controller.index.success', | |
'parameters' => ['%date%' => date('d/y/M')], | |
]); | |
// After 2.6 | |
$this->addFlash('success', [ | |
'id' => 'flash.my_controller.index.success', | |
'parameters' => ['%date%' => date('d/y/M')], | |
]); | |
// Change domain | |
$this->addFlash('success', [ | |
'id' => 'flash.my_controller.index.success', | |
'parameters' => ['%date%' => date('d/y/M')], | |
'domain' => 'controller', | |
]); | |
// Change locale | |
$this->addFlash('success', [ | |
'id' => 'flash.my_controller.index.success', | |
'parameters' => ['%date%' => date('d/y/M')], | |
'locale' => 'fr', | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment