7

I'm trying to access to the translations via twig.

For example, I have the name of my application inside my Resources/translations/messages.de.yml and Resources/translations/messages.en.yml

My controller does only a render of the twig file.

And inside my twig-file I want to access to the application.name property which is defined inside the messages-file (yml)

How can I access to this property to get the application name (let's say it contains some language-specific information)

I tried these methods, and failed:

  • {{ application.name }}
    • Looks more like for variables which have been sent through the controller, I've got an error, that the variable 'application' was not found
  • {% trans% } application.name {% endtrans %}
    • displays application.name
  • {% trans% } 'application.name' {% endtrans %}
    • displays 'application.name'
1
  • Can you show us the content of your messages.en.yml ?
    – Touki
    Feb 21, 2013 at 17:33

2 Answers 2

25

With inline notation you should use filter:

{{ 'application.name'|trans }}

With trans tag I think problem in whitespaces around application.name

2
  • Don't forget to mention (I know is not asked but just gave a best answer) that if the message is not defined in the default domain the translation is done with {{ 'application.name' | trans ({}, 'domain') }}
    – Axxiss
    Feb 21, 2013 at 21:00
  • Thanks, somehow I had also this commented out in my app/config/config.yml file (i don't know why): framework.translator: { fallback: %locale% }
    – eav
    Feb 25, 2013 at 7:58
-1
{% trans% }app.name{% endtrans %}

In your messages.en.yml

<trans-unit id="app.name" resname="app.name">
    <source>My app</source>
    <target>My app</target>
  </trans-unit>

In your messages.de.yml

 <trans-unit id="app.name" resname="app.name">
    <source>My app</source>
    <target>Meine App</target>
  </trans-unit>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.