Click here to Skip to main content
15,885,546 members
Articles / Web Development / HTML
Tip/Trick

ASP.NET Website and C# with Multi-Language

Rate me:
Please Sign up or sign in to vote.
4.96/5 (30 votes)
23 Sep 2016CPOL2 min read 165.9K   10.6K   55   26
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.

Image 1

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).

Image 2

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.NET
<asp:Literal ID="Literal3" runat="server" 
Text="<%$Resources:chienvh.language,mnuHome%>"/>
C#
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();
        }
    }
}
C#
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)


Written By
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.

Comments and Discussions

 
PraiseThanks! Pin
Hakan AK4-Apr-18 19:14
professionalHakan AK4-Apr-18 19:14 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun12-Oct-16 19:56
Humayun Kabir Mamun12-Oct-16 19:56 
QuestionHow about if we have 3 Languages ? Pin
urnightmare9120-Sep-16 1:07
urnightmare9120-Sep-16 1:07 
AnswerRe: How about if we have 3 Languages ? Pin
ChienVH21-Sep-16 22:25
ChienVH21-Sep-16 22:25 
GeneralRe: How about if we have 3 Languages ? Pin
urnightmare9122-Sep-16 5:12
urnightmare9122-Sep-16 5:12 
GeneralRe: How about if we have 3 Languages ? Pin
ChienVH22-Sep-16 17:02
ChienVH22-Sep-16 17:02 
GeneralRe: How about if we have 3 Languages ? Pin
urnightmare9122-Sep-16 23:46
urnightmare9122-Sep-16 23:46 
GeneralRe: How about if we have 3 Languages ? Pin
ChienVH22-Sep-16 23:49
ChienVH22-Sep-16 23:49 
GeneralRe: How about if we have 3 Languages ? Pin
urnightmare9122-Sep-16 23:53
urnightmare9122-Sep-16 23:53 
QuestionParser Error Message: The resource object with key 'aboutTitle' was not found. Pin
Member 1215211519-Nov-15 0:03
Member 1215211519-Nov-15 0:03 
AnswerRe: Parser Error Message: The resource object with key 'aboutTitle' was not found. Pin
ChienVH19-Nov-15 15:31
ChienVH19-Nov-15 15:31 
QuestionDoes not Exits in Current contex Pin
Shrikesh_kale18-Mar-15 2:48
Shrikesh_kale18-Mar-15 2:48 
AnswerRe: Does not Exits in Current contex Pin
ChienVH23-Mar-15 16:36
ChienVH23-Mar-15 16:36 
GeneralRe: Does not Exits in Current contex Pin
Shrikesh_kale23-Mar-15 19:18
Shrikesh_kale23-Mar-15 19:18 
GeneralRe: Does not Exits in Current contex Pin
ChienVH24-Mar-15 17:24
ChienVH24-Mar-15 17:24 
GeneralRe: Does not Exits in Current contex Pin
Shrikesh_kale24-Mar-15 19:52
Shrikesh_kale24-Mar-15 19:52 
ok
QuestionError Overrides Sub InitializeCulture() with MasterPage Pin
Member 108062064-Sep-14 23:49
Member 108062064-Sep-14 23:49 
AnswerRe: Error Overrides Sub InitializeCulture() with MasterPage Pin
ChienVH19-Nov-15 15:27
ChienVH19-Nov-15 15:27 
QuestionThanks Pin
Hussain Mubaireek20-Sep-13 22:46
Hussain Mubaireek20-Sep-13 22:46 
AnswerRe: Thanks Pin
ChienVH20-Sep-13 23:03
ChienVH20-Sep-13 23:03 
GeneralRe: Thanks Pin
Hussain Mubaireek20-Sep-13 23:17
Hussain Mubaireek20-Sep-13 23:17 
QuestionNice Article Pin
vipz00711-Jul-13 2:53
vipz00711-Jul-13 2:53 
AnswerRe: Nice Article Pin
ChienVH14-Jul-13 5:31
ChienVH14-Jul-13 5:31 
GeneralMy vote of 5 Pin
manoj.jsm2-May-13 23:19
manoj.jsm2-May-13 23:19 
GeneralRe: My vote of 5 Pin
ChienVH2-May-13 23:22
ChienVH2-May-13 23: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.