user3491057
user3491057

Reputation:

Symfony Translations

i'm using the last version of symfony (3.1.3) i want to trans multi vars. but i didn't find and good solution

right now i'm doing:

{% block h1 %}{{ 'service.create'|trans }} {{ ('service'|trans) }}{% endblock %}

I try :

{% block h1 %}{{ 'service','service.create'|trans }} }} { %endblock %}

but no luck.

I also try with

{% trans %}service.create|service{% endtrans %}

thanks

Upvotes: 1

Views: 357

Answers (4)

Aastal
Aastal

Reputation: 350

If you have a "service.yml.en" file or something like that :

{{ service.create|trans({}, "service") }}

Here the translator will search in your service.yml.en file and load the correct translation key.

If you want to do advance translations, you can use vars like that (for example) :

{{ (className|lower ~ "." ~ field)|trans({}, className|lower) }}

Here if you have a "user.yml.en" and you want to load something like "user.width", it'll work.

Good luck :)

Upvotes: 0

Hugo Fouquet
Hugo Fouquet

Reputation: 149

You can use parameters in your translation like this:

{{ 'service.create'|trans }}
{{ 'service.create'|trans({'%separator%': 'any text'}) }}

And in your messages.ru.yml

...
service:
    create: ... %separator% ...
    ...

If your separator is in html, add raw like this :

{{ 'service.create'|trans({'%separator%': '<br>'})|raw }}

You have more details in symfony doc here

Upvotes: 1

user3491057
user3491057

Reputation:

I needed to just trans multi vars. twig not support that...

Upvotes: 0

omxv
omxv

Reputation: 646

You can translate strings in backend:

public function indexAction($name)
{
    $translated = $this->get('translator')->trans('Hello '.$name);

    return new Response($translated);
}

Or in twig templates:

<h1>{{ 'service.create'|trans }}</h1>

Each time you create a new translation resource (or install a bundle that includes a translation resource), be sure to clear your cache so that Symfony can discover the new translation resources:

php bin/console cache:clear

More info: http://symfony.com/doc/current/translation.html

Upvotes: 0

Related Questions