简体   繁体   English

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

[英]Showing text from resources.resx in JavaScript

This is example code in ASP.NET MVC 3 Razor : 这是ASP.NET MVC 3 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.resx with value ExampleCompany = "Twoja firma / Twój biznes" 这个变量@ Resources.ExampleCompany是一个文件resources.resx ,其值为ExampleCompany = "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'? 为什么角色'ó''&#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; 现在角色是&#243; and looks good, but this is still not answer to my issue. 并且看起来不错,但这仍然不能解决我的问题。

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 Raw extension-method (eg, @Html.Raw(Resources.ExampleCompany) ) to decode the HTML. 根据HTML Encoding Strings - ASP.NET Web Forms VS Razor View Engine@语法自动进行HTML编码,解决方案是使用Raw扩展方法(例如, @Html.Raw(Resources.ExampleCompany) )来解码HTML 。 Try that and let us know if that works. 试试看,如果有效,请告诉我们。

I had similar issue, but in my case I was assigning a value from Resource to javascript variable. 我有类似的问题,但在我的情况下,我正在从Resource分配一个值到javascript变量。 There was the same problem with letter ó encoding. 字母ó编码存在同样的问题。 Afterwards this variable was binded to a html object (precisely speaking by knockout binding). 之后,这个变量被绑定到一个html对象(正好通过敲除绑定)。 In my situation below code give a trick: 在我的情况下,代码提供了一个技巧:

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

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. 在浏览器中,“result”标签正确显示(如您所愿),而“other”则显示转义字符。 And BOTH alerts show it with the escaped character. 并且两个警报都会显示转义字符。

See here for example: http://jsfiddle.net/MarkSchultheiss/uJtw3/ . 请参见此处: http//jsfiddle.net/MarkSchultheiss/uJtw3/

I use following trick: 我使用以下技巧:

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

Maybe it will help. 也许它会有所帮助。

UPDATE 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'ó'(不是:&#243;)

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; 但是如果将它作为属性放在html标签内,那么Razor会将其转换为:&#243; and @Html.Raw doesn't help at all 和@ 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) Yo可以通过将整个标记放到Resource(如该帖子中)或字符串(如我的示例中)来修复它

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

So, sometimes somebody could have been little confused that the answers helps the others but not him. 所以,有时候有些人可能会对答案帮助其他人而不是他而感到困惑。

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

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