0

I developed a web application and it is a dynamic site. Can I place a multilingual option for my site?

If yes, are there any plugins for that? How can I go further to develop a multilingual application?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Surya sasidhar
  • 29,607
  • 57
  • 139
  • 219

1 Answers1

0

We have the DropdownList control. On its SelectedIndexChanged event, it postbacks and we then change the site culture, and it then loads the respective resource files...

DropDownList ASP.NET Code

<asp:DropDownList ID="ddlLanguage" runat="server" CssClass="select-language" AutoPostBack="true">
    <asp:ListItem Value="en-US" Text="English" title="/images/Flag_USA.gif"></asp:ListItem>
    <asp:ListItem Value="it-IT" Text="Italiano" title="/images/Flag_Italian.gif"></asp:ListItem>
    <asp:ListItem Value="fr-FR" Text="Française" title="/images/Flag_French.gif"></asp:ListItem>
</asp:DropDownList>

Common class inherited by all of the web pages

using System;
using System.Web;
using System.Threading;
using System.Globalization;

public class languagebase : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        try
        {
            string LanguageCode = Request["ctl00$ucMenu$ddlLanguage"]; // Language Drop Down Control in Front End
            if (!LanguageCode.IsNullOrEmpty())
            {
                setCulture(LanguageCode);  // Set Culture language from drop down
                Request.Cookies["LanguageCode"].Value = LanguageCode; // Update REQUEST Cookie language from drop down
                SetCookies(LanguageCode); // Set Cookie language from drop down
            }
        }
        catch(Exception ex)
        {
            setCulture("en-US"); // Set default language
            Request.Cookies["LanguageCode"].Value = "en-US"; // Update REQUEST Cookie language to default
            SetCookies("en-US"); // Set default language
        }
        base.InitializeCulture();
    }

    private static void setCulture(string LanguageValue)
    {
       Thread.CurrentThread.CurrentUICulture = new CultureInfo(LanguageValue);
       Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(LanguageValue);
    }

    public void SetCookies(string strLanguage)
    {
        System.Web.HttpContext.Current.Response.Cookies["LanguageCode"].Value = strLanguage;
        System.Web.HttpContext.Current.Response.Cookies["LanguageCode"].Expires = DateTime.Now.AddDays(15);
    }
}

Extension Method (just for sake of information)

public static Boolean IsNullOrEmpty(this String original)
{
    return string.IsNullOrEmpty(original);
}

Please review these link before starting to implement:

  1. Globalization Architecture
  2. [jQuery globalization tricks used in ASP.NET MVC] which can be helpful in ASP.NET also.
  3. My question here defines the way to implement multi-language code
  4. How to create and use Global language resources in ASP.NET

These make all of your pages to use inherit from the languagebase class as below:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default : languagebase
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}
Community
  • 1
  • 1
Harsh Baid
  • 7,199
  • 5
  • 48
  • 92