Click here to Skip to main content
12,633,519 members (29,553 online)
Click here to Skip to main content
Add your own
alternative version

Stats

83.1K views
8.1K downloads
51 bookmarked
Posted

ASP.NET Website and C# with Multi-Language

, 23 Sep 2016 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. You can also find the video here Create Multi-language ASP.NET Website.

Step 1

Create a new website project (I’m using VS2012 in this demo).

Step 2

Once your project has been 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 a new class file with the name as BasePage.

In this file, you will do the work to initialize the Culture, and then, hence, when 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--
        }
    }
}

History

  • 24th December, 2013: Initial version
  • 23rd September, 2016: Added video link

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
Product Manager
Vietnam Vietnam
Updated LinkedIn: https://vn.linkedin.com/in/chienvh

I am currently working as the position of project manager for a long time. Had to take care a lot of projects at the same times, so I don't have many free times in a day for contributing the articles, tips/tricks on codeproject.
While I was at the previous company sometimes I participated in training courses for new employees, so I have good teaching skills and ability to convey information to others. Meaning in each my post I will try to explain more detail as possible for the junior devs are able to implement/understand what's I have done.

Also, would like to share my responsibilities for current position:

• Coordinate internal resources and third parties/vendors for the flawless execution of projects
• Ensure that all projects are delivered on-time, within scope and within budget
• Assist in the definition of project scope and objectives, involving all relevant stakeholders and ensuring technical feasibility
• Ensure resource availability and allocation
• Develop a detailed project plan to monitor and track progress
• Report and escalate to management as needed
• Perform risk management to minimize project risks
• Establish and maintain relationships with third parties/vendors
• Create and maintain comprehensive project documentation
• Support team members to solve technical issues

Opening and looking forward to finding suitable jobs.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun12-Oct-16 20:56
memberHumayun Kabir Mamun12-Oct-16 20:56 
QuestionHow about if we have 3 Languages ? Pin
urnightmare9120-Sep-16 2:07
memberurnightmare9120-Sep-16 2:07 
AnswerRe: How about if we have 3 Languages ? Pin
ChienVH21-Sep-16 23:25
memberChienVH21-Sep-16 23:25 
GeneralRe: How about if we have 3 Languages ? Pin
urnightmare9122-Sep-16 6:12
memberurnightmare9122-Sep-16 6:12 
GeneralRe: How about if we have 3 Languages ? Pin
ChienVH22-Sep-16 18:02
memberChienVH22-Sep-16 18:02 
GeneralRe: How about if we have 3 Languages ? Pin
urnightmare9123-Sep-16 0:46
memberurnightmare9123-Sep-16 0:46 
GeneralRe: How about if we have 3 Languages ? Pin
ChienVH23-Sep-16 0:49
memberChienVH23-Sep-16 0:49 
GeneralRe: How about if we have 3 Languages ? Pin
urnightmare9123-Sep-16 0:53
memberurnightmare9123-Sep-16 0:53 
QuestionParser Error Message: The resource object with key 'aboutTitle' was not found. Pin
Member 1215211519-Nov-15 1:03
memberMember 1215211519-Nov-15 1:03 
AnswerRe: Parser Error Message: The resource object with key 'aboutTitle' was not found. Pin
ChienVH19-Nov-15 16:31
memberChienVH19-Nov-15 16:31 
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 
AnswerRe: Error Overrides Sub InitializeCulture() with MasterPage Pin
ChienVH19-Nov-15 16:27
memberChienVH19-Nov-15 16:27 
QuestionThanks 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    Praise Praise    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
Web02 | 2.8.161208.2 | Last Updated 23 Sep 2016
Article Copyright 2013 by ChienVH
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid