Darkproduct
Darkproduct

Reputation: 1237

Get current system language from CultureInfo gives wrong results

I would like to get the current system language from Windows. I found thousands of answers like Get current language in CultureInfo with the suggestion to use System.Globalization and CultureInfo or Thread.CurrentThread... but the solutions don't work.

I tested nearly all solutions on my Windows 7 and also on my Windows 10 and I get always: "en-US".

Here a Screenshot from my Windows 7 Settings: Region and Language Settings from my Windows 7.

I found a working solution but I think System.Globalization is a better approach and i also want "de-DE" and not "de-de" as result.

 [DllImport("kernel32.dll")]
 static extern int GetUserGeoID(int geoId);
 [DllImport("kernel32.dll")]
 static extern int GetGeoInfo(int geoid, int GeoType, StringBuilder lpGeoData, int cchData, int langid);
 [DllImport("kernel32.dll")]
 static extern int GetUserDefaultLCID();

 private const int GEOCLASS_NATION = 16;
 //SYSGEOTYPE
 private const int GEO_NATION = 1;
 private const int GEO_LATITUDE = 2;
 private const int GEO_LONGITUDE = 3;
 private const int GEO_ISO2 = 4;
 private const int GEO_ISO3 = 5;
 private const int GEO_RFC1766 = 6;
 private const int GEO_LCID = 7;
 private const int GEO_FRIENDLYNAME = 8;
 private const int GEO_OFFICIALNAME = 9;
 private const int GEO_TIMEZONES = 10;
 private const int GEO_OFFICIALLANGUAGES = 11;

 public string language;

 // ***********************************************************

 // Code 
 int geoId = GetUserGeoID(GEOCLASS_NATION);
 int lcid = GetUserDefaultLCID();
 StringBuilder bldr = new StringBuilder(50);
 GetGeoInfo(geoId, GEO_RFC1766, bldr, bldr.Capacity, lcid);

 Debug.Log(lcid);

 language = bldr.ToString();

Does anyone know what I have to do, to get CultureInfo to work?

Edit: I forgot to mention I checked also my registry at HKEY_USERS\[Any Folder]\Control Panel\International and all languages are set properly LocaleName = de-DE.

Upvotes: 2

Views: 2998

Answers (1)

Darkproduct
Darkproduct

Reputation: 1237

I solved my question with this post in the unity3D forum. Sorry I'm not pointed out that I use Unity, but I did'nt know that the programm can have such an effect.

[System.Runtime.InteropServices.DllImport("kernel32.dll")]
static extern int GetUserDefaultLCID();

//*************************************************************************

CultureInfo culture = CultureInfo.GetCultureInfo(GetUserDefaultLCID());
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;

language = culture.ToString();

Upvotes: 1

Related Questions