Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ASP.NET Website and C# with Multi-Language

0.00/5 (No votes)
23 Sep 2016 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here