I have a site developed in C#.net and VS2010. It is is localized and works well overall. However, some of the localization strings don't look the best.
For example, I have a message at the top of the login page
Currently it appears like this:
Your session has expired. Please login to
continue.
I would like it to appear like this:
Your session has expired.
Please login to continue.
I can't change the size of the containing div because the width could be different for each language.
I am looking for a way to put layout capabilities in the localization file. The simplest approach (on the surface) is to put new line characters in the string. However, \n, \r\n, and <br/> all appear in the string because it is rendered with " around the string.
Is there another approach that will work? Is this a bad idea? How else can we compensate for length differences accross the many languages?
The best approach in this case is to use HTML formatting in your string (both in English and in your translation) and where necessary adjust the translation.
There is no reason why you could not include <br /> in your string and have it rendered as intended. I don't know if you are using WebForms or ASP.NET MVC, but in the case of MVC you can avoid the default behaviour (automatic HTML encoding of your string) by using the Html.Raw helper, for example instead of this:
<div class='whatever'>#Resources.MyString</div>
Do this:
<div class='whatever'>#Html.Raw(Resources.MyString)</div>
Related
I have just installed CK Editor onto a form that submits data to a database, when I want to use an apostrophe ' It is displaying as & #39; on my web page instead.
How can I get it to display an apostrophe instead?
What's happening is that somewhere along the line, CKEditor (or maybe another part of the system) is going through and converting characters that might potentially cause problems (due to having special meaning in HTML) into their HTML entity representations.
This is normal behaviour and if you don't need to do any string manipulation inside your database you can happily leave it as is for that stage. Indeed you can have them in along with normal HTML text and it should render just fine.
Clearly your setup is sufficiently different that something isn't happening. So, you'll want to use something like PHP's html_entity_decode() to convert back to normal unescaped text. There should be an equivalent function available in any language with a half-decent standard library.
i'm trying to figure out how to most elegantly integrate something like PHP's nl2br() function into a current project done with JSF2/Spring. I could easily create getters using something like return text.replaceAll("\n","<br/>"); in my model classes, however that does seem like putting view related code where it does not belong. I have the same feeling about storing actual html content in my database.
I guess the cleanest solution would be using tags/EL for this, however i couldn't find something that seemed to do just that. How would you guys implement something like this? Thank you in advance, any hints are highly appreciated!
Use either CSS, assuming that the text doesn't contain any HTML
<div style="white-space: pre">#{bean.text}</div>
Or create a custom EL function and then display it unescaped (Facelets implicitly escapes HTML)
<div><h:outputText value="#{my:nl2br(bean.text)}" escape="false" /></div>
You should only make absolutely sure that it's free of XSS.
Well, in the first place JSF is a Web UI framework. So, anything that you expect to output to the user will end as HTML (with the only exception of javascript, though). So, I don't find it a grave violation of MVC (if any at all). Maybe you could even push the envelope and directly use <br/> inside the text, instead of replacing \n
In a more general sense, if you have different lines/paragraphs in your text, the more flexible/standard solution would be break your text in the different elements and let your presentation logic handle it. So, instead of a properties with
presentationPage.introductionText=Ipse Lorum ...sum.\nVini vidi vinci.
You would end with
presentationPage.introductionText.par1=Ipse Lorum ...sum.
presentationPage.introductionText.par2=vini vidi vinci.
I have something like the StackOverflow's "Ask Question" page, where a text-box is used to put source markup code and, below, another text-box (non-editable) is used to render a preview of the post/question being typed.
Consider that my application not only lets me use a simple tiny markup language for my posts' content; it also provides me with the possibility to type math in it using MathJax. But the syntax is simple Latex syntax (however this is not the problem, so do not let this thing bother you too much).
The problem
The problem is the following. The page is a little different from the StackOverflow's one because it uses ASP.NET with AJAX in order to call a server-side procedure that performs the markup language translation in HTML (this is my choice, I have the code server-side, there is no implementation in javascript of the markup translation code).
When the asynchronous script runs (every 10 secs), the non-editable text-box is re-filled with the translated markup. The Latex code as well is put there (no translation performed on it) and MathJax script is called everytime the AJAX procedure returns (so the latex code will be rendered after the markup language, this latex rendering happens client-side).
The problem is that, sometimes, rendered math is strange especially considering equation numberings. Numbers do increase every new AJAX call. I guess there must be a way to reset, completely, the status of the MathJax object instantiated in the page or whatever... Is there a way to reset MathJax?
I assume that AJAX here is the problem... Thankyou for your help, hope I was clear.
If you have loaded a configuration file that includes the AMSmath extension, you can use
MathJax.InputJax.TeX.resetEquationNumbers()
to reset the equation numbering and the labels used for \ref and \eqref. If you want to start the numbering at a particular equation number, use
MathJax.InputJax.TeX.resetEquationNumbers(n)
where n is the starting number minus 1 (the default is 0).
Where I should escape output from model. Should I do it in Model too? I guess it should be in View, but i'm not sure. Put it into View seems logic, so you can change HTML to different output (PDF etc.).
Where do you escape HTML output (from users) to page?
With your clarification comment, the answer becomes simple; What you're asking about is presentation logic, which is handled solely in the View.
I have an ecommerce gift store where users can fill out a gift-card for their recipient.
In order to fill out the card, I have the users enter text into a multiline textbox.
When I display the gift-card on the final review page, I have to spit out the information with Html.Raw so that Newlines are being displayed properly. I'm doing this:
#(Model.GiftCard.Text != null ? Html.Raw(Model.GiftCard.Text.Replace(char.ConvertFromUtf32(13),"<br />")) : Html.Raw(""))
I'm frightened that i'm entering dangerous territory using Html.Raw on values that were user-entered. However, when I go back to the gift-card entry page, the page breaks when I try to do something like "This is my gift card! (scripttag)alert('test');(/scripttag)"... so I feel like .net will catch any malicious entries during that point.
Am I safe to proceed like this? It seems that since the gift-card entry page is running validations against malicious code, I should be okay to use HtmlRaw later to display newline html that I'm putting in myself...
(I replaced the actual script tag with this (scripttag) thing above so it will show in stackoverflow)
Use a regular expression in your view model to make sure people only enter A-Za-z0-9 and whatever else you think should use such as :) =] type of stuff. Screening this stuff front end is better than second guessing it on the way out.
How about using a
<pre></pre>
tag instead? This would allow returns to display in HTML without the need for Html.Raw?