0

I'm trying to set up an a culture info for my WPF application and I've found several examples, like:

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");

And something like this:

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

           }

What is right way to achieve this? And where should I set it? I think setting it everytime windows is opened/loaded is not good idea?

billy_56
  • 649
  • 3
  • 11
  • 27
  • In your Main() method and at the beginning of every thread that produces output that is culture-sensitive (which might be none). Note that in .Net 4.6 or later, you only need to set it in the main method, but you're using .net 4.0. – Matthew Watson Nov 20 '17 at 13:47
  • Where? How about the OnStartup method of your App.xaml.cs? – mm8 Nov 20 '17 at 14:28
  • Possible duplicate of [Setting Culture (en-IN) Globally in WPF App](https://stackoverflow.com/questions/7454024/setting-culture-en-in-globally-in-wpf-app) – Sinatr Nov 20 '17 at 14:40
  • @mm8 Can you write an example which of this methods above is the right and could you please write how that might look in App.xaml.cs? – billy_56 Nov 20 '17 at 15:18
  • Please see my answer. You can basically just copy the OnStartup method you have posted into the App class in App.xaml.cs. – mm8 Nov 20 '17 at 15:22

1 Answers1

0

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • If I apply this, then in my application if I use numbers for calculation, then in right culture 100.56 would be 10056? because that's what I want to set, if users enters 10000 to show it as 10.000 – billy_56 Nov 20 '17 at 15:26
  • Please ask a new question and provide an example of your issue if you have one. – mm8 Nov 20 '17 at 15:43