73

How to identify the operating system's language using CultureInfo? E.g. if the language in Windows is set to French, I need to identify French and load the fr resource files data.

Palec
  • 12,743
  • 8
  • 69
  • 138
Sharpeye500
  • 8,775
  • 25
  • 95
  • 143
  • 4
    If you're using .NET resource files (the `.resx` ones), then the system handles this for you. – Travis Gockel Nov 17 '10 at 19:26
  • Yes i know, but i didn't add mandarin resource file & the computer was set to mandarin language as its default language, so i am looking to get right.Thanks. – Sharpeye500 Nov 17 '10 at 19:31
  • 1
    Your answer might be this one http://stackoverflow.com/questions/329033/what-is-the-difference-between-currentculture-and-currentuiculture-properties-of – Raymund Nov 17 '10 at 19:24

6 Answers6

97

I think something like this would give you the current CultureInfo:

CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;

Is that what you're looking for?

Brosto
  • 4,445
  • 2
  • 34
  • 51
  • 6
    I would use CultureInfo.CurrentCulture which does the same, but is shorter to write. Unfortunately neither CurrentCulture not CurrentUICulture returns OS language. First returns current formatting setting, second returns user's preferred User Interface language. – Paweł Dyda Nov 17 '10 at 21:22
  • User can have system language set that has nothing common with culture setting... as those are 2 completely different settings – RTDev Dec 24 '19 at 12:12
34

This is what i used:

var culture = System.Globalization.CultureInfo.CurrentCulture;

and it's working :)

smukamuka
  • 1,442
  • 1
  • 15
  • 23
11

Current system language is retrieved using :

  CultureInfo.InstalledUICulture

"Gets the CultureInfo that represents the culture installed with the operating system."

InstalledUICulture

To set it as default language for thread use :

   System.Globalization.CultureInfo.DefaultThreadCurrentCulture=CultureInfo.InstalledUICulture;
DominicusPlatus
  • 139
  • 1
  • 7
6

I tried {CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;} but it didn`t work for me, since my UI culture was different from my number/currency culture. So I suggest you to use:

CultureInfo currentCulture = Thread.CurrentThread.CurrentUICulture;

This will give you the culture your UI is (texts on windows, message boxes, etc).

Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
6

To get the 2 chars ISO 639-1 language identifier use:

System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
primehunter
  • 280
  • 4
  • 10
2
Windows.System.UserProfile.GlobalizationPreferences.Languages[0]

This is the correct way to obtain the currently set system language. System language setting is completely different than culture setting from which you all want to get the language.

For example: User may use "en-GB" language along with "en-US" culture at the same time. Using CurrentCulture and other cultures you will get "en-US", hope you get the difference (that may be innoticable with GB-US, but with other languages?)

RTDev
  • 866
  • 7
  • 17