I am trying to keep newlines in a yaml file.
In a messages.en.yml file, I have.
test:|
Here is line 1
Here is line 2
And in form.html.twig:
{{'test'|trans}}
Both lines are rendered on the same line.
Here is line 1 Here is line 2
In this link, the literal style for Yaml is explained.
and in this other one, it says "Notable lacking features are: document directives, multi-line quoted messages ..."
So is it possible in Symfony Yaml to use multilines? and how?
You could use |nl2br filter
{{ 'test'|trans|nl2br }}
Or, if you are sure your translation can't get altered by any user, you could use some few html in your translation.
test:|
Here is line 1<br/>
Here is line 2<br/>
<br/><br/>
Here is line 5
Then
{{ 'test'|trans|raw }}
Related
I am currently having the situation where a twig-filtered string that contains placeholders does only translate the string itself but does not respect the available, and linked, placeholders.
My example:
{{ 'this is my test: %test%'|trans({'%test%': 'test!'|trans}) }}
I have the translation available in my XLF file (automatically generated by symfony), lets say I add __ as prefixex to the string. I clear the cache and reload, and I do see that the string itself get translated, but now, instead of:
__this is my test: __test!
The string looks like this:
__this is my test: %test%
In other words, it does not seem to insert the placeholder as expected.
Why? The placeholder translation works however, if I remove the original translation for the parent string. So, only one does work:
translate the parent string
place the placeholder correctly
But not both at the same time.
The template where I am using this is included by another template. Is this maybe an issue?
Default engine of translator isn't enough smart to do it, and there is a lot of confusions with word between % like %test%. To translate sentences like yours, you should use the ICU format. Here is an article about the ICU format and Symfony
You can fix it in a few steps:
Surround the parameter with { } in the sentence to translate:
{{ 'this is my test: {test}'|trans({'test': 'test!'|trans}) }}
Tell to symfony your using the ICU format by adding the good extension on your translation file. As example, rename your file from messages.yaml to messages+intl-icu.en.yaml
Clear your cache
Report modifications to your messages+intl-icu.en.yaml file.
# translations/messages+intl-icu.en.yaml
test!: '__test!'
"this is my test: {test}": '__this is my test: {test}'
Be aware on the non-translated parameter in translated sentence! Keep in mind that {test} shall NOT be translated because it will be replace by value after the translation process.
In your code, the process is:
translate test! to __test!
translate this is my test: {test} to __this is my test: {test}
replace {test} by __test!
Result becomes like you expected it:
__this is my test: __test!
I am using saltstack to managed configuration on my servers. I have a use case where I need to prepend text to the beginning of the file WITHOUT creating a line break.
Currently:
/path/to/file:
file.prepend:
- text:
here is some text to add
This works great ACCEPT for the fact that "here is some text to add" is added on a new line at the top of the file. I need this text to be prepended at the beginning of the file without creating a new line break. I would love to do this with saltstack but I cannot find a solution to this problem anywhere in their documentation.
The text being added on a new line at the beginning of the file creates things to not work as expected. I have found a few examples of how to do this using sed or a bash script but would love to be able to use saltstack to accomplish this.
Has anyone experiences this same problem that can point me in the right direction?
I was able to come up with a solution for this using pattern matching and replacing.
add_text_to_start:
file.replace:
- name: /path/to/the/file
- pattern: text that will always be at the beginning
- repl: here is some text to add text that will always be at the beginning
The text that is at the beginning of the file will always be at the beginning of the file. So by matching the pattern I am able to add text to the beginning of the file without having to add a line break. All the rest of the text that follows the "text that will always be at the beginning of" is untouched and will be in the same place that is was in before the pattern find and replace occurred.
I am trying to use a non-breaking space character in a translation.
# messages.cs.yml
"City near river": "Město u řeky"
However, the non-breaking space character is escaped when translating in template.
{{ 'City near river'|trans }} # prints "Město u řeky"
Is there any way how to make this work?
You may try to add raw filter like:
{{ 'City near river'|trans|raw }}
This will prevent Twig from automatic escaping.
Also I think that it would be better to prevent breaking string in CSS instead of putting a there.
You can achieve that with CSS rule:
white-space:nowrap;
assigned to the element containing translated string.
This way you will get more flexible translations as you may not want to get this html entity every time you use this string.
I just found a solution using Unicode character:
# messages.cs.yml
"City near river": "Město u\xA0řeky"
I'm currently trying to build a chat app, using the official markdown package as well as underscore's escape function, and my template contains something like this:
<span class="message-content">
{{#markdown}}{{text}}{{/markdown}}
</span>
When I grab the text from the chat input box, I try to escape any HTML and then add in line breaks. safeText is then inserted into the database and displayed in the above template.
rawText = $("#chat-input-textbox").val();
safeText = _.escape(rawText).replace(/(?:\r\n|\r|\n)/g, '\n');
The normal stuff like headings, italics, and bold looks okay. However, there are two major problems:
Code escape issue - With the following input:
<script>alert("test")</script>
```
alert('hello');
```
This is _italics_!
Everything looks fine, except the alert('hello'); has become alert('hello'); instead. The <pre> blocks aren't rendering the escaped characters, which makes sense. But the problem is, the underscore JS escape function escapes everything.
SOLVED: Line break Issue - With the following input:
first
second
third
I get first second third being displayed with no line breaks. I understand this could be a markdown thing. Since I believe you need an empty line between paragraphs to get linebreaks in markdown. But having the above behaviour would be the most ideal, anyone know how to do this?
UPDATE Line break issue has been solved by adding an extra \n to my regex. So now I'm making sure that any line break will be represented with at least two \n characters (i.e. \n\n).
You should check the showdown docs and the wiki article they have on the topic.
The marked npm package, which is used by Telescope removes disallowed-tags. These include <script> of course. As the article I linked to above explains, there's still another problem with this:
<a href='javascript:alert("kidding! Im more the world domination kinda guy. Muhahahah")'>
click me for world peace!
</a>
Which isn't prevented by marked. I'd follow the advice of the author and use a HTML sanitation library. Like OWASP's ESAPI or Caja's html-sanitizer. Both of these project's seem outdated dough. I also found a showdown extension for it called showdown-xss-filter. So my advice is to write your own helper, and use showdown-xss-filter.
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?
Use |
email:
subject: My Subject
message: |
Hello %name%,
This is my second line
Best regards,
John Smith
Customer Care
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 }}