Multi culture programming in Asp.Net

Asp.Net provide a very powerful multi culture programming pattern.In this post , we will discuss specially under the user control level.

Last weekend, one of my friends asked the question about the user control multi culture programming problem. He could not make it work at that time and I was in the middle of the NFlickr Release , so I promised to write a post about this problem.

First, I did not know what exactly the problem was, but I will try to use the standard way to implement it.

The user control is "Login.ascx" and the html markup is

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Login.ascx.cs"   Inherits="UserControl_Login"  %>
<asp:Label ID="lblUser" runat="server" Text="UserId"
meta:resourcekey="lblUserResource1" /> <asp:TextBox ID="txtUser"
runat="server" />
<br />
<asp:Label ID="lblPassword" runat="server" Text="UserId"
meta:resourcekey="lblPasswordResource1" /> <asp:TextBox ID="txtPassword"
TextMode="Password" runat="server" />
<br />
<asp:Button ID="cmdLogin" runat="server" Text="Login"
/>

Change to the design mode and select the menu tools->Generate the resource file. This tools will create the App_LocalResources folder and Login.ascx.resx file automatically. After this operation , the file content will be changed to

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Login.ascx.cs"   Inherits="UserControl_Login"  %>
<asp:Label ID="lblUser" runat="server" Text="UserId"
meta:resourcekey="lblUserResource1" /> <asp:TextBox ID="txtUser"
runat="server" meta:resourcekey="txtUserResource1" />
<br />
<asp:Label ID="lblPassword" runat="server" Text="UserId"
meta:resourcekey="lblPasswordResource1" /> <asp:TextBox ID="txtPassword"
TextMode="Password" runat="server" meta:resourcekey="txtPasswordResource1" />
<br />
<asp:Button ID="cmdLogin" runat="server" Text="Login"
meta:resourcekey="cmdLoginResource1" />

The trick is the meta:resourceKey , in the Login.ascx.resx file, there will be a list of keys which  will like cmdLoginResource1.Text,txtPasswordResource1.Text, etc.

If we want to support the Canada French, what we need to do is just create a french version of the resource, the name convention will be Login.ascx.fr-CA.resx

Now, we can go back to the default.aspx file which is the place holder for the user control

<%@ Page Language="C#" AutoEventWireup="true"   CodeFile="Default.aspx.cs"  Inherits="_Default" %>

<%@ Register src="UserControl/Login.ascx" tagname="Login" tagprefix="uc1" %>

<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<uc1:Login ID="Login1" runat="server" />
<asp:Label ID="lblMessage" runat="server" Text="Message"
/>
</div>
</form>
</body>
</html>

When we run this file , we will find that the multi-culture does not work!! Why?

By default, if we don't write any code and want to have the multi-culture feature, we need to enable the multi-culture for this page.We need to change the page register statement.

 

 

After we had this line of change, we will have the full set of multi-culture support without writing a line of code.

The source code can be downloaded at http://technetguy.com/Download/blog/UserControlCulture.rar

Published Sunday, November 16, 2008 1:08 AM by RobertNet
Filed under:

Comments

# Multi culture programming in Asp.Net - Technetguy.com/blog

Monday, November 17, 2008 1:59 AM by Multi culture programming in Asp.Net - Technetguy.com/blog

Pingback from  Multi culture programming in Asp.Net - Technetguy.com/blog

Leave a Comment

(required) 
(required) 
(optional)
(required)