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


It seems that the problem was another bundle I installed (LuneticsLocaleBundle).



Somehow it has to override $request->setLocale("fr");

which I am doing in the controller, so it turns out that I was using en_US

as a locale. It is for this reason that he did not show translations.

0


source







All Articles