Search code examples
c#wpfglobalizationcultureinfo

Culture Not Being Set In App


I'm working on a WPF / C# app that needs to be culturally aware for globalization. I already have resource files and a bindable translation manager that is all working as expected.

At the moment I'm doing this:

 Thread.CurrentThread.CurrentUICulture = _currentlyConfiguredUiCulture;
 Thread.CurrentThread.CurrentCulture = _currentlyConfiguredUiCulture;

This is all wired up in the UI like this:

TranslationManager.Instance.LanguageChanged += TranslationManager_LanguageChanged;

private void TranslationManager_LanguageChanged(object sender, EventArgs e)
{
    Thread.CurrentThread.CurrentUICulture = TranslationManager.Instance.CurrentLanguage;
    Thread.CurrentThread.CurrentCulture = TranslationManager.Instance.CurrentLanguage;
}

This all works sweet!

The problem is when the app is started my machine locale is "en-GB" and this is correctly set using the code shown above. However, when I hit some code I have in an IValueConverter class dealing with dates:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (value != null)
    {
        return DateTime.Parse(value.ToString(), culture);
    }

    return null;
}

The culture property here is always "en-US" ... how on earth is this occurring? How do I fix this so that the app is actually using the correct system culture?


Solution

  • This link states that you might have to add the following:

    FrameworkElement.LanguageProperty.OverrideMetadata(
      typeof(FrameworkElement), 
      new FrameworkPropertyMetadata(
        XmlLanguage.GetLanguage(
          System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag)));