symfony2: how to include line breaks / newlines in translations? symfony2: how to include line breaks / newlines in translations? symfony symfony

symfony2: how to include line breaks / newlines in translations?


Twig does not convert line feeds to <br /> automatically.

Use the nl2br filter.

#template.html.twig{{ 'foo'|trans|nl2br }}

I'm not quite sure but eventually you'll have to add \n to the translation strings additonally.

#messages.<locale>.ymlfoo: >    Hello i am a line \n    Hello i am a new line


In your YAML file use:

foo: |    first line    second line

and in your TWIG file:

<p>{{ 'foo'|trans|nl2br }}</p>

tested on:

  • Symfony version 2.6.6
  • twig/twig 1.x-dev 6792014


For me the following version did the trick:

foo: "| Hello \n this is a new line"