Translations not working in Symfony2
I have a file with French translations located at FooBundle/Resources/translations/messages.fr.xlf
Example:
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="1">
<source>Foo</source>
<target>Bar</target>
</trans-unit>
</body>
</file>
</xliff>
But I can't seem to get any translation work done, neither in the controller:
// FooBundle/Controller/DefaultController.php
/**
* @Route("/")
* @Template()
*/
public function indexAction(Request $request)
{
$request->setLocale("fr");
$translatedMessage = $this->get('translator')->trans('Foo');
echo $translatedMessage;
return array();
}
Or a branch template:
// FooBundle/Resources/views/Default/index.html.twig
{{ 'Foo'|trans }} or {% trans %}Foo{% endtrans %}
It always shows Foo
(original string).
I use English ('en') as the default. My local config from config.yml
:
framework:
translator: { fallback: "%locale%" }
default_locale: "%locale%"
...
I tried to clear the cache but it didn't make any difference.
If I try to debug the translations it shows they are in use:
$ php app/console debug:translation fr FooBundle
+----------+---------------+----------------------+
| State(s) | Id | Message Preview (fr) |
+----------+---------------+----------------------+
| | Foo | Bar |
+----------+---------------+----------------------+
Any idea what is going wrong here?
+3
source to share
1 answer