Dimentica Dimentica - 10 months ago 79
Twig Question

Symfony translator and Twig

I have a templates library based on Twig, but I have not installed the whole Symfony library. I want to use Symfony's translation in my project, is this possible without installing the whole Symfony? Currently I have imported

"twig/twig": "~1.0",
"symfony/translation": "^3.0",
"symfony/yaml": "^3.0",

If this is possible, how do I have to "enable" the translator?


You can use the translator as stand-alone component. As described in the doc here:

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

$translator = new Translator('fr_FR');
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', array(
    'Symfony is great!' => 'J\'aime Symfony!',
), 'fr_FR');

var_dump($translator->trans('Symfony is great!'));

So you need to make a mechanism to load the catalog and use the translator in TWIG as a custom filter.

hope this help