asp.net.netlocalecultureuiculture

Changing the culture and ui culture on the site only updates one field


I am having an issue where changing the culture and UI culture dynamically by performing a button click to change the language on the website (http://cptestlocalisation.azurewebsites.net) does not change "all" my resx fields (only one field changes for some reason) based on fallback for the following App_LocalResources files:

Site.Master.resx
Site.Master.fr.resx
Site.Master.zh.resx

However, the above works just fine only when adjusting the browser's language preference and refreshing the page.

The contents are respectively:

header.Text Localisation and Globalisation Test Website
login.Text  Log In
logout.LogoutText   Log Out
menuItemAbout.Text  About
menuItemHome.Text   Home

header.Text Localisation et mondialisation test Site Web
login.Text  Se Connecter
logout.LogoutText   Se Déconnecter
menuItemAbout.Text  à propos
menuItemHome.Text   maison

header.Text 本地化和全球化测试网站
login.Text  登入
logout.LogoutText   Log Out
menuItemAbout.Text  关于我们
menuItemHome.Text   登出

What does not work code snippet:

<h1 runat="server" enableviewstate="false">
    <asp:Localize runat="server" ID="header" meta:resourcekey="header" />
</h1>

What works:

<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
    <AnonymousTemplate>
        [ <a href="#" ID="HeadLoginStatus" runat="server"><asp:Localize runat="server" ID="login" meta:resourcekey="login" /></a> ]
    </AnonymousTemplate>
    <LoggedInTemplate>
        Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>!
        [ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" meta:resourcekey="logout" LogoutPageUrl="~/"/> ]
    </LoggedInTemplate>
</asp:LoginView>

Code behind:

protected void btnEnglish_Click(object sender, EventArgs e)
{
    Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
    InitializeCulture();
}

protected void btnFrench_Click(object sender, EventArgs e)
{
    Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("fr");
    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr");
    InitializeCulture();
}

protected void btnChinese_Click(object sender, EventArgs e)
{
    Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("zh");
    Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("zh");
    InitializeCulture();
}

Web.config entry:

<globalization culture="auto" uiCulture="auto" enableClientBasedCulture="true" />

I need help getting to the bottom of this issue. If you see the website, only the login link in the top right hand corner changes on button click.


Solution

  • I wouldn't expect it to work. You're only changing the culture and uiculture for the current thread from the point of execution of the button click. Anything that is handled before the button click event isn't going to be effected. Instead of doing it this way, set a cookie with the selected culture then redirect the page back to itself. Check for the cookie in the oninit event and if found, change the culture to match the cookie value.