In this article I will explain explained how to access ASP.Net Global Resource file in JavaScript, thus implementing Globalization in JavaScript strings.
For this you first need to add a new class to your ASP.Net Website or Web Application. Name it as BaseClass, this class will be inherited on all the pages where you want to Localize JavaScript strings
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 BasePageVB
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 need to inherit the same in the ASPX Page class.
C#
public partial class _Default : BasePage
VB.Net
Partial Class _Default
Inherits BasePageVB
That’s all you need to do on each page. Now let’s test how it works.
Below is a simple JavaScript function which displays a greeting message in alert.
<input id="btnGreetings" type="button" value="Show Greetings" onclick = "ShowGreetings()" />
<script type = "text/javascript">
function ShowGreetings() {
var message = 'Localize(Greetings)';
alert(message);
}
</script>
Above you will notice the string in Yellow. You will need to place your Resource Key wrapped inside the Localize method as I have done above. In this case the key name is Greetings as shown in the figure below.
After you run the application in the HTML source of the page you will notice that string Localize(Greetings) has been replaced with its localized string value as show in the figure below
The source code is available in VB.Net and C#. You can download it using the link below.
Download Code