Get current language in CultureInfo
Asked Answered
T

6

75

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.

Thornberry answered 17/11, 2010 at 19:20 Comment(3)
Your answer might be this one #329533Persecute
If you're using .NET resource files (the .resx ones), then the system handles this for you.Elin
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.Thornberry
F
98

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

CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;

Is that what you're looking for?

Finance answered 17/11, 2010 at 19:23 Comment(2)
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.Mohammedanism
User can have system language set that has nothing common with culture setting... as those are 2 completely different settingsAddressee
L
36

This is what i used:

var culture = System.Globalization.CultureInfo.CurrentCulture;

and it's working :)

Limpopo answered 7/10, 2014 at 8:12 Comment(0)
P
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;
Pharmacist answered 26/2, 2014 at 9:19 Comment(0)
I
7

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

Immerse answered 15/2, 2016 at 18:23 Comment(0)
M
6

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

System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
Melitamelitopol answered 10/11, 2017 at 16:49 Comment(0)
A
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?)

Addressee answered 24/12, 2019 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.