Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 454
Manu A N Avatar asked Oct 23 '14 06:10

Manu A N


2 Answers

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>
like image 167
Jesús María Avatar answered Nov 07 '22 17:11

Jesús María


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.

like image 42
Haseeb Asif Avatar answered Nov 07 '22 18:11

Haseeb Asif