12

I would like to set the Culture of a WPF application to a specific one based on user preferences.

I can do this for the current thread via Thread.CurrentThread.Current(UI)Culture, but is there any way to do this globally for the application (so it affects all threads by default)?

Botz3000
  • 39,020
  • 8
  • 103
  • 127

5 Answers5

11

there is no way to set it for all threads in the application, however if you are creating the threads in your application you can set the culture for everyone by yourself as you mentioned above

To set the Culture to the Main Application use the following snippet code:

Dim newCulture As CultureInfo = new CultureInfo("fr-FR")
CurrentThread.CurrentCulture = newCulture
Wael Dalloul
  • 22,172
  • 11
  • 48
  • 57
6

Try putting

<UICulture>en-US</UICulture>

... in your csproj file.

Grant Birchmeier
  • 17,809
  • 11
  • 63
  • 98
Scott Whitlock
  • 13,739
  • 7
  • 65
  • 114
5

ok so this is what I use in order to make sure all of my app is in a en-US culture.

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");
XmlLanaguage lang = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(lang));
FrameworkContentElement.LanguageProperty.OverrideMetadata(typeof(System.Windows.Documents.TextElement), new FrameworkPropertyMetadata(lang));

in order to make a single thread in a culture you can make

Thread.CurrentThread.CurrentCulture = new CultureInfo("EN-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("EN-US");
Grhm
  • 6,726
  • 4
  • 40
  • 64
Gilad
  • 6,437
  • 14
  • 61
  • 119
  • You also need to do `System.Windows.Documents.TextElement` in order to catch tags (eg within `TextBlock` elements) that don't inherit from `FrameworkElement`. – Grhm Sep 24 '14 at 12:30
  • Grhm what do you mean? – Gilad Sep 24 '14 at 14:09
  • 1
    I have some sections in my app that look like: `Label: ` In order to get the number formatted in the correct locale I had to add `FrameworkElement.LanguageProperty.OverrideMetadata(typeof(System.Windows.Documents.TextElement), new FrameworkPropertyMetadata( XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));` as well as the lines we mentioned. Run doesn't inherit from FrameworkElement ( see http://msdn.microsoft.com/en-us/library/system.windows.documents.run(v=vs.110).aspx ) – Grhm Sep 24 '14 at 14:30
  • Unfortunately `CultureInfo.DefaultThreadCurrentCulture` is not available prior to .net 4.5. – Pollitzer Oct 17 '15 at 13:48
4

Or you could try this:

 FrameworkElement.LanguageProperty.OverrideMetadata(GetType(FrameworkElement), New FrameworkPropertyMetadata(Markup.XmlLanguage.GetLanguage(Globalization.CultureInfo.CurrentCulture.IetfLanguageTag)))
dovid
  • 6,354
  • 3
  • 33
  • 73
Darren
  • 4,408
  • 4
  • 39
  • 57
  • You need to do it at application start up before loading the visual tree. – Darren Dec 06 '13 at 12:29
  • You also need to do `System.Windows.Documents.TextElement` in order to catch `` tags (eg within `TextBlock` elements) that don't inherit from `FrameworkElement`. – Grhm Sep 24 '14 at 12:31
0

Or try building an appropriate Attached Property like this

public class CultureHelper : DependencyObject
{

    public string Culture
    {
        get { return (string)GetValue(CultureProperty); }
        set { SetValue(CultureProperty, value); }
    }




    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CultureProperty =
        DependencyProperty.RegisterAttached("Culture", typeof(string), typeof(CultureHelper), new FrameworkPropertyMetadata("en", CultureChanged));

    private static void CultureChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        //For testing purposes in designer only 
        if (DesignerProperties.GetIsInDesignMode(d))
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo((string)e.NewValue);
        }

    }

    public static void SetCulture(DependencyObject element, string value)
    {
        element.SetValue(CultureProperty, value);
    }

    public static string GetCulture(DependencyObject element)
    {
        return (string)element.GetValue(CultureProperty);
    }


}
Matze
  • 1,402
  • 2
  • 17
  • 24