I want to implement translation in Symfony2 using csvFileLoader. In config.yml translation field is enabled and locale set to fr. I have written following code in controller.
$file = //file path eg: messages.fr.csv;
$loader = new CsvFileLoader($file);
$loader->setCsvControl(',');
$x = $loader->load($file, 'fr');
$translator = new Translator('fr',new MessageSelector());
$translator->addLoader('csv', $loader);
$translator->addResource('csv', $file, 'fr','messages');
$translator->trans('Symfony is great');
Above code works fine. If I use trans tag in twig then text is not translated. Even I add twig extentions:-
$loader = new \Twig_Loader_Filesystem("path to twig template file");
$twig = new \Twig_Environment($loader);
$twig->addExtension(new TranslationExtension($translator));
And Code in witten in twig file
{% trans %}Symfony2 is great{% endtrans %}
Above text is not translated in twig. I tried it using .xlf file then it works but for .csv file translation is not working. Need solution for above mentioned issue.
Controller :
/**
* @Route("{_locale}/translate1")
*/
public function showTwoAction(Request $request)
{
$delimiter = ";";
$enclosure = '"';
$escape = '\\';
$file = __DIR__.'/file_'.$request->getLocale().'.csv';
$translator = new Translator($request->getLocale(), new MessageSelector());
$translator->addLoader('csv', new CsvFileLoader());
$translator->addResource('csv', $file, $request->getLocale());
$catalogue = $translator->getCatalogue($request->getLocale());
$messages = $catalogue->all();
while ($catalogue = $catalogue->getFallbackCatalogue())
{
$messages = array_replace_recursive($catalogue->all(), $messages);
}
return $this->render("PRIYACoreTranslateBundle:Default:translate.html.twig",$messages);
}
Twig :
{{ messages.Hi }}
{{ messages.Hello }}
Translation CSV Files
=> file_en.csv
"Hi";"Hi"
"Hello";"Hello"
=> file_fr.csv
"Hi";"salut"
"Hello";"Bonjour"
Hope it helps.