How to force line break in Symfony2 translation fi

2020-03-01 07:43发布

问题:

In my messages.en.yml I have an entry that is used for an email body which is in plaintext, so I cannot use any HTML tags. Now when I need a line break and I add a blank line after, the line break appears in my email body.

But when I try to do a line break with a non empty line following, it does not break the lines.

email:
    subject: My Subject
    message: >
        Hello %name%,

        This is my second line

        Best regards,
        John Smith
        Customer Care

Arrives like this in my mailbox

Hello Tom,

This is my second line

Best regards, John Smith Customer Care

I've tried to play around with HTML tags or with the \n which I know from PHP. But if I use <br> or <br /> in the translation file and apply the |raw filter in my twig file, the HTML tags still appear in my email body. And also if I use \n to force a line break, no line break is given and instead, the \n appear in my email as well.

Now how can I force the line break if a non empty line is following?

回答1:

Use |

email:
    subject: My Subject
    message: |
        Hello %name%,

        This is my second line

        Best regards,
        John Smith
        Customer Care


回答2:

In your translations file (e.g. messages.en.yml):

your
   translation      
      key:
         here: |
                Lorem ipsum.

                CRMPICCO.

                www.crmpicco.co.uk.

Accessing it in your Twig template:

{{ 'your.translation.key.here'|trans|nl2br }}