Setting Culture (en-IN) globally in WPF application
I think you will need to add the following.
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-IN");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-IN");
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(
XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
Read more here:
http://www.west-wind.com/weblog/posts/2009/Jun/14/WPF-Bindings-and-CurrentCulture-Formatting
Just to give you an example, this is how I initialize the Culture in my program, based on the user setting, but you can simply replace UserSettings.DefaultCulture
and UserSettings.Default.UICultrue
with your wanted Culture.
private static void InitializeCultures()
{
if (!String.IsNullOrEmpty(UserSettings.Default.Culture))
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(UserSettings.Default.Culture);
}
if (!String.IsNullOrEmpty(UserSettings.Default.UICulture))
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserSettings.Default.UICulture);
}
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(
XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
}
How can i globally set the Culture in a WPF Application?
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
Where should I change/set culture info settings in my WPF app and why? (Must works on all .NET frameworks 4.0 and newer)
And where should I set it?
Just open the auto-generated App.xaml.cs
file and override the OnStartup
method:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); ;
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US"); ;
FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata(
XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
base.OnStartup(e);
}
}
This method is called once when the application starts.
How to change Application Culture in wpf?
try this
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");
How to globally set the culture in a WPF application (Windows XP)?
Problem was actually much simpler, but I completely missed it: The translated resources are located in the satellite *.resources.dll assemblies. On my own machine, these files are copied by the compiler to the output directory (to the respective language code subdirectories), on testing machines (XP in this case), these files were missing though since they have not been added to the setup package.
After adding the *.resources.dll files, all worked as it should.
How to set and change the culture in WPF
I never found a way to do exactly what I asked for in the question.
In my case I ended up solving it by having all my usercontrols inherit from a superclass that contained this:
/// <summary>
/// Contains shared logic for all XAML-based Views in the application.
/// Views that extend this type will have localization built-in.
/// </summary>
public abstract class ViewUserControl : UserControl
{
/// <summary>
/// Initializes a new instance of the ViewUserControl class.
/// </summary>
protected ViewUserControl()
{
// This is very important! We make sure that all views that inherit
// from this type will have localization built-in.
// Notice that the following line must run before InitializeComponent() on
// the view. Since the supertype's constructor is executed before the type's
// own constructor (which call InitializeComponent()) this is as it
// should be for classes extending this
this.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
}
}
When the user changes the language I then create new instances of any usercontrols that are currently running.
This solved my problem. However, I would still like a way to do this "automatically" (i.e. without having to keep track of any instantiated objects).
Setting Culture in .net5 WPF application
The reason for this behavior is how async
methods are implemented. async
methods have their own special execution context. This context has it's own CultureInfo
, which is inherited from the non-async context that invokes the async
method.
In your case, the async
context's culture is inherited from the main thread, before the culture is changed.
What you can do is to implement the already suggested solution using the Dispatcher.InvokeAsync
to postpone the CultureInfo configuration. This way the configuration is executed outside the async
context:
Dispatcher.InvokeAsync(() => CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-EN"));
Since this can mess up your initialization routine, as the real context would be avaailable after the application is configured and the main Window displayed, you would prefer a different solution.
You can for example used an event based initialization routine, where you run low-level application configuration like culture configuration first and continue with the remaining initialization that involves asynchronous operations in an async
context:
App.xaml.cs
// Event may be defined on a different class
private event EventHandler ConfigurationCompleted;
private void OnConfigurationCompleted() => this.ConfigurationCompleted?.Invoke(this, EventArgs.Empty);
protected override void OnStartup(StartupEventArgs startupEventArgs)
{
this.ConfigurationCompleted += ConfigureInAsyncContext;
// Do "low-level" application configuration. Code may be executed in a different class context
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-EN");
...
// Continue in async context
OnConfigurationCompleted();
}
private async void ConfigureInAsyncContext(object sender, EventArgs e)
{
// TODO::Execute async operations
new MainWindow().Show();
}
The key is to separate the non-async configuration from the async initialization.
Setting CurrentCulture AND CurrentUICulture in WPF
But that doesn't feel like proper usage of the interface.
It is. An implementation of the IValueConverter
interface is supposed to convert values and hence doesn't care about any UICulture
.
So getting the CurrentUICulture
from a source external to the converter is actually a proper use. The CurrentUICulture
is not passed to the Convert
method.
Setting global NumberDecimalSeperator / Culture - WPF XAML
You can only set the FrameworkElement.Language
property to a language, but not to a custom CultureInfo
. CultureInfo.CurrentCulture.IetfLanguageTag
returns a language tag that is not affected by NumberFormat
or DateTimeFormat
properties of CultureInfo.CurrentCulture
.
What you could to do apply the formatting globally is to define a custom binding where you set the ConverterCulture
property in one place:
public class CustomBinding : Binding
{
private static readonly CultureInfo cultureInfo;
static CustomBinding()
{
cultureInfo = new CultureInfo("sv");
cultureInfo.NumberFormat.NumberDecimalDigits = 2;
cultureInfo.NumberFormat.NumberDecimalSeparator = ",";
cultureInfo.NumberFormat.NumberGroupSeparator = ".";
cultureInfo.DateTimeFormat.ShortDatePattern = "dd.MM.yyyy";
cultureInfo.DateTimeFormat.FullDateTimePattern = "dd.MM.yyyy";
cultureInfo.DateTimeFormat.ShortTimePattern = "HH:mm";
cultureInfo.DateTimeFormat.LongDatePattern = "dd.MM.yyyy";
cultureInfo.DateTimeFormat.LongTimePattern = "HH:mm";
}
public CustomBinding()
{
ConverterCulture = cultureInfo;
}
public CustomBinding(string path)
: base(path)
{
ConverterCulture = cultureInfo;
}
}
You could then replace all your current bindings with your custom markup extension, e.g.:
<TextBlock Text="{local:CustomBinding Date}" />
Related Topics
How to Pass Parameters to the Custom Action
Event Bubbling and Mvp: ASP.NET
Non-Static Method Requires a Target
Differencebetween Server.Mappath and Hostingenvironment.Mappath
Httpclient Not Supporting Postasjsonasync Method C#
How to Wait for Async Method to Complete
Checking User Name or User Email Already Exists
Should I Store My Images in the Database or Folders
Create Dynamic Buttons in a Grid Layout - Create a Magic Square Ui
Does Using Tasks (Tpl) Library Make an Application Multithreaded
Getting Checkbox Value in ASP.NET MVC 4
Register a Dll into the Gac - But Then It Doesn't Show Up in the Assembly Window
How Does Transactionscope Roll Back Transactions
I Didn't Find "Zipfile" Class in the "System.Io.Compression" Namespace
How to Get the Actual Monitor Name? as Seen in the Resolution Dialog