从CultureInfo获取当前系统语言会产生错误的结果

时间:2017-03-08 01:48:46

标签: c# unity3d cultureinfo lcid

我想从Windows获取当前的系统语言。我找到了成千上万的答案,如Get current language in CultureInfo,建议使用System.Globalization和CultureInfo或Thread.CurrentThread ......但解决方案不起作用。

我在Windows 7和Windows 10上测试了几乎所有解决方案,我总是得到:" en-US"。

这是我的Windows 7设置屏幕截图:Region and Language Settings from my Windows 7

我找到了一个有效的解决方案,但我认为System.Globalization是一种更好的方法,我也想要" de-DE"而不是" de-de"结果。

 [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();

有没有人知道我要做什么,让CultureInfo工作?

编辑:我忘了提及我在HKEY_USERS \ [任意文件夹] \控制面板\国际上检查了我的注册表,所有语言都设置正确LocaleName = de-DE。

1 个答案:

答案 0 :(得分:0)

我用post in the unity3D forum解决了我的问题。对不起,我没有指出我使用Unity,但我不知道程序可以产生这样的效果。

[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();