8

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?

Manu A N
  • 131
  • 1
  • 1
  • 5
  • You can't get access to .resx file without using say `Hidden Field` or a `JavaScript file` containing .resx contents which is generated in server side, try [this](https://stackoverflow.com/a/54386719/4344976) way instead. – Muhammad Musavi Jan 28 '19 at 07:02

4 Answers4

12

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>
  • Hi Jesús María, I am not using MVC. Thanks in advance – Manu A N Oct 23 '14 at 08:18
  • I want to access it from .js file itself. Because I have a number of alert messages in this script file. I want to show them in English/German according to the context. MyQuestion is that if I put these messages in resx files and by giving the name of the resx file and the 'Name' of the entry,can I retrieve the 'Value' in .js itself??. – Manu A N Oct 23 '14 at 12:14
  • The only trick I come up with is that you could generate that js from the server and send it to the client. Apart from that, I do not know any other way of accessing your resources from your js file. Sorry for not being of more help. – Jesús María Oct 24 '14 at 22:19
3

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.

Haseeb Asif
  • 1,766
  • 2
  • 23
  • 41
  • I want to access it from .js file itself. Because I have a number of alert messages in this script file. I want to show them in English/German according to the context. MyQuestion is that if I put these messages in resx files and by giving the name of the resx file and the 'Name' of the entry,can I retrieve the 'Value' in .js itself??. – Manu A N Oct 23 '14 at 12:14
0

Try this:

Its Very Simple

<script type="text/javascript">
    var resources_en = {
        welcome_en : '<%= HttpContext.GetGlobalResourceObject("ResourceFileName", "yourMsg") %>';
    }
    alert(welcome_en);
</script>
Suganth G
  • 5,136
  • 3
  • 25
  • 44
-1

Its Look Like This

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