|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionWhile seeking on the internet for a solution to implement localization within an ASP.NET application using a BackgroundThe solution presented within this article uses the standard localization mechanism of the .NET framework. Using the codeThe published solution uses the If a culture change is requested by the user, the In a Let's start with the Global.asax file: void Session_Start(object sender, EventArgs e)
{
//set english as default startup language
Session["MyCulture"] = "en-GB";
}
Alternatively, the culture can be defined in the Web.config file with the key The next step is the master page: <%@ Master Language="C#" AutoEventWireup="true"
CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>[Smart-Soft - Masterpage with Localization Support]</title>
</head>
<body>
<form id="theForm" runat="server">
<div>
<asp:contentplaceholder id="ContentPlaceHolder" runat="server">
</asp:contentplaceholder>
</div>
<div style="margin-top:20px;">
<asp:LinkButton ID="btnSetGerman" runat="server" Text="Deutsch"
CommandArgument="de-CH" OnClick="RequestLanguageChange_Click">
</asp:LinkButton>
<asp:LinkButton ID="btnSetEnglish" runat="server" Text="English"
CommandArgument="en-GB" OnClick="RequestLanguageChange_Click">
</asp:LinkButton>
</div>
</form>
</body>
</html>
The buttons to change the culture can be either placed in the ..And the code-behind of the master page: public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void RequestLanguageChange_Click(object sender, EventArgs e)
{
LinkButton senderLink = sender as LinkButton;
//store requested language as new culture in the session
Session["MyCulture"] = senderLink.CommandArgument;
//reload last requested page with new culture
Server.Transfer(Request.Path);
}
}
The requested language, passed within the Last but not least, the /// <summary>
/// Custom base page used for all web forms.
/// </summary>
public class BasePage : Page
{
private const string m_DefaultCulture = "en-GB";
protected override void InitializeCulture()
{
//retrieve culture information from session
string culture = Convert.ToString(Session["MyCulture"]);
//check whether a culture is stored in the session
if (!string.IsNullOrEmpty(culture)) Culture = culture;
else Culture = m_DefaultCulture;
//set culture to current thread
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
//call base class
base.InitializeCulture();
}
}
As mentioned above, the Remark: In this article, only the culture was mentioned. Of course, there is also the UI culture. But it is not of any further interest in this article since the handling is absolutely identical. For more information, please see the MSDN pages. :) For a running example, download the Zip file above. History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||