Click here to Skip to main content
11,428,876 members (61,494 online)
Click here to Skip to main content

ASP.NET Website and C# with Multi-Language

, 23 Dec 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This article will guide you how to implement asp.net website with multi-language

Scenarios

In this article I will guide you how to implement Multi-language into an Asp.net website using C# and App_GlobalResources files.

Step 1: Create new a website project (I’m using VS2012 in this demo)

Step 2: Once your project created you need to add App_GlobalResources folder to your project.

This is an easy action. To create the folder, in Solution Explorer, just right-click the name of your Web site, click Add Folder, and then click App_GlobalResources folder. There can only be one of these folders in an application, and it must be located at the root of the application.

Step 3: After that, right-click on App_GlobalRrsources folder and select Resources File to create new files for additional languages. You can define the name for language files as the pattern “name.language-culture.resx”. In this demo I will define two files name as chienvh.language.resx (English) and chienvh.language.vi-vn.resx (Vietnamese).

Step 4: Open two resource files to define the name and value (notice leaving the names as a keys as the same in those files) 

 

Step 5: Create new a class file with the name as BasePage.  

In this file you will do the work to initialize the Culture, and then, hence, when you creating a new webpage then in the code-behind of that file you need to inherit from BasePage instead of default option as System.Web.UI.Page 

 Example:        

Original: public partial class Default : System.Web.UI.Page 
Now: public partial class Default : BasePage

Step 6: Create a new webpage with the name as Default.aspx, then open the code behind of this file and change inheritance from System.Web.UI.Page to BasePage.

Step 7: Use asp:Literal control to get data from resource files

Example:

<asp:Literal ID="Literal3" runat="server" Text="<%$Resources:chienvh.language,mnuHome%>"/>
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using System;
 
namespace ASPNetMultiLanguage
{
    public class BasePage : System.Web.UI.Page
    {
        protected override void InitializeCulture()
        {
            if (!string.IsNullOrEmpty(Request["lang"]))
            {
               
			Session["lang"] = Request["lang"];
            }
            string lang = Convert.ToString(Session["lang"]);
            string culture = string.Empty;
            /* // In case, if you want to set vietnamese as default language, then removing this comment
            if(lang.ToLower().CompareTo("vi") == 0 ||string.IsNullOrEmpty(culture))
            {               
				culture = "vi-VN";
            }
             */
            if (lang.ToLower().CompareTo("en") == 0 || string.IsNullOrEmpty(culture))
            {
                culture = "en-US";
            }
            if (lang.ToLower().CompareTo("vi") == 0)
            {               
				culture = "vi-VN";
            }
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
 
            base.InitializeCulture();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ASPNetMultiLanguage
{
    public partial class Default : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            #region--Show/hide language link
            if (!string.IsNullOrEmpty(Convert.ToString(Session["lang"])))
            {
                if (Convert.ToString(Session["lang"]) == "en")
                {
                    linkVietnameseLang.Visible = true;
                    linkEnglishLang.Visible = false;
                }
                else
                {
                    linkEnglishLang.Visible = true;
                    linkVietnameseLang.Visible = false;
                }
            }
            else
            {
                linkVietnameseLang.Visible = false;
                linkEnglishLang.Visible = true;
            }
            #endregion--
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

chienvh
Software Developer (Senior)
Vietnam Vietnam
Full name: Chien Vu Hoang
Email: chien.vh@gmail.com
Skype: chien.vh

Experienced in: (for more detail, refer to my linkedin as below url)
- .Net ( ASP.Net, MVC, C#, VB.Net)
- SharePoint
- LINQ
- SQL
- JQuery, Ajax, HTML, CSS
- BI, Big Data, Crystal report, Reporting service, Birt report, Birst.
Follow on   Google+   LinkedIn

Comments and Discussions

 
QuestionDoes not Exits in Current contex Pin
Member 1141130818-Mar-15 3:48
memberMember 1141130818-Mar-15 3:48 
AnswerRe: Does not Exits in Current contex Pin
chienvh23-Mar-15 17:36
memberchienvh23-Mar-15 17:36 
GeneralRe: Does not Exits in Current contex Pin
Member 1141130823-Mar-15 20:18
memberMember 1141130823-Mar-15 20:18 
GeneralRe: Does not Exits in Current contex Pin
chienvh24-Mar-15 18:24
memberchienvh24-Mar-15 18:24 
GeneralRe: Does not Exits in Current contex Pin
Member 1141130824-Mar-15 20:52
memberMember 1141130824-Mar-15 20:52 
QuestionError Overrides Sub InitializeCulture() with MasterPage Pin
Member 108062065-Sep-14 0:49
memberMember 108062065-Sep-14 0:49 
QuestionThanks [modified] Pin
Hussain Mubaireek20-Sep-13 23:46
memberHussain Mubaireek20-Sep-13 23:46 
AnswerRe: Thanks Pin
chien.vh21-Sep-13 0:03
memberchien.vh21-Sep-13 0:03 
GeneralRe: Thanks Pin
Hussain Mubaireek21-Sep-13 0:17
memberHussain Mubaireek21-Sep-13 0:17 
QuestionNice Article Pin
vipz00711-Jul-13 3:53
membervipz00711-Jul-13 3:53 
AnswerRe: Nice Article Pin
chien.vh14-Jul-13 6:31
memberchien.vh14-Jul-13 6:31 
GeneralMy vote of 5 Pin
manoj.jsm3-May-13 0:19
membermanoj.jsm3-May-13 0:19 
GeneralRe: My vote of 5 Pin
chien.vh3-May-13 0:22
memberchien.vh3-May-13 0:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 23 Dec 2013
Article Copyright 2013 by chienvh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid