ASP.Net MVC: Localized texts with new line? - c#

I've an Asp.Net MVC 3 website, which will be localized.
I've several resx files which contains my texts, and I've in my views some
#My.NameSpace.Through.My.LocalizationFile.Key
But I can't make it represent the new line.
I tried:
Shift+enter: I've got the new line in the resource file, but not in my browser
\r\n : I see the \r\n in my browser
\n : Same
<br/> : I see the <br/> in my text
So what should I do to have a new line?
Edit: I know that I could use Html.Raw, but I just can't ask to translators to put html code in their translation.

To be honest, I know it's not the nicest thing in the world, but it's fool-proof and it means your translators don't have to put any code in their translations:
Building upon the answers already given, why don't you just use Html.Raw, but before doing so, replacing the \r\n that using Shift+Enter in the resource file results in, with a <br />
So say for example you had the string named Welcome in the resource file ApplicationMessage, you could do:
#Html.Raw(ApplicationMessage.Welcome.Replace("\r\n", "<br />")
That will give you what you need. Here's a similar question:
HTMLencode HTMLdecode

You can put the <br /> for the line breaks and use the #Html.Raw() method to show the string with the line break instead of the <br /> string.

I think you should use a combination of Shift+Enter and the CSS white-space property instead of potentionally opening up to XSS vulnerabilities, as you would do using #Html.Raw() solution.
<span style="white-space: pre-line">#My.NameSpace.Through.My.LocalizationFile.Key</span>
I don't know the specific case, but it could be that you might find that pre-wrap suits your case better. Read more about the different white-space properties here

You should probably use <br /> and render the output with Html.Raw()

Yeah, thanks to mattytommo.
You can use
First line <br /> second line
in resource, or
resource.Replace("\r\n", "<br />")
in code and Shift + Enter in resource editor.
Both works fine, but you have to use
#Html.Raw();

Related

Ensure that a bloc of text will be displayed on the same page

I'm generating reports from a .docx document using HtmlToOpenXml.
I need to ensure that a particular html block will be displayed on the same page, for example:
<p>Video provides a powerful way to help [...]</p>
<br />
<br />
<p>To make your document look professionally [...]</p>
I took a look around the web:
Open XML Table Header Same page
Create page break using OpenXml
<w:pPr><w:keepNext/></w:pPr> had my attention but I'm not sure that I can put two paragraphs inside a larger one.
I'm aware that it will depend of the font, size and so on but it will not change.
Use "page-break-inside" style in your first block surrounding content to move it to new page. Then try to keep other blocks small enough to fit page (no matter how hard you will try, if the content is too big, it won't fit on one page). Like in example:
<div style="page-break-inside: avoid">
<p>Video provides a powerful way to help [...]</p>
<br />
<br />
<p>To make your document look professionally [...]</p>
</div>
Take a look at documentation here: CSS page-break-before Property

ASP.NET HTML Encoded Data-Bind and br tag

I'm trying to encode data that is to be displayed on a separate page (.aspx) after a user submits a form with their information and questions. There are both text input and textarea fields that the user can fill out.
I'm using the <%#: %> expression to place this data on the page since it does the HTML encoding, but have run into an issue with the encoding also encoding the br tags as text instead of actual line breaks.
Is there a way to make it so that the br tags still cause a line break, but leave everything else encoded using this method?
Example of user input:
Hi
This is what should be responded with.
Here is an example of the code that places it on the page:
<p><%#: ((DiscussionThread.Discussion)Container.DataItem).Text %></p>
What it currently displays on the page:
Hi<br /><br />This is what should be responded with.
Thanks in advanced!
Don't know, will it help you in your case or no, but I always use next way:
string forInput = yourTextBoxID.Text.ToString().Replace(Environment.NewLine, "%NL%");
string forOutput = forInput.Replace("%NL%", Environment.NewLine);
If in forOutput Environment.NewLine will not work, change it to <br />
%NL% it is just a identificator, u may change to anything else.

Html Encoding causing new lines in Chrome

In my signup form, I'm using asp.net unobtrusive validation to check if a username exists. My JsonResult method returns the following if a clash is found:
This is already in use. How about \u003cstrong\u003efoo123\u003c/strong\u003e?
I use Microsoft's unobtrusive validation to display this in my view:
#Html.ValidationMessageFor(m => m.Username)
which causes it to appear as follows in the page source:
This is already in use. How about
<strong>foo123</strong>
?
As you can see, the text is split over three lines. This is not a problem in Firefox, but in Chrome these new lines are causing the displayed text to break in a similar manner to <br />. I believe the encoding is to blame for this - can anyone explain why? Is there any solution to this issue?
Thank you in advance.
Without having tried it, you could try to replace newlines in the string with... nothing?
#Html.ValidationMessageFor(m => m.Username).ToString().Replace(Environment.NewLine, "");
Feels really weird that would be the problem though since it's just in Chrome, double-check CSS rules. Or just surround the validation message in a p-tag?
If they are displayed with line breaks on the actual page, then first thing I would do is inspect <strong/> tag and see if there is any CSS that defines it as display:block; or overrides white-space property.
If you are concerned about output of the DOM explorer... That's just how Chrome displays DOM trees. These three lines are each individual nodes (2 text nodes and one element node), so they are treated as equal. If you inspect the first paragraph of my answer, you will see the same thing.
try tweaking with css.
\u003cdiv\u003e
This is already in use. How about \u003cstrong\u003efoo123\u003c/strong\u003e?
\u003c/div\u003e?
I think this is due the some padding issue. Upload the generated Html code of the page.

HTML string does not get verified

These spaces are not added by me on HTML SIDE and i cannot edit HTML
I want to know what should my comparison string?
I am using watin to automate website testing process but I am unable to encounter only one button.Every other works
watin searches content by name /values /id and many more and works fine but when i see the value of the submit button that i need to be clicked it has some breaks &nsbp so i think they are playing some role
Here is the html:
<span class='button'><input type="submit" value=" Login " /></span>
<span class='button'><input type="button" value=" Back " onclick="history.back(-1)" /></span>
and here is the code to search
browser.Button(WatiN.Core.Find.ByValue(" Login ")).Click();
what can be done??
-- Suggestion -- (i.e. too big for a comment)
You shouldn't use to add spaces to the submit button. Rather, you should use CSS to style the button to your liking. So you would have something like:
input[type=button] {
padding:10px;
min-width: 150px;
}
By the same token, this could eliminate any of the issues you're having with selecting the button. It could be an issue of encodings breaking with watin and as a result, doing this with CSS will make debugging the issue much cleaner and much easier.
Edit:
Have you tried searching by ID as opposed to by value? ID's are supposed to be unique on a page, so if it doesn't find it by those means, then that's one issue that can be rules out. It could also be the fact that you're searching for a button. A <button> is not the same as a <input type="button">.
Edit 2: Even though the issue was due to encodings breaking, I still recommend you reset that button to reset the text (removing all the non breaking spaces) and attach an id/name to it. The reason being for internationalization purposes - and if for some reason you modify the size of the button in the designer, or i18n the app and the text is different, your test will break.
You shouldn't use entities with WatiN.
This code will work, but you have to use real non-breaking space character:
browser.Button(
WatiN.Core.Find.ByValue(
"   Login   ")).Click();
This is probably inconvenient, but you could use (after adding reference to System.Web) HttpUtility class:
browser.Button(
WatiN.Core.Find.ByValue(
System.Web.HttpUtility.HtmlDecode(
" Login "))).Click();
But, if I were you, I would just go with Regex:
browser.Button(
WatiN.Core.Find.ByValue(
new Regex(#"^\s*Login\s*$"))).Click();
or even new Regex("Login").
Interesting thing: If you ever will have to Find.ByText you don't have to bother so much, and you can use regular space (ie. not exactly non-breaking space). That's because native IE IHTMLElement::getAttribute (http://msdn.microsoft.com/en-us/library/aa752280(VS.85).aspx) converts from innertext attribute to regular spaces, but from value, id etc. it doesn't ( are converted to real non-breaking spaces - 0xA0)
Wow, you really like spaces! I would remove those and use padding/margins like html was designed to be used. Then you wont need all those spaces and you can assign a proper value to your button which watiN will recognize.
I think it is because the in the HTML source is actually an escaped version of the special character that represents a none breaking space. So in you C# source, you'll probably need that character instead of the html entity code. I think you can find the code of that character by using this button to submit a GET form. It will show the escaped character code in the url.
Of course it is better not to put the spaces in there at all. You should give the button a padding using CSS instead.

Render or convert Html to 'formatted' Text (.NET)

I'm importing some data from another test/bug tracking tool into tfs, and I would like to convert it's description, which is in simple HTML, so a plain string, where the 'layout' of the HTML is preserved.
For example:
<body>
<ol>
<li>Log on with user Acme & Co.</li>
<li>Navigate to the details tab</li>
<li>Check the official name</li>
</ol>
<br>
<br>
Expected Result:<br>
official name is filled in<br>
<br>
Actual Result:<br>
The &-sign is not shown correctly<br>
See attachement.
</body>
Would become plain text with newlines inserted and HTML-entities translated like:
1. Log on with user Acme & Co.
2. Navigate to the details tab
3. Check the official name
Expected Result:
official name is filled in
Actual Result:
The &-sign is not shown correctly
See attachment
I can currently replace some tags with newlines using a regex and strip the rest, but replacing the HTML-entities and stuff like <ol> and <ul> seemed like I'm re-inventing something (browser?). So I was wondering if someone has done this before me. I can't find it using Google.
Rather than regex, you could try loading it into the HTML agility pack? If it was xhtml, then an xslt transformation might be a good option.
In the end, once I got more comfortable with TFS, I customized the work item type to include a new HTML Field, and just copied the contents into that field.
This solution was so much better, because we could now see the intended formatting of the field.

Categories