Localization resources not changing on certain hyperlink controls in a page

Enter image description here

Above is the screenshot of the site in development…

We have a DropdownList control and on its SelectedIndexChanged it postbacks, and we then change the site culture and it then loads the respective resources 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 info)

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

Example Control on which Localized Resource is not loading

<li>
     <img src="/images/my-listing.png" alt="" align="absmiddle" /><asp:HyperLink ID="hlnkMyProperties" runat="server" meta:resourcekey="hlnkMyProperties"></asp:HyperLink>
</li>

Problem at hand

Here, if I reload the page from the browser’s addressbar then resources are loaded correctly.
I am just imaging how it is possible that the certain controls have English resources and some have old Italian resources when switching the culture?

I hope I have explained it well.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

I changed the HyperLink control to the following syntax and voila! It’s working now.

<li>
    <img src="/images/my-listing.png" alt="" align="absmiddle" /><a ID="hlnkMyProperties" runat="server"><%= GetLocalResourceObject("hlnkMyProperties")%></a>
</li>

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply