Website for Multi Language Support
How to make website for multiple languages?
A website which can support Multiple Languages
and change according to the culture of a specific geographical location
is not a new topic. Websites to support multiple languages can be a
challenging and time-consuming process. You find many artilce related on
internet and well explained, But i still see that many of them are
complicated for "Beginners". I tried to make this article to approach a
more dynamic way to manage languages regarding Globalization/ Localization/ Culture concepts.
With standard HTML pages, you may have to create and maintain duplicate versions of each page for each supported language as well as having the language content embedded into the HTML, where content can’t easily be edited. For those of you who have to develop multi-lingual interfaces and applications, you’ll be glad to know that ASP.NET makes things easier.ASP.NET and the .NET framework ship with support for multilingual applications, namely in the form of Resource Files, the CultureInfo class, and the System.Globalization and System.Resources.ResourceManager namespaces.
However, we need to understand what is meant by "globalization", "localization" and "culture" in this context.
Globalization:
Globalization is a process of identifying all the parts of your application that need to be different for respective languages and separate them from the core application.
Localization:
Localization is process of creating and configuring your application for a specific language.
Cultures:
A culture is combination of the language that you speak and the geographical location you belong to. It also includes the way you represent dates, times and currencies. It's important to have a good understanding of Cultures since our new code will make use of them - specifically the System.Globalization.CultureInfo class, and the culture name value which follows the RFC 1766 naming standard. Basically, you create a new CultureInfo instance by specifying the culture name in the constructor:
CultureInfo c = new CultureInfo("en-US");
Follow the step to create website for multiple languages:
With standard HTML pages, you may have to create and maintain duplicate versions of each page for each supported language as well as having the language content embedded into the HTML, where content can’t easily be edited. For those of you who have to develop multi-lingual interfaces and applications, you’ll be glad to know that ASP.NET makes things easier.ASP.NET and the .NET framework ship with support for multilingual applications, namely in the form of Resource Files, the CultureInfo class, and the System.Globalization and System.Resources.ResourceManager namespaces.
However, we need to understand what is meant by "globalization", "localization" and "culture" in this context.
Globalization:
Globalization is a process of identifying all the parts of your application that need to be different for respective languages and separate them from the core application.
Localization:
Localization is process of creating and configuring your application for a specific language.
Cultures:
A culture is combination of the language that you speak and the geographical location you belong to. It also includes the way you represent dates, times and currencies. It's important to have a good understanding of Cultures since our new code will make use of them - specifically the System.Globalization.CultureInfo class, and the culture name value which follows the RFC 1766 naming standard. Basically, you create a new CultureInfo instance by specifying the culture name in the constructor:
CultureInfo c = new CultureInfo("en-US");
Step 1:
Create new website -> Solution Explorer -> Select sln right click and Add Asp.Net Folder - App_GlobalResources;
Now under App_globalization add resources file (.resx file), i add three resx file deafult for english, fr for french, de for german haivin key pair values;
Structure look like as in below image:
Step 2:
Code: write this piece of code
void Application_BeginRequest(Object sender, EventArgs e) { // Code that runs on application startup HttpCookie cookie = HttpContext.Current.Request.Cookies["CultureInfo"]; if (cookie != null && cookie.Value != null) { System.Threading.Thread.CurrentThread.CurrentUICulture =
new System.Globalization.CultureInfo(cookie.Value); System.Threading.Thread.CurrentThread.CurrentCulture =
new System.Globalization.CultureInfo(cookie.Value); } else { System.Threading.Thread.CurrentThread.CurrentUICulture =
new System.Globalization.CultureInfo("en"); System.Threading.Thread.CurrentThread.CurrentCulture =
new System.Globalization.CultureInfo("en"); } }
Step 3:
Add masterPage having a dropdownlist control;
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Globalization; using System.Threading; public partial class MasterPage : System.Web.UI.MasterPage { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack){ if (Session["ddindex"] != null) { ddlanguage.SelectedValue = Session["ddindex"].ToString(); ddlanguage.SelectedIndex = Convert.ToInt32(Session["ddindex"].ToString()); } else{ ddlanguage.SelectedValue = Thread.CurrentThread.CurrentCulture.Name; } } } protected void ddlanguage_SelectedIndexChanged(object sender, EventArgs e) { Session["language"] = ddlanguage.SelectedValue; //Sets the cookie that is to be used by Global.asax HttpCookie cookie = new HttpCookie("CultureInfo"); cookie.Value = ddlanguage.SelectedValue; Response.Cookies.Add(cookie); //Set the culture and reload for immediate effect. //Future effects are handled by Global.asax Thread.CurrentThread.CurrentCulture =
new CultureInfo(ddlanguage.SelectedValue); Thread.CurrentThread.CurrentUICulture =
new CultureInfo(ddlanguage.SelectedValue); if (cookie.Value == "en"){ Session["ddindex"] = 0; } else if (cookie.Value == "fr"){ Session["ddindex"] = 1; } else if (cookie.Value == "de"){ Session["ddindex"] = 2; } Server.Transfer(Request.Path); } }
Add childPage with controls and on dropdownlist selected index it will display respective language content
My sample Childpage desin code :
set text as resoures key;
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec |
OutPut shown in below images
1) For English
![]() |
No comments:
Post a Comment