In this Post I will Explain you following Points:-
- Globalization and Localization in ASP.NET
- Globalization and Localization are essential aspects of developing ASP.NET C# web applications, particularly when aiming to support multiple languages and cultures. Even if your website is initially designed for a single language and culture, you can easily extend its support for different linguistic preferences using Globalization and Localization concepts in ASP.NET.
- Globalization enables your web application to display its content based on the language preference set by the user in their browser. With ASP.NET, achieving Globalization and Localization is straightforward through the use of resource files.
- Resource Files:
- A resource file, typically in XML format, contains key-value pairs that you wish to translate into different languages. Think of it as a dictionary in C#. These resource files usually have a .resx extension.
- In ASP.NET, there are two types of resource files:
- 1. Global Resource:
- You can create a global resource file by placing it in the reserved folder "App_GlobalResources" at the root of your web application. Any .resx file in this folder has global scope, meaning you can utilize its resources on any page within the web application.
- 2. Local Resource:
- Localization involves translating specific pages into different languages and cultures. Local resource files are specific to particular pages only, typically ASP.NET files with extensions such as .aspx, .ascx, or .master.
- Each page in your web application can have its own local resource file. These files are placed in the "App_LocalResources" folder.
- To implement Globalization and Localization:
- 1. Open Visual Studio.
- 2. Create a new empty website: File > New > Web Site.
- 3. Select "C#" from the left side of Visual Studio.
- 4. Choose "Empty Web Site."
- By following these steps and understanding the concepts of Globalization and Localization, you can enhance the reach and usability of your ASP.NET web applications across different languages and cultures.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style2">Language</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>Select Language</asp:ListItem>
<asp:ListItem Value="da-dk">Denis</asp:ListItem>
<asp:ListItem Value="it-IT">Italian</asp:ListItem>
<asp:ListItem Value="en-us">English</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label1" runat="server" Font-Size="Larger" ForeColor="#003399" Text="Login Panel" meta:resourcekey="LoginPanel"></asp:Label>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label2" runat="server" Text="Username" meta:resourcekey="password"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label3" runat="server" Text="Password"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<asp:Button ID="Button1" runat="server" Text="<%$Resources:Resource,submit%>" />
<asp:HyperLink ID="HyperLink1" runat="server">Registration</asp:HyperLink>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td> </td>
</tr>
</table>
</div>
</form>
</body>
</html>
Now we need to override the InitializeCulture function and set the UICulture to the user selected language.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Login1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
InitializeCulture();
}
protected override void InitializeCulture()
{
if (Request.Form["DropDownList1"] != null)
{
UICulture = Request.Form["DropDownList1"];
Session["culture"] = Request.Form["DropDownList1"]; //set the UICulture to the user selected language.
}
base.InitializeCulture();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
InitializeCulture();
}
}
I have stored the selected culture in a session because I need this session value in the 'Registration.aspx' page to initialize the culture.
I will do the same thing for the registration page.
For changing the text of the submit button, we are using a global resource so that the registration page can also use the 'Submit' key from the global resource file. This way, we don’t have to include the 'Submit' key in every local resource; I've made this key a global key for all pages.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Registration.aspx.cs" Inherits="Registration1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Font-Size="Larger" ForeColor="#0099FF" Text="Register" ></asp:Label>
</td>
<td> </td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="name" meta:resourcekey="Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="lastname" meta:resourcekey="lastname"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="password" meta:resourcekey="Password"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Submit" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Registration1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
InitializeCulture();
}
protected override void InitializeCulture()
{
if (Session["culture"].ToString()!= null)
{
UICulture = Session["culture"].ToString(); //set the UICulture to the user selected language from session
}
base.InitializeCulture();
}
}
Now run your application and choose language from drop down
Now click on Registration link
You can see that the text of the submit button is in Italian, which is coming from the global resource file. Therefore, if you want to use any key in multiple pages, it's advisable to put this key in the global resource file.
Read Similar Articles
- Convert JSON Data To HTML Table Using JavaScript
- INNER JOIN,RIGHT JOIN,LEFT JOIN USING LINQ & SQL IN Multiple Table C#
- [Fixed] The name 'InitializeComponent' does not exist in the current context
- Solved Error: 'SqlQuery' method is not natively supported by your database provider. Either use a supported element type
- [Fixed] FileSystemException: Cannot retrieve length of file, path ='...' (OS Error: No such file or directory, errno = 2)
- What is "Transaction" in Sql Server with example