Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 'trans' domain inside Twig template

I'd like to do this:

$this->get('translator')->trans('notice.unregistered', array(), 'index');

Inside Twig template, so I don't have to pass this as an argument. How?

like image 375
Misiur Avatar asked Oct 07 '11 15:10

Misiur


3 Answers

You can also do using trans filter :

{{ 'translationkey'|trans({},'domain') }}
like image 77
krishna Avatar answered Oct 09 '22 08:10

krishna


The solution is:

{% trans from "domain" %}text{% endtrans %}
like image 28
Misiur Avatar answered Oct 09 '22 09:10

Misiur


You can add custom functions to change domains inside your templates.

Add your functions:

$getTextdomain = new Twig_SimpleFunction('get_textdomain', function () {
    return textdomain(NULL);
});
$setTextdomain = new Twig_SimpleFunction('set_textdomain', function ($domain) {
    textdomain($domain);
});

$twig->addFunction($getTextdomain);
$twig->addFunction($setTextdomain);

Then use it:

{% set originalDomain = get_textdomain() %}
{{ set_textdomain('errors') }}
{% trans "My error message" %}
{{ set_textdomain(originalDomain) }}
like image 5
kbtz Avatar answered Oct 09 '22 09:10

kbtz