In this article I will explain with an example, how to use and access ASP.Net Global and Local Resource files (.resx) in JavaScript using C# and VB.Net.
With the help of a Base Class, the ASP.Net Render function will be overridden and the values of the Global and Local Resource files (.resx) will be embedded in JavaScript using C# and VB.Net.
 
 
Creating Base Class for performing Localization in JavaScript
First add a new class named BasePage.cs and place it in the App_Code folder of your ASP.Net website project.
Then you will need to override the Render function to call the Localize function. The Localize function will automatically fetch all the Resource keys placed with braces along with Localize keyword and replace it with its Localized string value from the respective Resource file.
C#
using System;
using System.Web;
using System.Web.UI;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
 
public class BasePage : Page
{
    protected override void Render(HtmlTextWriter writer)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        HtmlTextWriter hWriter = new HtmlTextWriter(sw);
        base.Render(hWriter);
        writer.Write(this.Localize(sb.ToString()));
    }
 
    private const string ResourceFileName = "Resource";
    private string Localize(string html)
    {
        MatchCollection matches = new Regex(@"Localize\(([^\))]*)\)", RegexOptions.Singleline | RegexOptions.Compiled).Matches(html);
        foreach (System.Text.RegularExpressions.Match match in matches)
        {
            html = html.Replace(match.Value, GetGlobalResourceObject(ResourceFileName, match.Groups[1].Value).ToString());
        }
        return html;
    }
}
 
VB.Net
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Text
Imports System.IO
Imports System.Text.RegularExpressions
Public Class BasePage
    Inherits System.Web.UI.Page
 
    Private Const ResourceFileName As String = "Resource"
 
    Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
        Dim sb As StringBuilder = New StringBuilder
        Dim sw As StringWriter = New StringWriter(sb)
        Dim hWriter As HtmlTextWriter = New HtmlTextWriter(sw)
        MyBase.Render(hWriter)
        writer.Write(Me.Localize(sb.ToString))
    End Sub
 
    Private Function Localize(ByVal html As String) As String
        Dim matches As MatchCollection = (New Regex("Localize\(([^\))]*)\)", (RegexOptions.Singleline Or RegexOptions.Compiled)).Matches(html))
        For Each match As System.Text.RegularExpressions.Match In matches
            html = html.Replace(match.Value, GetGlobalResourceObject(ResourceFileName, match.Groups(1).Value).ToString)
        Next
        Return html
    End Function
End Class
 
Note: The name of the resource file in my application is Resource, you need to replace the name of the Resource file where the line is marked in Yellow.
 
Once you are done with this you are now ready to use it. To make use of this Base class you need to inherit the same in the ASPX Page class.
C#
public partial class _Default : BasePage
 
VB.Net
Partial Class _Default
    Inherits BasePage
 
 
HTML Markup
The following HTML Markup consists of an HTML Button which calls the ShowGreetings JavaScript function when clicked.
Inside the ShowGreetings JavaScript function, the message variable has been assigned with Resource Key value.
This Resource key value will be replaced with the string value from the respective Resource value based on the selected Language.
The value of the variable message is displayed in JavaScript Alert Message Box.
<input id="btnGreetings" type="button" value="Show Greetings" onclick="ShowGreetings()" />
<script type="text/javascript">
        function ShowGreetings() {
            var message = 'Localize(Greetings)';
            alert(message);
        }
</script>
 
 
Resource file
The following is the Resource file for French language. The Resource Key value is wrapped inside the Localize method in your HTML page.
ASP.Net: Using Global and Local Resource files (.resx) in JavaScript using C# and VB.Net
 
 
Screenshots
ASP.Net: Using Global and Local Resource files (.resx) in JavaScript using C# and VB.Net
 
 
Downloads