从 C# 中检测 os 语言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/479300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
detect os language from c#
提问by crauscher
Is there a way to detect the Language of the OS from within a c# class?
有没有办法从 ac# 类中检测操作系统的语言?
采纳答案by Oliver Friedrich
With the System.Globalization.CultureInfo
class you can determine what you want.
通过System.Globalization.CultureInfo
课程,您可以确定自己想要什么。
With CultureInfo.CurrentCulture
you get the system set culture, with CultureInfo.CurrentUICulture
you get the user set culture.
随着CultureInfo.CurrentCulture
您获得系统集文化,CultureInfo.CurrentUICulture
您将获得用户集文化。
回答by Bevan
Do you mean whether the machine is configured (e.g.) with English, French or Japanese?
您的意思是机器是否配置(例如)英语、法语或日语?
Have a look at the CultureInfo class - particularly CurrentCulture, which is initialised from the OS current regional settings.
查看 CultureInfo 类 - 特别是 CurrentCulture,它是从操作系统当前区域设置初始化的。
回答by Bevan
System.Threading.Thread.CurrentThread.CurrentCulture
System.Threading.Thread.CurrentThread.CurrentCulture
回答by Mihai Drebot
Unfortunately, the previous answers are not 100% correct.
不幸的是,之前的答案并非 100% 正确。
The CurrentCulture
is the culture info of the running thread, and it is used for operations that need to know the current culture, but not do display anything. CurrentUICulture
is used to format the display, such as correct display of the DateTime
.
Because you might change the current thread Culture
or UICulture
, if you want to know what the OS CultureInfo
actually is, use CultureInfo.InstalledUICulture
.
的CurrentCulture
是正在运行的线程的文化信息,它是用于需要知道当前的文化,但不做任何显示操作。CurrentUICulture
用于格式化显示,例如正确显示DateTime
. 因为您可能会更改当前线程,Culture
或者UICulture
,如果您想知道操作系统的CultureInfo
实际情况,请使用CultureInfo.InstalledUICulture
.
Also, there is another question about this subject (more recent than this one) with a detailed answer:
此外,还有关于这个主题的另一个问题(比这个问题更近),有详细的答案:
回答by Lost
"System.Globalization.CultureInfo.CurrentUICulture.DisplayName"
. This is exactly what you want.
"System.Globalization.CultureInfo.CurrentUICulture.DisplayName"
. 这正是您想要的。
回答by Mark Green
Means that if the system locale on Region and Language, you can use Win32 API function GetSystemDefaultLCID. The signiture is as follow:
意味着如果系统区域设置在Region 和Language 上,则可以使用Win32 API 函数GetSystemDefaultLCID。签名如下:
[DllImport("kernel32.dll")]
static extern uint GetSystemDefaultLCID();
GetSystemDefaultLCID function returns the LCID. It can map language string from the folowing table. Locale IDs Assigned by Microsoft
GetSystemDefaultLCID 函数返回 LCID。它可以映射下表中的语言字符串。 Microsoft 分配的区域设置 ID
回答by Mike Parkhill
All of the answers above seem to only get you the OS installed culture (at best). I ran into an issue where I needed the actual display language being used in Windows. i.e. the user installed the default en-US Windows install, but then added a language pack for German (de-DE) and set that as their display language.
上面的所有答案似乎只能让您了解已安装操作系统的文化(充其量)。我遇到了一个问题,我需要在 Windows 中使用实际的显示语言。即用户安装了默认的 en-US Windows 安装,但随后添加了德语 (de-DE) 语言包并将其设置为显示语言。
To get that, I used System.Windows.Input.InputLanguageManager.Current.CurrentInputLanguage
为此,我使用了System.Windows.Input.InputLanguageManager.Current.CurrentInputLanguage