Reputation: 39660
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)?
Upvotes: 13
Views: 25940
Reputation: 4488
Or you could try this:
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(Markup.XmlLanguage.GetLanguage(Globalization.CultureInfo.CurrentCulture.IetfLanguageTag)));
Upvotes: 4
Reputation: 6595
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");
Upvotes: 6
Reputation: 13839
Try putting
<UICulture>en-US</UICulture>
... in your csproj file.
Upvotes: 6
Reputation: 1584
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);
}
}
Upvotes: 0
Reputation: 23044
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
Upvotes: 11