3

I'm seeing strange behavior in a WPF app (which I reproduced in a sample app to be sure).

When the app starts I'm setting CultureInfo.CurrentCulture and CultureInfo.CurrentUICulture to e.g. French, before the main app window loads. Then the main app window loads and localized text appears as expected, in French, like so:

<Window.Resources>
  <Strings x:Key="Resources"/>
</Window.Resources

<Button Content={Binding Label, Source={StaticResource Resources}}/>

The problem is that if I click the button, the culture and the button's text gets reset to en-US. I added an event handler for the Click event and inside it CurrentCulture and CurrentUICulture are both reset to "en-US".

Any idea why this happens? It seems weird that this would somehow happen automatically.

Note: I've also tried doing

 var cultureName = CultureInfo.CurrentCulture.Name;
 FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
           new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(cultureName)));

and

this.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);

before the main window loads, but it doesn't help, the culture still gets reset.

UPDATE: I asked the question also on the WPF forums (https://social.msdn.microsoft.com/Forums/vstudio/en-US/884af2cb-fcc3-4cfe-bea0-7a84f74583a8/culture-gets-reset-on-button-click?forum=wpf). There I described the actual scenario maybe a bit better: I need to be able to set a specific culture when the app starts and have it be "preserved" throughout the app's lifetime. Unfortunately when clicking a button the culture gets reset, as described.

adrian h.
  • 2,996
  • 3
  • 18
  • 24
  • This question MAY have the answers you need. It looks as if the culture is not applied to controls for some reason. Not sure if theres much you can about it however. http://stackoverflow.com/questions/4041197/how-to-set-and-change-the-culture-in-wpf – Lex Webb Oct 23 '15 at 14:48
  • What's interesting is that if I set a breakpoint in the button's click handler, the culture gets reset to en-US only the first time it's hit. If, after clicking it once, I set again the French culture and then click the button again, the CurrentCulture and CurrentUICulture are both French. – adrian h. Oct 26 '15 at 05:54

1 Answers1

0

Might have something to do with your Window.Resources->Binding method of displaying resource strings. I display localization strings like this:

<Window x:Class="CultureTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:p="clr-namespace:CultureTest.Properties"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Content="{x:Static p:Resources.Greeting}"/>
</Grid>

and setting the culture in App.OnStartup:

protected override void OnStartup(StartupEventArgs e)
{
    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ru-RU");
    base.OnStartup(e);
}

it keeps it's culture just fine. Is this an option for you?

  • Unfortunately it's not an option as I need to be able to dynamically change the culture at runtime (user selects it from a list) and using the x:Static extension doesn't work in this case (the content of the button doesn't change) – adrian h. Oct 26 '15 at 05:50