简体   繁体   English

asp.net:资源 .resx 文件中的换行符 \n 不起作用

[英]asp.net: line break \n in resource .resx file does not work

The "\n" does not work in below code, it's just displayed as "\n" “\n”在下面的代码中不起作用,它只是显示为“\n”

if (!window.confirm(XXXXX.Globalization.LocalResource.InvalidUrl)) {
    return false;
}

the string is "Invalid URL! \n Are you sure you want to continue?"字符串是“无效的 URL!\n 你确定要继续吗?” in resource resx file.在资源 resx 文件中。

how to make it work?如何使它工作?

What worked in my case was this:在我的情况下起作用的是:

This text has a<br>line break!

See the <br> there.见那里的<br>

Look here for the details: Putting a line break in an resx resource file在此处查看详细信息: 在 resx 资源文件中放置换行符

I'm using Razor in ASP.NET MVC and so I used:我在 ASP.NET MVC 中使用 Razor,所以我使用了:

@Html.Raw(Localization.NotAnswered)

so that it outputs the <br> as HTML code.以便将<br>输出为 HTML 代码。

Try shift-enter to create a new line in the designer.尝试 shift-enter 在设计器中创建一个新行。 Alternatively copy and paste the result from notepad.或者从记事本复制并粘贴结果。

Another way is to edit the RESX xml manually.另一种方法是手动编辑 RESX xml。

The problem is that the RESX designer UI will auto-escape the strings.问题是 RESX 设计器 UI 将自动转义字符串。 So your value is actually:所以你的价值实际上是:

"Invalid URL! \\n Are you sure you want to continue?"

Use break tag "<br/>" instead of "\\n" 使用break标记"<br/>"而不是"\\n"

EDITED EDITED

This should surely work 这肯定会奏效

use "\r\n"

I used shift+enter, it think it's the simplest.我用shift+enter,它认为这是最简单的。

< br >certainly will not work, have not tried \r\n, but since \n does not work, I am not very confident with \r\n < br >肯定不行,没试过\r\n,但是由于\n不行,我对\r\n不是很有信心

ASP.NET MVC ASP.NET MVC

STEP-1步骤1
In my Resource.resx file put Shift+Enter between the paragraph1 & paragraph2在我的Resource.resx文件中,在第 1 段和第 2 段之间放置Shift+Enter
My text paragraph1我的文字段落1
My text paragraph2我的文字段落2

STEP-2第2步
In my view page its在我的视图页面中

<p>@Html.Raw(Regex.Replace(Resource.Mytext, "\r\n", "<br />"))</p>

Short answer: use &lt;br&gt;简短回答:使用&lt;br&gt; if editing raw file or use <br> if editing in IDE.如果编辑原始文件或使用<br>如果在 IDE 中编辑。

Details: If you create line breaks by just pressing Enter or Shift+Enter then the line breaks will not show up on a web page.详细信息:如果您只通过按 Enter 或 Shift+Enter 创建换行符,则换行符将不会显示在网页上。 Going back to HTML basics, if you want the browser to render line breaks then you must use a <br> tag.回到 HTML 基础,如果您希望浏览器呈现换行符,那么您必须使用<br>标签。 For me I was editing my resx file using a simple text editor, and the problem I encountered when adding a <br> tag is that when I later tried to open the file in Visual Studio it threw an error about " the closing tag doesn't match ".对我来说,我正在使用一个简单的文本编辑器编辑我的 resx 文件,我在添加<br>标签时遇到的问题是,当我后来尝试在 Visual Studio 中打开该文件时,它抛出了一个关于“关闭标签没有”的错误不匹配“。 It turns out that resx files don't allow you to use a <br> tag.事实证明 resx 文件不允许您使用<br>标记。 You need to actually use &lt;br&gt;你需要实际使用&lt;br&gt; . . In fact, if you're editing the file in Visual Studio and you enter <br> in the value field, it will automatically convert <br> to &lt;br&gt;实际上,如果您在 Visual Studio 中编辑文件并在值字段中输入<br> ,它会自动将<br>转换为&lt;br&gt; . . You won't see it in the IDE, but if you enter <br> in the IDE, save it, then open the raw file in Notepad++ you will see the actual value is &lt;br&gt;你不会在 IDE 中看到它,但是如果你在 IDE 中输入<br> ,保存它,然后在 Notepad++ 中打开原始文件,你会看到实际值是&lt;br&gt; . .

当您输入字符串时,Shift-enter 效果很好,但是如果您已经创建了资源文件,并且您不想编辑每个字符串,您可以这样做

String myStringFromResx = myStringFromResx.Replace("\\n," "\n");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM