ASP.Net MVC: Localized Texts with a New Line?

I have an Asp.Net MVC 3 site that will be localized.

I have several resx files that contain my texts, and I have some in my views

@ My.NameSpace.Through.My.LocalizationFile.Key

But I can not get it to represent a new line.

I tried:

  • Shift + enter: I have a new line in the resource file, but not in my browser
  • \ r \ n: I see \ r \ n in my browser
  • \ n: The same
  • <br/> : I see <br/> in my text

So what should I do to have a new line?

Change I know that I can use Html.Raw, but I just can’t ask the translators to put the html code in their translation.

+7
source share
5 answers

Honestly, I know that this is not the most pleasant thing in the world, but it is impeccable, and this means that your translators do not need to enter any code in their translations:

Based on the answers already received, why don’t you just use Html.Raw , but before you do this, replace \r\n , which uses Shift + Enter in the resource file, with <br />

So say, for example, you had a line called Welcome in the ApplicationMessage resource file, you could do:

 @Html.Raw(ApplicationMessage.Welcome.Replace("\r\n", "<br />") 

This will give you what you need. Here's a similar question:

HTMLencode HTMLdecode

+10
source

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

+2
source

I think you should use the Shift + Enter combination and the CSS white space property instead of potentially opening up to XSS, since you would use the @Html.Raw() solution.

 <span style="white-space: pre-line">@My.NameSpace.Through.My.LocalizationFile.Key</span> 

I don’t know the specific case, but you may find that pre-wrap better for your business. Read more about the various properties of white space here.

+1
source

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

0
source

Yes, thanks mattytommo.

you can use

 First line <br /> second line 

in the resource or

 resource.Replace("\r\n", "<br />") 

in code and Shift + Enter in the resource editor.

Both work fine, but you should use

 @Html.Raw(); 
-one
source

All Articles