Skip to content

Instantly share code, notes, and snippets.

@maximecolin
Created February 16, 2015 11:26
Show Gist options
  • Save maximecolin/de101ad4aa19fe7585bb to your computer and use it in GitHub Desktop.
Save maximecolin/de101ad4aa19fe7585bb to your computer and use it in GitHub Desktop.
Symfony2 : add translated flash with parameters, domain or locale
{% 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 %}
<?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