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

4

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?

Retread answered 23/10, 2014 at 6:1 Comment(1)
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 way instead.Canvass
E
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>
Elope answered 23/10, 2014 at 7:5 Comment(3)
Hi Jesús María, I am not using MVC. Thanks in advanceRetread
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??.Retread
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.Ubiquitous
F
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.

Fewness answered 23/10, 2014 at 10:50 Comment(1)
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??.Retread
D
0

Try this:

Its Very Simple

<script type="text/javascript">
    var resources_en = {
        welcome_en : '<%= HttpContext.GetGlobalResourceObject("ResourceFileName", "yourMsg") %>';
    }
    alert(welcome_en);
</script>
Declivous answered 30/3, 2015 at 14:20 Comment(1)
This is what you do it is in line. The question was an external javascript file.Rajasthan
I
-1

Its Look Like This

 document.getElementById("btnAdd").value = '@Resources.Admin.AddAdmin';
Incorporeity answered 4/9, 2018 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.