Twig translate whole text of rendered view
<p>Using Twig I render a particular view. I need this view to be translated into a language I choose. I display the view using:</p>

<pre><code>return $this-&gt;setup-&gt;twig-&gt;display($view, $params);
</code></pre>

<p>Where <code>$view</code> is the name of the <code>*.html.twig</code> template and <code>$params</code> is an array with the parameters I need to pass.</p>

<p>However, if I want to translate the template before displaying it, how I have to do it?</p>

<p>Currently I have included .yml files for different languages and I have also replaced the text inside the views with the appropriate corresponding values from the yml file.</p>

<p>Apart from everything else, I have also loaded the Twig translator in a file separate from the rest of the project. It has the following code:</p>

<pre><code>require dirname(__DIR__) . '/vendor/autoload.php';

use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\ArrayLoader;

class Translation
{
    public $translator;

    public function translator()
    {
        $this-&gt;translator = new Translator('fr_FR');
        $this-&gt;translator-&gt;addLoader('array', new ArrayLoader());
        $this-&gt;translator-&gt;addResource('array', array(
            'Symfony is great!' =&gt; 'J\'aime Symfony!',
        ), 'fr_FR');

        var_dump($this-&gt;translator-&gt;trans('Symfony is great!'));
    }
}

$show = new Translation;
$show-&gt;translator();
</code></pre>

<p>And it really displays the translation. </p>

<p>Still, I have no idea how to connect everything together.... </p>