Question

In my current project, I use the Silex framework with template engine TWIG. I have to internationalize my site. For this I imported the required module :

$app->register(new Silex\Provider\TranslationServiceProvider(), array(
        'locale_fallbacks' => array('fr'),
));

I created my YAML files in a folder 'translation':

use Symfony\Component\Translation\Loader\YamlFileLoader;
$app['translator'] = $app->share($app->extend('translator', function($translator, $app) {
    $translator->addLoader('yaml', new YamlFileLoader());
    $translator->addResource('yaml', __DIR__.'/translation/en.yml', 'en');
    $translator->addResource('yaml', __DIR__.'/translation/fr.yml', 'fr');
    return $translator;
}));

Now I know I should use 'gettext' to extract strings of my TWIG templates in a '.po' file, but I can't find how.

Some people talk about "Twig Gettext Extractor" but I don't think Silex supports this module.

Thanks for your help !

Was it helpful?

Solution

The translation provides .po loaders as of 2.1:

use Symfony\Component\Translation\Loader\PoFileLoader;

// ...
$translator->addLoader('po', new PoFileLoader());
$translator->addResource('po', __DIR__.'/translation/messages.en.po', 'en');
// ...

The component also provide other gettext loaders, for a full list see: https://github.com/symfony/Translation/tree/master/Loader

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top