2
votes

I've read tons of answers and tutorials about Symfony 2 translation, but i'm still not getting it to work. No errors are shown, but messages are not translated in twig templates (nor by testing $this->get('translator')->trans('...') ). The xliff file seems to be loaded because if i add a wrong character on purpose the markup Symfony throws me an error.

Here is my setup :

app/Resources/translations/messages.en.xliff

config.yml

translator: { fallback: "%locale%" }
...
default_locale:  "%locale%"

parameters.yml

locale: en

Contents of messages.en.xliff

<?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>test.message</source>
                <target>TRANSLATED TEST</target>
            </trans-unit>
        </body>
    </file>
</xliff>

It still does not work even after runing

php app/console cache:clear

And in my twig template (it's a included template...)

{{ "test.message"|trans }}

Any idea ? PS: I don't have other Bundles installed appart from the liip-imagine Bundle. Any help would be strongly appreciated !

1

1 Answers

4
votes

I solved my problem by adding symfony/translation into composer and then updating symfony and clearing the cache.

composer require symfony/translation

php app/console cache:clear

For those who stumble upon this post, here are some of the problems & things to check you could encounter when trying to make translations work (based on all of my reading)

  • Check composer for symfony/translation
  • Checks config.yml and parameters.yml (as show in initial post)
  • Check overriding of /messages.[locale].xliff from Bundle/Resources/translations/ vs app/Resources/translations/ don't override (or o
  • Always clear the cache when you add new translation files