Javascript 如何从 .resx 文件获取字符串到 .js 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26522133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:59:44  来源:igfitidea点击:

How to get a string from .resx file to a .js file

javascriptasp.netresxapp-globalresources

提问by Manu A N

I want to show an alert message in two different language. I am using asp.net App_GlobalResources. Can I use it inside my script.js file?

我想以两种不同的语言显示警报消息。我正在使用 asp.net App_GlobalResources。我可以在我的 script.js 文件中使用它吗?

回答by Jesús María

You can't call directly to resources in a RESX file from a javascript files.

您不能从 javascript 文件直接调用 RESX 文件中的资源。

However, you could create a partial view (in case you are using MVC) from which you can access all those resources. You could, thereafter, include that partial inside your pages.

但是,您可以创建一个局部视图(如果您使用的是 MVC),您可以从中访问所有这些资源。此后,您可以在页面中包含该部分内容。

For example, you could do a partial in which you would have:

例如,你可以做一个部分,你会:

<script>

var Resources = {

  Name: '@Resources.tags.Name',
  Surname: '@Resources.tags.Surname',

};

</script>

After this, you could include this page in the pages you need and access from javascript to these resources by using:

在此之后,您可以将此页面包含在您需要的页面中,并使用以下方法从 javascript 访问这些资源:

Resources.Name

If you are not using MVC, please let me know to look for the way it should be done in ASP.NET.

如果您不使用 MVC,请让我知道在 ASP.NET 中寻找它应该完成的方式。

If you have any doubt, please say.

如果您有任何疑问,请说。

For WebForms

对于 WebForms

In case you use WebForms, you coud make use of a user control, in which you will configure the javascript to be injected in the page.

如果您使用 WebForms,您可以使用用户控件,您将在其中配置要注入页面的 javascript。

Thereafter, include that user control in your page (preferably the master to make it available through all your website) and you will be able to access it.

此后,将该用户控件包含在您的页面中(最好是在您的所有网站上都可用的主控件),您就可以访问它了。

The user control would look this way:

用户控件看起来像这样:

public partial class resources : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LiteralControl jsResource = new LiteralControl();
        jsResource.Text = "<script type=\"text/javascript\">";

        jsResource.Text += "var Resources = {";

        jsResource.Text += "Name: '" + Resources.Resource.Name + "',";
        jsResource.Text += "Surname: '" + Resources.Resource.Surname + "',";
        jsResource.Text += "};";

        jsResource.Text += "</script>";
        Page.Header.Controls.Add(jsResource);

    }
}

Then you would include that control in your page:

然后您将在您的页面中包含该控件:

<uc1:resources runat="server" ID="resources" />

And you could simply access your javascript by doing so:

你可以通过这样做简单地访问你的javascript:

<script>
    alert(Resources.Name);
    alert(Resources.Surname);
</script>

回答by Haseeb Asif

A quick method is that You can set the Javascript variable values in aspx file in advance.

一个快速的方法是您可以提前在 aspx 文件中设置 Javascript 变量值。

<script type="text/javascript">
    var alertMessage = '<%=Resources.YourResourceFile.alertMessage%>';
    ...
    ...
    alert(alertMessage);
</script>

This will render the resource value in the alertMessage variable and you can use it wherever required.

这将在 alertMessage 变量中呈现资源值,您可以在任何需要的地方使用它。

--
You can access all the resource file variables on client side using this as well

--
您也可以使用它访问客户端的所有资源文件变量

<script type="text/javascript">
    var resources_en = {
        welcome_en : '<%= Resources.testResources_en.Welcome %>'
    }
    alert(welcome_en);
</script>

Add all the required resource variable to the resources_en to access them on client.

将所有必需的资源变量添加到 resources_en 以在客户端访问它们。

回答by Suganth G

Try this:

尝试这个:

Its Very Simple

它非常简单

<script type="text/javascript">
    var resources_en = {
        welcome_en : '<%= HttpContext.GetGlobalResourceObject("ResourceFileName", "yourMsg") %>';
    }
    alert(welcome_en);
</script>

回答by Esam Aldin Elzain

Its Look Like This

它看起来像这样

 document.getElementById("btnAdd").value = '@Resources.Admin.AddAdmin';