简体   繁体   English

如何在JavaScript中访问嵌入式ASP.NET GlobalResources?

[英]How can I access embedded ASP.NET GlobalResources in JavaScript?

I'm working on a legacy ASP.NET project, which I'm trying to massage into shape slowly, but I can't make significant changes without it all collapsing like a chocolate finger house.... 我正在研究一个遗留的ASP.NET项目,我试图慢慢地按摩它,但如果不像巧克力手指房子那样崩溃,我就无法做出重大改变....

I've tried to find a solution for this, but failed miserably due to the specific mix of terminology ("javascript", "embedded" and/or "resource" just give me information as to how to embed a .js file...), and the fact that it's probably a weird way of doing things. 我试图找到一个解决方案,但由于术语的具体组合(“javascript”,“嵌入式”和/或“资源”只是给我关于如何嵌入.js文件的信息,因此失败了。 。),以及它可能是一种奇怪的做事方式。

The project makes use of App_GlobalResources for translatable strings, which is used in C# code behind, the markup and in some of the JavaScript. 该项目利用App_GlobalResources进行可翻译的字符串,后者用于C#代码,标记和一些JavaScript中。 For example: 例如:

HTML: HTML:

Text="<%$Resources: Resources, MeasuresLabel %>"

JS: JS:

setDialogTitle('summaryDialog', physicalElementName, Resources.Resources.Summary);

This was all working fine, until we started using NUnit for some integration testing. 这一切都运行正常,直到我们开始使用NUnit进行一些集成测试。 To make the Resources.resx available to NUnit I've followed Scott Allen's suggestion , which works for NUnit but means that the above Resources.Resources is undefined. 为了使Resources.resx可用于NUnit,我遵循了Scott Allen的建议 ,该建议适用于NUnit,但意味着上面的Resources.Resources未定义。

So, my question is with this embedded file, how can I make it accessible to the JavaScript (or is there a better way I can make it available to NUnit)? 所以,我的问题是这个嵌入式文件,我如何让它可以访问JavaScript(或者有更好的方法让我可以使用NUnit)?

There's an the added complication due to the code I want to access it with being in separate .js files, rather than within script tags in the .aspx files. 由于我希望通过在单独的.js文件中访问它而不是在.aspx文件中的脚本标记内访问它,因此会增加复杂性。

EDIT After looking at it some more, the real challenge is the properties that are accessed in the JS are static. 编辑在看了一些之后,真正的挑战是在JS中访问的属性是静态的。 Due to the number of places these are referenced, it's not practical to add a variable for each string that's being used. 由于引用这些位置的数量,为每个正在使用的字符串添加变量是不实际的。

I think that JS files aren't going through the ASP.NET engine and they are served as they are, that's why Resources.Resources.Summary isn't defined in your case, while it will work when embedded inside <script> tag in .aspx file. 我认为JS文件没有通过ASP.NET引擎,它们按原样提供,这就是为什么在你的情况下没有定义Resources.Resources.Summary,而它嵌入在<script>标签中时可以工作.aspx文件。 The only way I know around this would be to store the used resource values in global javascript variables on the .aspx page and then use them in loaded JS files. 我知道的唯一方法是将已使用的资源值存储在.aspx页面上的全局javascript变量中,然后在加载的JS文件中使用它们。

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

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