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.
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
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"
Related
I followed the official Symfony documentation on how to translate messages using the new ICU message format.
Inside the messages+intl-icu.pl.yaml file I have the following entry:
pages:
show:
ratings_title: Opinie
ratings_count: >
{count, plural,
=0 {brak opinii}
one {jedna opinia}
few {# opinie}
other {# opinii}
}
Which is consumed as follows:
<div>
<h1>{{ 'pages.show.ratings_title'|trans }}</h1>
<sub>({{ 'pages.show.ratings_count|trans({'count': count}) }})</sub>
</div>
Which results in:
<div>
<h1>Opinie</h1>
<sub>(brak opinii
)</sub>
</div>
While the expected output should be:
<div>
<h1>Opinie</h1>
<sub>(brak opinii)</sub>
</div>
So my question is: how to remove that trailing newline character after the translated message?
Ok this was simply due to misconfigured YAML file. In order to strip YAML's trailing newline inside a multi-line string I should have written the translation entry as follows:
pages:
show:
ratings_title: Opinie
ratings_count: >-
{count, plural,
=0 {brak opinii}
one {jedna opinia}
few {# opinie}
other {# opinii}
}
Notice the use of >- instead of >
I have this text :
Ch d\`Arnb
Now I want to remove all \ from string if exist. I try a lot of solutions, one of this : {{ item|replace({'\/': ''}) }}. But without success. What can I try next?
There is an error in the replace parameter. You are presently trying to remove the / char. Try this:
{% set item = 'Foo\Bar' %}
{{ item|replace({'\\': ''}) }}
Output: FooBar
My twig template file
{{ 'Hello *world*' | format_text('markdown') }}
Error:
Template "<p>Hello <em>world</em></p>" is not defined.
There is a BC break in the SonataFormatterBundle. You can revert to version 3.2.0 or apply a patch. As mentioned in this issue.
How can I translate string containing colon in symfony2 using twig? Here's what I have:
Inside twig template:
{% trans with {'%order_no%': order_no} %}Loading offer: %order_no%...{% endtrans %}
Inside translation file:
Loading offer: %order_no%...: Įkeliamas užsakymas %order_no%...
As expected this doesn't work. How can I make it work?
You need to use quotes around the text as such:
'Loading offer: %order_no%...': Įkeliamas užsakymas %order_no%...
edited to show the correct example.
I'm displaying some variable retrieved in my database using Twig :
<p>{{ my_variable }}</p>
The thing is this variable may contain html tags, such as "<br />".
Twig seems to automatically call some htmlentities-like function when displaying variables.
Is there any way to disable it so that when I display a variable containing "Hello<br />world !" I get :
Hello
world !
rather than :
Hello<br />world !
Thanks
Use {{ my_variable|raw }} to prevent my_variable from being automatically escaped.
See Twig documentation: http://twig.sensiolabs.org/doc/filters/raw.html
Try using this
{% autoescape false %}{{ my_variable}}{% endautoescape %}
even better: {{ '<br />|raw('html') }} to avoid unescaping other sensible stuff.
If you just want to use linebreaks in the text stored in your database but don't care to use html , you can also use the nl2br filter as in {{ var|nl2br }}. Allows you to use string linebreak character \n in your text. Filter converts it to <br/>