Question

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?

Was it helpful?

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)));

OTHER TIPS

If you created a thread, then the culture info is reset to the default one(for that thread). I think in .net 4.5 there might be a way to set the default thread cultureinfo. http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.defaultthreadcurrentculture.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top