如何在 javascript 中使用 .net 资源文件

发布于 2024-09-24 14:36:21 字数 91 浏览 0 评论 0原文

我是否可以在 javascript 中访问我的资源文件 (.resx)?
如果没有,那么是否有任何解决方法可以用不同的语言在 javascript 中显示消息?

is there anyway i can access my resource files (.resx) in javascript?
if no then are there any workarounds to diplay messages in javascript in different languages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑着哭最痛 2024-10-01 14:36:21

如果您的 javascript 位于页面中,您可以使用:

var globalResource = '<%= Resources.YourClass.YourResourceKey %>';

访问全局资源 (/App_GlobalResources)。或者

var localResource = '<%= GetLocalResourceObject("LocalResourceKey").ToString() %>';

访问本地资源(/App_LocalResources 相对于页面位置)。

或者 JavaScript 解决方案可能是 这个 不错的 jQuery 插件。

If your javascript is in the page you can use:

var globalResource = '<%= Resources.YourClass.YourResourceKey %>';

to access the Global Resources (/App_GlobalResources). Or

var localResource = '<%= GetLocalResourceObject("LocalResourceKey").ToString() %>';

to access the Local resources (/App_LocalResources relative to the page position).

Or a javascript solution may be this nice jQuery plugin.

听不够的曲调 2024-10-01 14:36:21

我通过使用 Jquery ajax 来做到这一点,它调用服务器端 GetGlobalResourceObject()

i did it by using Jquery ajax which calls a serverside GetGlobalResourceObject()

丑丑阿 2024-10-01 14:36:21

您可以在 MVC4 中使用双引号。

var e = document.getElementById("myelementID");
e.textContent = "@Resource.MyVariable";

我测试过,它对我有用。

You can just use double quotes in MVC4.

var e = document.getElementById("myelementID");
e.textContent = "@Resource.MyVariable";

I tested and it worked for me.

起风了 2024-10-01 14:36:21

它是一个单独的javascript文件吗?您始终可以使用 asp 文字将文本插入 html 页面(例如:var myString = '';)并填充文字服务器端。

Is it a separate javascript file? You can always use an asp literal to insert text into your html page (for exemple: var myString = '<asp:literal id="myLiteral" />';) and fill the literal serverside.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文