在 JavaScript 中显示来自 resources.resx 的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9190496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:52:56  来源:igfitidea点击:

Showing text from resources.resx in JavaScript

javascriptasp.net-mvc-3encoding

提问by stitch7c0

This is example code in ASP.NET MVC3 Razor:

这是ASP.NET MVC3 Razor 中的示例代码:

@section header
{
    <script type="text/javascript">    
        $(function() {
            alert('@Resources.ExampleCompany');
        });
    </script>
}

<div>
    <h1>@Resources.ExampleCompany</h1>
</div>

The code above this is just an example, but it also shows my problem with encoding. This variable @Resources.ExampleCompany is a file resources.resxwith value ExampleCompany = "Twoja firma / Twój biznes"

上面的代码只是一个例子,但它也显示了我的编码问题。这个变量@Resources.ExampleCompany 是一个带有值的文件resources.resxExampleCompany = "Twoja firma / Twój biznes"

In JavaScript, the alert shows the "Twoja firma / Tw&#243;j biznes".

在 JavaScript 中,警报显示“ Twoja firma / Tw&#243;j biznes”。

Why is character 'ó' '&#243'? What am I doing wrong?

为什么字符 'ó' 是 'ó'?我究竟做错了什么?

In HTML tag, <h1>@Resources.ExampleCompany</h1>is displayed correctly.

在 HTML 标记中,<h1>@Resources.ExampleCompany</h1>正确显示。

UPDATE:

更新:

Mark Schultheiss wrote a good hint and my "ugly solution" is:

Mark Schultheiss 写了一个很好的提示,我的“丑陋的解决方案”是:

var companySample = "@Resources.ExampleCompany";
$('#temp').append(companySample);
alert($('#temp').text());

Now the character is &#243;and looks good, but this is still not answer to my issue.

现在这个角色&#243;看起来不错,但这仍然不能解决我的问题。

回答by pete

According to HTML Encoding Strings - ASP.NET Web Forms VS Razor View Engine, the @syntax automatically HTML encodes and the solution is to use the Rawextension-method (e.g., @Html.Raw(Resources.ExampleCompany)) to decode the HTML. Try that and let us know if that works.

根据HTML 编码字符串 - ASP.NET Web 窗体 VS Razor 视图引擎@语法自动进行 HTML 编码,解决方案是使用Raw扩展方法(例如,@Html.Raw(Resources.ExampleCompany))来解码 HTML。试试看,让我们知道它是否有效。

回答by Mark Schultheiss

Some of this depends upon WHAT you do with the text.

其中一些取决于您对文本的处理方式。

For example, using the tags:

例如,使用标签:

<div id='result'>empty</div>

<div id='other'>other</div>

And code (since you are using jQuery):

和代码(因为您使用的是 jQuery):

var whatitis="Twoja firma / Tw&#243;j biznes";
var whatitisnow = unescape(whatitis);
alert(whatitis);
alert(whatitisnow);
$('#result').append(whatitis+" changed to:"+whatitisnow);
$('#other').text(whatitis+" changed to:"+whatitisnow);

In the browser, the "result" tag shows both correctly (as you desire) whereas the "other" shows it with the escaped character. And BOTH alerts show it with the escaped character.

在浏览器中,“result”标签正确显示(如您所愿),而“other”标签显示带有转义字符。并且两个警报都用转义字符显示它。

See here for example: http://jsfiddle.net/MarkSchultheiss/uJtw3/.

例如,请参见此处:http: //jsfiddle.net/MarkSchultheiss/uJtw3/

回答by Bronek

I use following trick:

我使用以下技巧:

<script type="text/javascript">
    $('<div/>').html("@Resources.ExampleCompany").text();
</script>

Maybe it will help.

也许它会有所帮助。

UPDATE

更新

I have tested this behavior of Razor more thoroughly and I've found that:

我已经更彻底地测试了 Razor 的这种行为,我发现:

1.When the text is put as normal content of html then @Html.Raw method simply helps and writes char 'ó' without html encoding (not as: &#243;)

1.当文本作为html的正常内容放置时,@Html.Raw方法只是帮助并写入没有html编码的char 'ó'(而不是:ó)

example:

例子:

<div> @Html.Raw("ó") </div>

example:

例子:

<script type="text/javascript">
    var a = $('<div/>').html('@("ó")').text();// or var a =  '@Html.Raw("ó")';
    console.log(a); // it shows: ó
</script>

2.But if it is put inside html tags as attribute then Razor converts it to: &#243; and @Html.Raw doesn't help at all

2.但如果它作为属性放在 html 标签中,那么 Razor 会将其转换为: ó 而@Html.Raw 根本没有帮助

example:

例子:

<meta name="description" content="@("ó")" />

Yo can fix it by putting the entire tag to Resource (as in that post) or to string (as in my example)

你可以通过将整个标签放到 Resource (如该帖子中)或字符串(如我的示例中)来修复它

@("<meta name="description" content="ó" />")

So, sometimes somebody could have been little confused that the answers helps the others but not him.

因此,有时有人可能会感到困惑,因为答案对其他人有帮助,但对他没有帮助。

回答by Otishone

I had similar issue, but in my case I was assigning a value from Resource to javascript variable. There was the same problem with letter óencoding. Afterwards this variable was binded to a html object (precisely speaking by knockout binding). In my situation below code give a trick:

我有类似的问题,但在我的情况下,我将一个值从 Resource 分配给 javascript 变量。字母ó编码也存在同样的问题。之后这个变量被绑定到一个 html 对象(准确地说是通过淘汰赛绑定)。在我的情况下,下面的代码给出了一个技巧:

var label = '@Html.Raw(Resource.ResourceName)';