I try to add translation for Twig DateExtension in my Silex application. Here is what I did for now.
First I added the requested components in my composer.json
"twig/twig": "^1.22", "twig/extension": "^1.3", "symfony/twig-bridge": "^2.7", "symfony/translation": "^2.7", "symfony/config": "^2.7", "symfony/yaml": "^2.7",
Then I register services like this:
<?php
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/../views',
));
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
'locale_fallbacks' => array('en'),
'locale' => 'fr_FR'
));
$app['translator'] = $app->share($app->extend('translator', function($translator, $app) {
$translator->addLoader('yaml', new Symfony\Component\Translation\Loader\YamlFileLoader());
$translator->addResource('yaml', __DIR__.'/../src/locales/fr.yml', 'fr_FR');
return $translator;
}));
$app['twig'] = $app->share($app->extend('twig', function(Twig_Environment $twig, $app) {
$twig->addExtension(new Twig_Extensions_Extension_Text());
$twig->addExtension(new Twig_Extensions_Extension_Date($app['translator']));
return $twig;
}));
In my fr.yml I try to put translation like this:
diff.ago.day: Il y a %count% jour|Il y a %count% jours
test: converted
This configuration seems to be partially working because in my template I have (where foo is a DateTime):
{{ foo|time_diff }} - {{ "test"|trans }}
The result should be:
Il y a 1 jour - converted
But it's:
diff.ago.day - converted
So the first part isn't converted while the second one is.
Where is my mistake ?
Thanks a lot.
EDIT:
I finally found my mistake. After digging into Symfony code, I found they use a special domain for translation: 'date'. So I add a forth parameter to my ressource loading in order to tell it.
The new loading line is now:
$translator->addResource('yaml', __DIR__.'/../src/locales/fr.yml', 'fr_FR', 'date');