Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

how am I supposed to get line breaks working in Symfony 2.4?

#messages.de.yml
foo: |
    Hello i am a line
    Hello i am a new line

and

#messages.de.yml
foo: >
    Hello i am a line
    Hello i am a new line

twig

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

The translation is working but line breaks aren't.

The documentation isn't really clear to me. Hints will be highly appreciated.

like image 391
smartius Avatar asked Jul 05 '14 17:07

smartius


2 Answers

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
like image 133
Dado Avatar answered Oct 12 '22 11:10

Dado


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>.yml
foo: >
    Hello i am a line \n
    Hello i am a new line
like image 16
Nicolai Fröhlich Avatar answered Oct 12 '22 10:10

Nicolai Fröhlich