Introduction: In
this post I will try to explain how we can create Multilanguage website in
asp.net.
Description:
In the last article i have explained Edit, Update and Delete in Gridview using three tier architecture in Asp.net using SqlHelper and How to Export Selected rows of Gridview to Excel, Word and PDF in Asp.net.
Take a new Website/Project. Now add a Resource File to
website/project (Right click on website >> Add New item>> Resource
File) as mention in attached snapshot:
When you added the Resource file to project App_GlobalResources
will be created. Here I add Three Resource File namely Home.de-DE.resx (German), Home.hi-IN.resx (Hindi) and Home.resx
(English).
Now added the value to Home.resx (English) respect to
language as mention below:
Home.de-DE.resx (German):
Home.hi-IN.resx (Hindi):
Note: Add the
Culture in web.config file as mention below:
<appSettings>
<add key="Culture" value=""></add>
</appSettings>
<table align="center"><tr><td><%=Homepage%></td></tr>
<tr><td><%=MultipleLang%></td></tr>
<tr><td><%=Indian %></td></tr>
<tr><td><table> <tr><td><asp:Button ID="btnhindi" runat="server" Text="Hindi" onclick="btnhindi_Click" /></td>
<td><asp:Button ID="btnenglish"
runat="server"
Text="English"
onclick="btnenglish_Click"/></td>
<td><asp:Button ID="btngerman"
runat="server"
Text="German"
onclick="btngerman_Click"
/></td></tr></table></td></tr>
</table>
After that on button click write the below mention code
(.aspx.cs):
using System.Globalization;
using System.Resources;
using System.Reflection;
using System.Threading;
using System.Configuration;
static string
CurrentCulture;
public string
Homepage;
ResourceManager rm;
CultureInfo ci;
protected void
Page_Load(object sender, EventArgs e)
{
CurrentCulture = ConfigurationManager.AppSettings["Culture"];
Thread.CurrentThread.CurrentCulture = new CultureInfo(CurrentCulture);
rm = new ResourceManager("Resources.Home", System.Reflection.Assembly.Load("App_GlobalResources"));
ci = Thread.CurrentThread.CurrentCulture;
if (!Page.IsPostBack)
{
if (CurrentCulture == "")
{
ConfigurationManager.AppSettings["Culture"] = "en-US";
CurrentCulture = ConfigurationManager.AppSettings["Culture"];
Thread.CurrentThread.CurrentCulture =
new CultureInfo(CurrentCulture);
rm = new ResourceManager("Resources.Home", System.Reflection.Assembly.Load("App_GlobalResources"));
ci = Thread.CurrentThread.CurrentCulture;
LoadString(ci);
}
else
{
CurrentCulture = ConfigurationManager.AppSettings["Culture"];
Thread.CurrentThread.CurrentCulture =
new CultureInfo(CurrentCulture);
rm = new ResourceManager("Resources.Home", System.Reflection.Assembly.Load("App_GlobalResources"));
ci = Thread.CurrentThread.CurrentCulture;
LoadString(ci);
}
}
}
private void
LoadString(CultureInfo ci)
{
Homepage = rm.GetString("Homepage",
ci);
}
protected void
btnhindi_Click(object sender, EventArgs e)
{
ConfigurationManager.AppSettings["Culture"] = "hi-IN";
string CurrentCulture = ConfigurationManager.AppSettings["Culture"];
Thread.CurrentThread.CurrentCulture = new CultureInfo(CurrentCulture);
rm = new ResourceManager("Resources.Home", System.Reflection.Assembly.Load("App_GlobalResources"));
ci = Thread.CurrentThread.CurrentCulture;
LoadString(ci);
}
protected void
btnenglish_Click(object sender, EventArgs e)
{
ConfigurationManager.AppSettings["Culture"] = "en-US";
string CurrentCulture = ConfigurationManager.AppSettings["Culture"];
Thread.CurrentThread.CurrentCulture = new CultureInfo(CurrentCulture);
rm = new ResourceManager("Resources.Home", System.Reflection.Assembly.Load("App_GlobalResources"));
ci = Thread.CurrentThread.CurrentCulture;
LoadString(ci);
}
protected void
btngerman_Click(object sender, EventArgs e)
{
ConfigurationManager.AppSettings["Culture"] = "de-DE";
string CurrentCulture = ConfigurationManager.AppSettings["Culture"];
Thread.CurrentThread.CurrentCulture = new CultureInfo(CurrentCulture);
rm = new ResourceManager("Resources.Home", System.Reflection.Assembly.Load("App_GlobalResources"));
ci = Thread.CurrentThread.CurrentCulture;
LoadString(ci);
}
In VB (.aspx.vb)
Imports System.Globalization
Imports System.Resources
Imports System.Reflection
Imports System.Configuration
Imports System.Threading
Shared CurrentCulture As String
Public Homepage As String
Public MultipleLang As
String
Public Indian As String
Private rm As ResourceManager
Private ci As CultureInfo
Protected Sub
Page_Load(sender As Object,
e As System.EventArgs)
Handles Me.Load
CurrentCulture = ConfigurationManager.AppSettings("Culture")
Thread.CurrentThread.CurrentCulture = New CultureInfo(CurrentCulture)
rm = New ResourceManager("Resources.Home", System.Reflection.Assembly.Load("App_GlobalResources"))
ci = Thread.CurrentThread.CurrentCulture
If Not
Page.IsPostBack Then
If CurrentCulture = ""
Then
ConfigurationManager.AppSettings("Culture") = "en-US"
CurrentCulture = ConfigurationManager.AppSettings("Culture")
Thread.CurrentThread.CurrentCulture =
New CultureInfo(CurrentCulture)
rm = New ResourceManager("Resources.Home", System.Reflection.Assembly.Load("App_GlobalResources"))
ci = Thread.CurrentThread.CurrentCulture
LoadString(ci)
Else
CurrentCulture = ConfigurationManager.AppSettings("Culture")
Thread.CurrentThread.CurrentCulture =
New CultureInfo(CurrentCulture)
rm = New ResourceManager("Resources.Home", System.Reflection.Assembly.Load("App_GlobalResources"))
ci = Thread.CurrentThread.CurrentCulture
LoadString(ci)
End If
End If
End Sub
Private Sub
LoadString(ci As CultureInfo)
Homepage = rm.GetString("Homepage",
ci)
MultipleLang = rm.GetString("MultipleLang",
ci)
Indian = rm.GetString("Indian",
ci)
End Sub
Protected Sub
btnhindi_Click(sender As Object, e As System.EventArgs) Handles
btnhindi.Click
ConfigurationManager.AppSettings("Culture") = "hi-IN"
Dim CurrentCulture As
String = ConfigurationManager.AppSettings("Culture")
Thread.CurrentThread.CurrentCulture = New CultureInfo(CurrentCulture)
rm = New ResourceManager("Resources.Home", System.Reflection.Assembly.Load("App_GlobalResources"))
ci = Thread.CurrentThread.CurrentCulture
LoadString(ci)
End Sub
Protected Sub
btnenglish_Click(sender As Object, e As System.EventArgs) Handles
btnenglish.Click
ConfigurationManager.AppSettings("Culture") = "en-US"
Dim CurrentCulture As
String = ConfigurationManager.AppSettings("Culture")
Thread.CurrentThread.CurrentCulture = New CultureInfo(CurrentCulture)
rm = New ResourceManager("Resources.Home", System.Reflection.Assembly.Load("App_GlobalResources"))
ci = Thread.CurrentThread.CurrentCulture
LoadString(ci)
End Sub
Protected Sub
btngerman_Click(sender As Object, e As System.EventArgs) Handles
btngerman.Click
ConfigurationManager.AppSettings("Culture") = "de-DE"
Dim
CurrentCulture As String
= ConfigurationManager.AppSettings("Culture")
Thread.CurrentThread.CurrentCulture = New CultureInfo(CurrentCulture)
rm = New ResourceManager("Resources.Home", System.Reflection.Assembly.Load("App_GlobalResources"))
ci = Thread.CurrentThread.CurrentCulture
LoadString(ci)
End Sub
Now bulid and run the project.
If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.
No comments:
Post a Comment