0

I have noticed people use this code to localize

Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR");
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");

Could anyone tell me what CurrentUICulture and CurrentCulture are? Why do we need to assign same value to both

What will happen if I assign the value CultureInfo("fr-FR"); to only CurrentUICulture or CurrentCulture?

I would really like to know in detail what CurrentUICulture and CurrentCulture does?

Suppose client pc language setting could be German or French etc., but I want to develop my application in such a way that whatever language setting is there, my apps will work in that pc with their language setting. I mean that all the controls will show the text in German or French. I do not want to hard code the culture this way CultureInfo("fr-FR")

I would rather the user's language setting dictate the language of my controls. How would I go about doing this?

Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82
Thomas
  • 33,544
  • 126
  • 357
  • 626

1 Answers1

0

It's pretty simple.

CurrentCulture is the culture assigned for thread to be used in various culture-specific methods. To example, ToString() one.

CurrentUICulture is

used by the Resource Manager to look up culture-specific resources at run time.

By setting both you ensure what all ToString() works correctly and your form resources are loaded from appropriate satellite.

Regarding usage, you may think to provide user with option to change language. To example, I have native German windows, but I set English culture in it (date format and default culture for console applications), while I speak Russian. Which language do you think I will chose when using your software? =P

By default, either take language chosen in installer (if you have installer in multiple languages and options to change it there) or current Windows user language preferences.


One possible scenario:

Preconditions: application is already localized, satellites are there (if not, read this). I don't like satellites myself, but they are easy to start with.

When application starts, you create a list of available languages (either statically or by enumerating satellites).

string[] Languages = new[] {"en", "fr", "de", "ru"};

Then, if application start for the first time (to example, you have string language setting and it's by default null or "", this would be a flag what no language is selected yet), you detect default os language

var language = CultureInfo.InstalledUICulture.TwoLetterISOLanguageName; // to ex, "fr"

and if it's in your list, then you set

Thread.CurrentThread.CurrentUICulture =
    Thread.CurrentThread.CurrentCulture = new new CultureInfo(language);

Otherwise, if you don't have this language, then use "default" language (to example, "en"). If its done at at the start of your application, then any further constructed and used window will use proper resources.

User should be able to change language. Give him possibility to chose one of available Languages. In winforms you will have to reload forms after change culture. Easiest solution would be to simply tell user what "software restart is required".

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • so guide me. suppose i have win application when it will run in user pc then it will show everything as per user pc OS language setting but there will another provision by which user can change language from German to French. looking for guidance in terms of code. how to develop the routine for changing language at run time as user user pc language setting and also what code i need to write as a result user can change language of my apps from German to French....guidance in details will be good help. thanks – Thomas Aug 14 '14 at 11:15
  • See edit. If you mean multiple accounts on same pc, then your application simply have to implement setting "language" per user. You can, to example, use `Registry` ability to define user settings. Or it could be a configuration file in current user folder. – Sinatr Aug 14 '14 at 12:18