How to Create Multiple Language Website in Asp.net C#

/ / 17 Comments

Multilingual Website in Asp.net C# : In this tutorial, we are going to create multi-language website in Asp.net C#. A website which can support Multiple Language and change according to the culture of a specific geographical location is not a new topic. Web sites to support multiple languages can be a challenging and time-consuming process.

You find many articles related on internet and well explained, But I still see that many of them are complicated for "Beginners". I tried to make this article 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 multiple language website, namely in the form of Resource Files, the CultureInfo class, and the System.Globalization and System.Resources.ResourceManager namespaces.


First 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 a process of creating and configuring your application for a specific language.

Cultures:  A culture is a 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  multiple language websites ( Multilingual ):

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 default for English, fr for French, de for german having key pair values;

The structure looks like as shown below image:




Step 2:
Add Global.asax file: Code: write this piece of code
//*
Public 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 drop-down list 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 drop-down list selected index it will display respective language content My sample Child page design code : set text as resources key; i.e `Text="<%$Resources:Resources,FieldName %>"`
    <table id="Table1">
        <tbody>
            <tr>
                <td class="style1">
                    <asp:Label ID="Label2" runat="server" Text="<%$Resources:Resources, FirstName %>"></asp:Label></td>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="<% $Resources:Resources, LastName %>"></asp:Label></td>
            </tr>
            <tr>
                <td class="style1">
                    <asp:Label ID="Label4" runat="server" Text="<%$Resources:Resources, Gender %>"></asp:Label></td>
                <td>
                    <asp:RadioButtonList ID="RdBtnLtGender" RepeatDirection="Horizontal" runat="server">
                        <asp:ListItem Text="<%$Resources:Resources, Male %>" Value="1"></asp:ListItem>
                        <asp:ListItem Text="<%$Resources:Resources, Female %>" Value="0"></asp:ListItem>
                    </asp:RadioButtonList></td>
            </tr>
            <tr>
                <td class="style1">
                    <asp:Label ID="Label7" runat="server" Text="<%$ Resources:Resources, Month %>"></asp:Label></td>
                <td>
                    <asp:DropDownList Height="20px" ID="ddyear" runat="server" Width="64px">
                        <asp:ListItem Value="1">Jan</asp:ListItem>
                        <asp:ListItem Value="2">Feb</asp:ListItem>
                        <asp:ListItem Value="3">Mar</asp:ListItem>
                        <asp:ListItem Value="4">Apr</asp:ListItem>
                        <asp:ListItem Value="5">May</asp:ListItem>
                        <asp:ListItem Value="6">Jun</asp:ListItem>
                        <asp:ListItem Value="7">Jul</asp:ListItem>
                        <asp:ListItem Value="8">Aug</asp:ListItem>
                        <asp:ListItem Value="9">Sep</asp:ListItem>
                        <asp:ListItem Value="10">Oct</asp:ListItem>
                        <asp:ListItem Value="11">Nov</asp:ListItem>
                        <asp:ListItem Value="12">Dec</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
        </tbody>
    </table>
 
ScreenShots :

OutPut   1) For English


2) For French

 

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to thank me? 👳 Buy Me a Coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.


Post Comment

Your email address will not be published. Required fields are marked *

17 Comments

  1. Anonymous 02/01/2013 07:27:55
    It's so simple. Keep em' coming. :)
  2. syed muhammad kawish 08/11/2015 08:13:42
    you website is very good , and give good understanding.
  3. Satinder singh 08/11/2015 16:37:10
    @syed muhammad kawish: Thanks for your kind words
  4. Manzar Hussain 09/27/2016 08:50:01
    NICE POST. EASY TO UNDERSTAND. THANKS
  5. Manzar Hussain 09/27/2016 08:52:35
    POST IS GOOD. BUT GIVE ME SCREEN SHORT OF MASTER PAGE.
  6. Manzar Hussain 09/27/2016 08:53:38
    POST IS GOOD. PLEASE GIVE SCREEN SHORT OF MASTER PAGE.
  7. Satinder singh 10/13/2016 15:55:09
    Thanks Rahul for updating this.
  8. Satinder singh 06/12/2017 12:10:41
    Hi Sandy, Make sure you have selected project as Asp.net (Webforms) and not Asp.net MVC, then right click on Web Project and you will see Add Asp.Net Folder – App_GlobalResources folders.
  9. Sandy 06/13/2017 02:50:19
    Oh no. My project is Asp.net MVC. I am new using .NET and C#, hence the newbie question! Is there a way for me to add in the Spanish option? Thanks, Sandy
  10. Rajesh 06/14/2017 09:57:36
    Yes, I will implement on my site Global-directory.org and make this website multilingual multiple languages in asp.net c
  11. mina 06/17/2017 13:07:52
    Thanks, thats very good and helpful But I have two problem 1)Rahul update which part if code in your article 2)If my dropdown list elements comes from data bound of database, which part of code must be change
  12. Gokul 09/13/2017 07:50:33
    What about Binded DropDown values from Database how to convert them too ??? any idea ??
  13. Satinder singh 10/26/2017 06:57:27
    Hi Gokul, For this, you have to maintain values in your database and based on language bind specific column or specific table.
  14. akash 01/30/2018 07:56:27
    how i suppose to change whole project , i have nearly 1000 pages in my web application, is there any tool to change that
  15. vikram singh 07/30/2021 12:38:29
    Hello it is very useful me thanks you Please send me this complete
  16. Satinder singh 07/31/2021 12:54:54
    Hi Vikram,
    This article was written very long back, so am not having the source code. But you can try yourself step by step following this article. If you got any issue share it here will look into it.
  17. Francis Carrier 09/26/2022 18:13:37
    POST IS GOOD. BUT GIVE ME SCREEN SHORT OF MASTER PAGE.