从JavaScript文件中使用ASP.NET资源字符串 [英] Use ASP.NET Resource strings from within javascript files

查看:126
本文介绍了从JavaScript文件中使用ASP.NET资源字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该如何获得RESX资源字符串转换为JavaScript code存储在一个.js文件?

How would one get resx resource strings into javascript code stored in a .js file?

如果您的JavaScript是在标记脚本块,你可以使用这个语法:

If your javascript is in a script block in the markup, you can use this syntax:

<%$Resources:Resource, FieldName %>

和它将解析中,因为它呈现页面...不幸的是,资源的值,如果该JavaScript的出现在页面的主体,将只进行解析。在A&LT引用的外部.js文件;脚本&GT;标签,这些标签的服务器显然永远不会被解析。

and it will parse the resource value in as it renders the page... Unfortunately, that will only be parsed if the javascript appears in the body of the page. In an external .js file referenced in a <script> tag, those server tags obviously never get parsed.

我不希望有写一个ScriptService返回这些资源或类似的东西,因为它们不改变页面之后被渲染所以这是有东西废物活动的。

I don't want to have to write a ScriptService to return those resources or anything like that, since they don't change after the page is rendered so it's a waste to have something that active.

一种可能是写一个ASHX处理程序,并指出在&lt;&脚本GT;标签,但我真不知道我怎么会在.js文件读取和流文本到客户端之前解析喜欢的任何服务器的标签。有code的一条线,我可以运行,将做这个任务类似于ASP.NET解析器?

One possibility could be to write an ashx handler and point the <script> tags to that, but I'm still not sure how I would read in the .js files and parse any server tags like that before streaming the text to the client. Is there a line of code I can run that will do that task similarly to the ASP.NET parser?

或任何人有任何其他建议?

Or does anyone have any other suggestions?

推荐答案

有对这个没有原生支持。

There's no native support for this.

我建了一个JavaScriptResourceHandler前一阵子,可以通过在那里的对象重新$ P $每个属性psents本地化资源ID和它的值对象服务Serverside集团的资源投入到客户端页面。你可以看看这个和这个博客帖子下载:

I built a JavaScriptResourceHandler a while ago that can serve Serverside resources into the client page via objects where each property on the object represents a localization resource id and its value. You can check this out and download it from this blog post:

<一个href=\"http://www.west-wind.com/Weblog/posts/698097.aspx\">http://www.west-wind.com/Weblog/posts/698097.aspx

我已经在许多应用中广泛使用这个和它工作得很好。在这个主胜是,你可以在一个地方(RESX或在我的情况定制ResourceProvider使用数据库),而不是有多个本地化方案本地化资源。

I've been using this extensively in a number of apps and it works well. The main win on this is that you can localize your resources in one place (Resx or in my case a custom ResourceProvider using a database) rather than having to have multiple localization schemes.

这篇关于从JavaScript文件中使用ASP.NET资源字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆