- Globalization and Localization in ASP.NET,
- Globalization and Localization in ASP.Net C# web applications with example.
A resource file is a XML file that contains Key (or you can say strings) that you want to translate into different languages.Basically you can say that resource file contain key/value pair like dictionary in c#.Resource files have an .resx extension.
- Open visual studio add new empty website
- File>New>Web Site then select “C#”(left side in visual studio) “Empty Web Site”
<%@ 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 selected culture in session because I want this session value in registration.aspx page for initializing the culture.
Do same thing for registration page
for changing submit button text we are using global resource so that registration page can also use submit key from global resource file.
so that we don’t have to put submit key in every local resource I make this key global key for all page.
<%@ 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 text of submit is in Italian which is coming from global resource file.
so if you want use any key in multiple pages then put this key in global resource file.
The post How to Create Multilingual Website in Asp .Net appeared first on Software Development | Programming Tutorials.
Read More Articles
- Write a value which contain comma to a CSV file in c#?
- Reading CSV File with cells containing commas c#
- Split CSV with columns may contain ‘,’ Comma C#
- [Simple Way]-Cascading DropDownList in Asp.Net Mvc Using Jquery Ajax
- [Simple Way]-How to get data from database using JQuery Ajax in asp net MVC
- [Simple Way]-ASP.NET Core Upload Multiple File Web API model
- [Simple Way]- Image Upload in .NET Core Web API
- [Easy Way]-Receive File and other form data together in ASP.NET Core Web API
- Replace image in word document using C#
- How to add new rows to an existing word document table in C#
- How to make all model properites nullable in asp net core api
- How to make IIS ignore one of two instances of ASP Net Core Host in the application?
- In Visual Studio 2022, using ASP NET Webforms how do I fix element is not a known element?
- How to use JWT token to authorize user from react to asp net core web api. When to use autorization header bearer token?
- How to add specific user to table when you have many in one List in asp net core mvc
- How to clear/reset all child controls within a usercontrol from code behind asp net c#
- How can i post both file and text data to web api using asp net core http client?
- How to get Textbox from <ItemTemplate> in Repeater ASP Net
- How to run Html and Url helpers when use Html.Raw - ASP NET MVC
- How do I block invalid json request parameters in asp net core 5 controller (for POST method)
- How to Use Rotativa Asp Net Core mvc in linux
- How to filter in mongodb asp net core?
- How to use ionic zip or dot net zip in asp classic
- How to return 404 by default in Asp Net Web Api
- how to fix the link to the pictures in asp net mvc
- How do i get a value from Literall Control ASP NET
- How to change two factor authentication code lifetime in Asp Net Core Idenity?
- how to create Multilevel dropdown using asp tags
- How to define an api adress for an asp net core application
- How to create drag-drop labels report in ASP .NET MVC
- Asp Net Core : How Can I Copy *.json Config/Content File of Dependent Project?
- Asp Net Core : How Can I Copy *.json Config/Content File of Dependent Project?
- how to apply CSSClass & style in while create asp button using StringBuilder in c#
- how to apply CSSClass & style in while create asp button using StringBuilder in c#
- how can use a custom method in controller in asp net mvc
- How to use repeater databound on ASP net for bootstrap carrousel slides
- how do i inject into some generic asp net http handler using unity
- c# asp net how to set enabled and visible properties of elements using code behind according to user permissions
- Azure mobile services custom authentication intergated with asp net website identity
- How to create sub domain in asp .net step step through coding.?
- Create an efficient logging library for logging socket communication
- How to return specified fields when use linq query in cosmos db sdk
- Reading two column data from a file
- How do I disable RadButton from server-side?
- SAP DI API update OBTW location field
- Maintaining Email headers to link back to database object
- Add pause to Alexa without using SSML
- Why does iOS link "static" to Unity?
- Cannot update tweet using twitterizer in c# (error: ssl required)
- How to pass something to Ajax.Complete jquery
- Value Not Binding In Edit Item Template Dropdown Field When Click Edit Button In Asp.net C#
- WCF Response is not mapping always returning null
- Excel VBA: compute an approximate interest rate of a french amortization schedule given: Starting Principal, Periodic Payment Amount, Nr Of Years
- Not seeing all data received through TCP
- Sendkeys is inconsistent in selenium webdriver
- C# Web scraper copying text
- ASP.NET Identity in wrong database
- C# MVC RouteBase routing too many redirects
- Where are application properties settings cached?
- SQLDataAdapter.Update() not Updating when RowState = Modified