如何从Resx中获取文本以在Javascript中使用? [英] How to get texts from Resx to be used in Javascript?

查看:107
本文介绍了如何从Resx中获取文本以在Javascript中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在构建大型ASP.NET应用程序,以便在多种语言/文化中使用Intranet。我们利用带有RESX文件的全球化,并在服务器端使用 GetResourceText 来获取本地化文本。

We are building large ASP.NET applications for the intranet use in multiple languages/cultures. We utilize the Globalization with RESX files and use GetResourceText on the server side to get the localized texts.

最近我们做了越来越多的客户使用JQuery的边逻辑。

Lately we are doing more and more client side logic with JQuery.

如何在Javascript中使用RESX文本?


  • 例如用于验证的文本,动态消息等。

我们所有的Javascripts都在.JS文件中,我们不希望在ASPX页面和Javascript块。

All our Javascripts are in .JS files, we do not want to mix HTML in the ASPX page and Javascript blocks.

感谢您的帮助。

推荐答案

不幸的是,在外部JS文件中,服务器未处理服务器端代码。但是我看到了一种解决方法,您可以在页面上的隐藏字段中设置翻译的值 - 这样您的javascript就能读取其中的值。

Unfortunately, in an external JS file the server side code is not being processed by the server. However I have seen a workaround where you can set your translated values in hidden fields on the page - this way your javascript will be able to read the values in.

例如:

 <%-- This goes into your page --%>
 <input type="hidden" id="translatedField" name="translatedField" value="<%=Resources.Resources.translatedText %>" />

并在你的javascript文件中使用:

and use this inside your javascript file:

 // This is the js file
 $(document).ready(function() {
  alert($("#translatedField").attr("value"));
 });

您将能够分离这些值,并仍然可以在外部JS文件中看到它。

You will be able to separate the values and still see it in your external JS file.

还有另一种解决方法是创建一个只输出Javascript而不是HTML的.aspx文件。请查看以下链接:

There is also another workaround that creates a .aspx file that only outputs Javascript instead of HTML. Check out the link below:

在外部JavaScript文件中使用服务器端方法

这篇关于如何从Resx中获取文本以在Javascript中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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