3

I added new Rosource file UserNotification.resx. Then I added two files for localization and named it UserNotification.hr-HR.resx and UserNotification.sl-SI.resx. (followed thread: How to use localization in C#)

Then I changed Windows language's to "si". If i print current culture using System.Globalization.CultureInfo.CurrentCulture.ToString() the output si sl-SI

But, I allways get a string from UserNotification.resx. Seams it doesn't recognizes the localization resource files.

Community
  • 1
  • 1
davor
  • 939
  • 2
  • 14
  • 31
  • Try to change CurrentUICulture in your code and see what happen? – KV Prajapati Oct 15 '11 at 09:50
  • It works if I add this to my App.xaml.cs constructor: Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(System.Globalization.CultureInfo.CurrentCulture.ToString()); But is it the correct way? What if I use multithreading? – davor Oct 15 '11 at 10:24

1 Answers1

5

The problem in .NET localization if you use multithreading is that you need to set culture manualy for every thread that you spawn (in case it is different than windows culture, in case when user can choose desired language). It is not inherited from the main thread.

After you create new thread and before you start it you should set it's Culture to the Culture of main thread (which normaly uses the default culture of the windows).

So, system shows good CurrentCulture. But what does current thread says? Maybe application is started with different culture for some reason so that's why it loads default resources file? Anyway, there is no reason why you shouldn't manualy set current culture. Mostly you need to do that when you want to give user the option to change application's language.

Goran Genter
  • 159
  • 4
  • System.Globalization.CultureInfo.CurrentUICulture.ToString() and Thread.CurrentThread.CurrentUICulture.ToString() returns same output: en-US, but System.Globalization.CultureInfo.CurrentCulture.ToString() returns hr-HR (== Windows -> Control Panel -> Region and language format property). So I must set CurrentThread.CurrentUICulture, because there isn't setter for System.Globalization.CultureInfo.CurrentUICulture? – davor Oct 15 '11 at 16:12
  • Yes, you must set CurrentThread.CurrentUICulture and CurrentThread.CurrentCulture to what you want. You are getting wrong System.Globalization.CultureInfo, english one, that is why your app is using default resource file. Like MSDN says for Thread.CurrentUICulture - "Gets or sets the current culture used by the Resource Manager to look up culture-specific resources at run time.". – Goran Genter Oct 15 '11 at 16:39