Search code examples
c#c#-4.0localizationascx

Localization inside an ascx user control


----------------OLD CODE ---------------------------------------------- I have create a user control, which is a Main Menu, that has to be localized. So I have created 3 resource files inside the App_LocalResources, and I have a dropdown to change the languages selected.

The Main Menu looks like this :-

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MainMenu.ascx.cs" Inherits="GGX4._2.MainMenu" %>
<div>
    <asp:DropDownList ID="ddlLangs" runat="server" 
        onselectedindexchanged="ddlLangs_SelectedIndexChanged" AutoPostBack="True" meta:resourcekey="ddlLangsResource1">
        <asp:ListItem Text="English" Value="en-US" meta:resourcekey="ListItemResource1"></asp:ListItem>
        <asp:ListItem Text="German" Value="de-DE" meta:resourcekey="ListItemResource2"></asp:ListItem>
        <asp:ListItem Text="Spanish" Value="es-ES" meta:resourcekey="ListItemResource3"></asp:ListItem>
    </asp:DropDownList>
</div>
<div>
    <table>
      <tr>
        <td width="100%" nowrap height="16">
        <img border="0" src="Images/GREENSQUARE.gif" width="16" height="16"><b><font size="2" face="Arial">
            <asp:HyperLink ID="hypIntroduction" runat="server" NavigateUrl="Overview.htm" 
                meta:resourcekey="hypIntroductionResource1" >[hypIntroduction]</asp:HyperLink>
      </tr>
      <tr>
        <td width="100%" nowrap height="16">
        <img border="0" src="Images/GREENSQUARE.gif" width="16" height="16"><b><font size="2" face="Arial">
            <asp:HyperLink ID="hypGlobalGradingMethodology" runat="server" NavigateUrl="GGMethodology.htm" 
                meta:resourcekey="hypGlobalGradingMethodologyResource1" >[hypGlobalGradingMethodology]</asp:HyperLink>
        </td>
      </tr>
      <tr>
        <td width="100%" nowrap height="16">
        <img border="0" src="Images/Redsquare.gif" width="16" height="16"><b><font size="2" face="Arial">
            <asp:HyperLink ID="hypDeterminingBusiness" runat="server" 
                NavigateUrl="ScopematrixGeneral.htm" 
                meta:resourcekey="hypDeterminingBusinessResource1">[hypDeterminingBusiness]</asp:HyperLink>
        </td>
      </tr>
      <tr>
        <td width="100%" nowrap height="16"><font size="2" face="Arial">&nbsp;&nbsp;&nbsp;
        <img border="0" src="Images/BLUEBULLET.gif" width="16" height="16">
            <asp:HyperLink ID="hypMethodology" runat="server" 
                NavigateUrl="methodology.htm" 
                meta:resourcekey="hypMethodologyResource1">[hypMethodology]</asp:HyperLink>
          </font>
        </td>
      </tr>
  </table>
</div>

and in the code behind I have the following :-

        string defaultLanguage = Thread.CurrentThread.CurrentUICulture.ToString();


    protected void Page_Load(object sender, EventArgs e)
    {
        this.InitializeCulture();
    }

    protected void InitializeCulture()
    {
        if (String.IsNullOrEmpty(CurrentCulture))
        {
            CurrentCulture = defaultLanguage;
        }            

        if (!String.IsNullOrEmpty(CurrentCulture))
        {
            try
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo(CurrentCulture);
                Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
            }
            catch
            {
                throw;
            }
        }
    }

    public String CurrentCulture
    {
        get
        {
            if (null != Session["PreferedCulture"])
                return Session["PreferedCulture"].ToString();
            else
                return "en-US";
        }
        set
        {
            Session["PreferedCulture"] = value;
        }
    }


    protected void ddlLangs_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["PreferedCulture"] = ddlLangs.SelectedValue;
        InitializeCulture();
    }

However when I do the change in the dropdownlist, the Culture reamins the same. Normally when applied to a System.Web.UI.Page, I would override the InitializeCulture(), however I cannot find a way to do it in the ascx.

How can I achieve that?

Thanks for your help and time

-------------NEW CODE-----------------------------------------------

I have decided to make things simpler, and I have managed to achieve what I want, however with a page refresh which I do not like at all and wish to make without.

So basically I have created a simple example that is working now :-

The Site.Master just has the drop-down as the extra code :-

            <div>
            <asp:DropDownList ID="ddlLangs" runat="server" onselectedindexchanged="ddlLangs_SelectedIndexChanged" AutoPostBack="True" 
                    meta:resourcekey="ddlLangsResource1">
                <asp:ListItem Text="English" Value="en-US" 
                    meta:resourcekey="ListItemResource1" ></asp:ListItem>
                <asp:ListItem Text="German" Value="fr-FR" meta:resourcekey="ListItemResource2" ></asp:ListItem>
                <asp:ListItem Text="Spanish" Value="it-IT" 
                    meta:resourcekey="ListItemResource3" ></asp:ListItem>
            </asp:DropDownList>
        </div>

and the code behind looks like this:-

       protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["PreferredCulture"]!= null)
                ddlLangs.SelectedValue = Session["PreferredCulture"].ToString();

        }
    }

    protected void ddlLangs_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["PreferredCulture"] = ddlLangs.SelectedValue;
        Server.Transfer(Request.Path);
    }

The Default.aspx has the MainMenu UC, and a sample label, however it inheris from the BasePage :-

<asp:Content ID="MenuContent" runat="server" ContentPlaceHolderID="MainMenuContent">
<uc:MainMenu runat="server" ID="ucMainMenu" />

Welcome to ASP.NET!

</asp:Content>

And the BasePage has the code to Initialize the Culture

I wish to get rid of the Server.Transfer(Request.Path), and avoid refreshing the page, however I have not found out a method yet.

Any help/ideas will be very much appreciated!

Thanks


Solution

  • InitializeCulture() method execute earlier in Page life cycle, hence the culture you are setting in this method is old one..not the one user has selected..so you have to request a page again to set the culture user has selected.that's what you doing by Server.Transfer(Request.Path)..

    if you want to avoid 'Server.transfer'.. you need get the new value of culture during InitializeCulture() method execution and assign that new value. here is the link this shows you how to retrieve new value during InitializeCulture() method execution.

    Hope this helps..