- 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 can i create a website in IIS and bind a certificate from the store using c#?
- How to log the request url when an error occurs in asp net core?
- ASP . NET CORE - Identity _LoginPartial.cshtml how to use function from javascript
- How to correctly receive string in url format in ASP NET Core 3.1?
- ASP NET Core MVC how to display data from SQL table in List Razor Template by UserID foreign key
- How do I convert a byte array back to an image consumable by a view in asp net core?
- How do I convert a byte array back to an image consumable by a view in asp net core?
- How to set up default value for Null Type in Kendo UI asp net MVC
- How to resolve HttpPut Error 405 in ASP NET Core 3.1 MVC project?
- How to send large string from JavaScript to Asp Net Core
- How to create DBContext/Tenant Factory per user in Dot Net Core 2.2, JWT
- Asp Net Core web API - How to send image and JSON
- How to access to ConnectionString from IConfiguration injected to controller in asp net core?
- asp net core mvc create instance controller System.MissingMethodException: 'No parameterless constructor defined for this object.'
- How to connect to SQL Server 2008 with asp net core 2?
- How to get a Page from a class in asp website
- how to create privileges for users in a asp.net website
- how to create a ComboBox in website c#
- How to Post/Get and not wait for the response (Async like Ajax) in asp net
- how to host an ASP .NET 3.5 website to world wide web. like www.mynewwebsite.com
- How do I retrieve SSIS package logging from ASP NET after/during execution? (C#)
- How to create a new user using jabber net in XMPP server using C#?
- How to create dynamic multilingual web application using Asp.net 3.5 and c# 3.0
- How to set an error as return on controller and get it on uploadify in asp net mvc 4?
- How to pass image from a flex application to a asp net c# web service?
- How can I create a vs.net website project from existing files on a server?
- How can I add an info bubble to a mapxtreme map in asp net 2.0
- How to create new website in IIS using Web Setup Project in Visual Studio 2008 SP1
- How to create an extensible API, and still use object initializer syntax?
- How can I create a collection of business objects from multiple elements in a XmlDocument
- error in use application from servise chaining in wso2 esb
- Refactoring C# code for conversion to a WPF application (Visual Studio)
- How do I get each value from each table row in an MVC project?
- Get clicked MenuStrip Item in C#
- Matching listview header to column dynamically uwp
- How to create a simple glass effect
- iTextSharp pdf merge spool issue
- How change language Resources in api .net Core with appsettings?
- Check whether the value exists in the mongodb database using c# driver 2.4.4
- Create A treenode with List
- Merge two datatables?
- WPF C# Excel-like grid
- C# WinForm MenuStrip Event doesn't seem to fire
- Db4o select random objects
- Getting IDs back from an Oracle query using .net core and Oracle.ManagedDataAccess.Core
- Display an Expression in a human readable format
- About textbox events
- How to read ms access database file programmatically without installing ms office in c#
- Microsoft Azure Map – Weather Preview
- AutoFac inject based on controller