How to get a string from .resx file to a .js file [closed]

强颜欢笑 提交于 2019-12-07 03:03:56

问题


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?


回答1:


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

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.

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:

Resources.Name

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

If you have any doubt, please say.

For 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.

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:

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



回答2:


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

<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.

--
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.




回答3:


Try this:

Its Very Simple

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



回答4:


Its Look Like This

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


来源:https://stackoverflow.com/questions/26522133/how-to-get-a-string-from-resx-file-to-a-js-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!