.NET 中 CultureInfo 的 CurrentCulture 和 CurrentUICulture 属性有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/329033/
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
What is the difference between CurrentCulture and CurrentUICulture properties of CultureInfo in .NET?
提问by splattne
In .NET there is the CultureInfoclass in the System.Globalizationnamespace. It has two similar properties both returning values of the CultureInfotype: CurrentCultureand CurrentUICulture.
在 .NETCultureInfo中,System.Globalization命名空间中有类。它有两个相似的属性,都返回CultureInfo类型的值:CurrentCulture和CurrentUICulture。
What is the difference between them?
它们之间有什么区别?
Which one should I use when and why?
我应该在何时以及为什么使用哪一种?
采纳答案by Tomalak
CurrentCultureis the .NET representation of the default user locale of the system. This controls default number and date formatting and the like.
CurrentCulture是系统默认用户语言环境的 .NET 表示。这控制默认数字和日期格式等。
CurrentUICulturerefers to the default user interface language, a setting introduced in Windows 2000. This is primarily regarding the UI localization/translation part of your app.
CurrentUICulture指的是默认用户界面语言,Windows 2000 中引入的设置。这主要与应用程序的 UI 本地化/翻译部分有关。
Whatever regional options the system is configured to have will be the "Current" values in your .NET app.
系统配置为具有的任何区域选项都将是 .NET 应用程序中的“当前”值。
Often times they are both the same. But on my system they would be different: I prefer my numbers and dates in the German format, so the CurrentCulturewould be German, but I also prefer all my applications in English, so the CurrentUICulturewould be English.
很多时候它们都是一样的。但在我的系统上,它们会有所不同:我更喜欢德语格式的数字和日期,所以CurrentCulture应该是德语,但我也更喜欢我的所有应用程序都是英语,所以CurrentUICulture应该是英语。
There is a nice article on the topic: Sorting it all Out: Why we have both CurrentCulture and CurrentUICulture
有一篇关于该主题的不错的文章:整理所有内容:为什么我们同时拥有 CurrentCulture 和 CurrentUICulture
回答by foxontherock
This is a simple trick I use to remember which one to use:
这是我用来记住使用哪个的一个简单技巧:
(date, currency, double).tostring = CurrentCulture
resource.fr-CA.resx file = currentUICulture
回答by Ivaylo Slavov
A good way to make a difference in addition to the nice explanations done by fellow users, and an important aspect in web application development is the following:
除了其他用户所做的很好的解释之外,还有一个很好的方法来有所作为,并且 Web 应用程序开发中的一个重要方面如下:
CurrentCulturerepresents the setup of the web server. For example, if your ASP.NET web application is hosted in Germany, the value ofCutlureInfo.CurrentCulturewould most probably bede-DE. Thus, the default.ToString()formatting forIFormattabletypes would use the default German formattings, or the ones which have been set up on the server OS as defaults.CurrentUICulturecan be captured from the user agent, and may represent the user interface culture of the client connecting to the website. For example, if you load that website from Russia, your local settings are set to use Russian language, and your user agent sends your locale settings to the server (Opera and IE do this automatically, not sure for Chrome and FireFox), theCurrenUICulturewould representru-RU. This will cause any resources like localized strings retrieved via ResourceManager or localization expressions in ASP.NET aspx/ascx files to be in Russian (if translations are available).
CurrentCulture表示 Web 服务器的设置。例如,如果您的 ASP.NET Web 应用程序托管在德国,则 的值CutlureInfo.CurrentCulture很可能是de-DE. 因此,类型的默认.ToString()格式IFormattable将使用默认的德语格式,或已在服务器操作系统上设置为默认值的格式。CurrentUICulture可以从用户代理中捕获,并且可以代表连接到网站的客户端的用户界面文化。例如,如果您加载来自俄罗斯的那个网站,你的本地设置被设置为使用俄语,和您的用户代理发送你的本地设置服务器(Opera和IE自动完成此操作,不知道对Chrome和Firefox)中,CurrenUICulture将代表ru-RU。这将导致任何资源,如通过 ResourceManager 检索的本地化字符串或 ASP.NET aspx/ascx 文件中的本地化表达式为俄语(如果翻译可用)。
回答by Matas Vaitkevicius
Differences:
区别:
CurrentCultureis for formatting of dates and currency whileCurrentUICulturegoes with language/translations. It will be used byResourceManagerto look up resources by culture.- Namespace of
CurrentCultureclass is inSystem.GlobalizationwhileCurrentUICulturecomes fromSystem.Threading. CurrentCultureis persisted across different requests in the session whileCurrentUICultureneeds to be set with every request.
CurrentCulture用于格式化日期和货币,同时CurrentUICulture使用语言/翻译。它将被用于ResourceManager按文化查找资源。CurrentCulture类的命名空间 在System.GlobalizationwhileCurrentUICulture来自System.Threading.CurrentCulture在会话中的不同请求中持久化,同时CurrentUICulture需要为每个请求设置。
Likeness:
肖像:
They both are System.Globalization.CultureInfoinstances.
回答by Sebris87
It is worth noting that the CurrentUICulturesupports non-country specific locales such as 'en' (Neutral cultures) whereas CurrentCultureONLY supports country specific locales such as 'en-GB'. Setting CurrentCultureto a neutral culture will throw an ArgumentException.
值得注意的是,CurrentUICulture支持非国家特定语言环境,例如“en”(中性文化),而CurrentCulture仅支持国家特定语言环境,例如“en-GB”。设置CurrentCulture为中性文化会抛出一个ArgumentException.
I assume that this is because formats such as dates and currency are more strongly linked to the country itself, but the language displayed is often interchangeable between countries.
我认为这是因为日期和货币等格式与国家本身的联系更紧密,但显示的语言在国家之间通常可以互换。

