.net CurrentCulture、InvariantCulture、CurrentUICulture 和 InstalledUICulture 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5060446/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:17:46  来源:igfitidea点击:

Difference between CurrentCulture, InvariantCulture, CurrentUICulture and InstalledUICulture

.netinternationalizationglobalization

提问by Diego Pacheco

What is the difference between CurrentCulture, InvariantCulture, CurrentUICultureand InstalledUICulturefrom System.Globalization.CultureInfo?

是什么区别CurrentCultureInvariantCultureCurrentUICultureInstalledUICultureSystem.Globalization.CultureInfo

回答by Pawe? Dyda

I would try to give a bit more insightful answer than this one.

我会尝试给出比这个更有见地的答案。

CurrentCultureshould be used for formatting. That is, Numbers, Currencies, Percentages, Dates and Times should alwaysbe formatted with this culture before displaying them to user. Few examples here:

CurrentCulture应该用于格式化。也就是说,在向用户显示数字、货币、百分比、日期和时间之前,应始终使用这种文化进行格式化。这里的几个例子:

const string CURRENCY_FORMAT = "c";
const string PERCENTAGE_FORMAT = "p";

DateTime now = DateTime.UtcNow; // all dates should be kept in UTC internally
// convert time to local and format appropriately for end user
dateLabel.Text = now.ToLocalTime().ToString(CultureInfo.CurrentCulture);

float someFloat = 12.3456f;
// yields 12,3456 for pl-PL Culture
floatLabel.Text = someFloat.ToString(CultureInfo.CurrentCulture);
// yields 12,35 z? for pl-PL Culture - rounding takes place!
currencyLabel.Text = someFloat.ToString(CURRENCY_FORMAT, CultureInfo.CurrentCulture);
// yields 1234,56% for pl-PL Culture - 1.0f is 100%
percentageLabel.Text = someFloat.ToString(PERCENTAGE_FORMAT, CultureInfo.CurrentCulture);

One important thing to note is that you should never use floatnor doublefor storing currency related info in the first place (decimalis the right choice).
The other common use case for CurrentCultureis Locale-aware parsing. Your application should alwaysallow users to provide input in their regional format:

需要注意的一件重要事情是,您不应该首先使用floatdouble存储与货币相关的信息(这decimal是正确的选择)。
另一个常见的用例CurrentCulture是 Locale-aware 解析。您的应用程序应始终允许用户以其区域格式提供输入:

float parsedFloat;
if (float.TryParse(inputBox.Text, NumberStyles.Float, CultureInfo.CurrentCulture, out parsedFloat))
{
    MessageBox.Show(parsedFloat.ToString(CultureInfo.CurrentCulture), "Success at last!");
}

Please note that I always provide IFormatProviderparameter, although it is implied and assumed to be CultureInfo.CurrentCulture. The reason for it is, I want to communicate with fellow developers: this is something that will be displayed to end users. This is one of the reasons why FxCop treats omitting this parameter as error.

请注意,我总是提供IFormatProvider参数,尽管它是隐含的并假定为CultureInfo.CurrentCulture. 这样做的原因是,我想与其他开发人员交流:这是将向最终用户显示的内容。这就是 FxCop 将忽略此参数视为错误的原因之一。



InvariantCultureon the other hand should be used to convert reliably one of the classes mentioned above to its textual representation. So if you want to for example transmit DateTime, float, doubleor similar object via network, store it to database or some kind of text file (including XML), you should alwaysuse InvariantCulture:

另一方面,InvariantCulture应用于将上述类中的一个可靠地转换为其文本表示。所以,如果你想例如发射DateTimefloatdouble或通过网络相似的对象,将其存储到数据库或某种形式的文本文件(包括XML)的,你应该始终使用InvariantCulture

float someFloat = 1234.56f;
// yields 1234.56
string internalFloat = someFloat.ToString(CultureInfo.InvariantCulture);
DateTime now = DateTime.UtcNow;
// yields something like 04/16/2011 19:02:46
string internalDateAndTime = now.ToString(CultureInfo.InvariantCulture);

A thing to note here is that date/time format offered by InvariantCultureis actually exactly the same as en-US. Although it is reliable, it is not exactly correct. What really should be used is ISO8601 Interchangeable Date & Time Format. For some reason Microsoft does not even provide such pattern (the closest ones are Sortable pattern - "s" and Universal pattern - "u" which only resembles ISO8601 format), you would need to create your own like this:

这里需要注意的一点是,提供的日期/时间格式InvariantCulture实际上与 en-US 完全相同。虽然它是可靠的,但它并不完全正确。真正应该使用的是ISO8601 Interchangeable Date & Time Format。出于某种原因,微软甚至没有提供这样的模式(最接近的是可排序模式 - “s”和通用模式 - “u”,它只类似于 ISO8601 格式),你需要像这样创建自己的:

const string iso8601Pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
string iso8601Formatted = now.ToString(iso8601Pattern);

Quite important side note: IFormatProvideris actually requiredhere as omitting this parameter may result in serious errors. I once had to fix a defect on French OS. The code was like this:

非常重要的旁注:这里IFormatProvider实际上是必需的,因为省略此参数可能会导致严重错误。我曾经不得不修复法语操作系统上的一个缺陷。代码是这样的:

float dbVersion = // some kind of logic that took float from database
string versionString = dbVersion.ToString(); // culture-aware formatting used
Version ver = new Version(versionString); // exception thrown here

The reason was very simple: float formatted on French OS (with French regional formatting) was formatted to something like 10,5 and Versionclass requires Culture-invariant input.

原因很简单:在法语操作系统上格式化的浮点数(使用法语区域格式)被格式化为类似 10,5 的格式,并且Version类需要文化不变的输入。



CurrentUICultureis responsible for loading appropriate translatable resources for your application. That is, it should be use for displaying correct text messages, colors and images.
In Asp.Net application if you want to for some reason implement CSS Localization Mechanism (ability to override CSS definitions per language), CurrentUICultureis good way to go (provided that you actually read this property from web browser).
Likewise if you want to implement language switching mechanism, CurrentUICultureis the one that should be overridden.

CurrentUICulture负责为您的应用程序加载适当的可翻译资源。也就是说,它应该用于显示正确的文本消息、颜色和图像。
在 Asp.Net 应用程序中,如果您出于某种原因想要实现 CSS 本地化机制(能够覆盖每种语言的 CSS 定义),这CurrentUICulture是一个好方法(前提是您实际上是从 Web 浏览器读取此属性的)。
同样,如果您想实现语言切换机制,CurrentUICulture则应该覆盖它。



InstalledUICultureis wired to Default OS UI Locale. As MSDNsays:

InstalledUICulture连接到默认操作系统 UI 区域设置。正如MSDN所说:

This property is the equivalent of GetSystemDefaultUILanguage in the Windows API.

此属性等效于 Windows API 中的 GetSystemDefaultUILanguage。

To actually understand what this property is, we need to dig into some theory. There are different Windows product lines:

要真正理解这个属性是什么,我们需要深入研究一些理论。有不同的 Windows 产品线:

  • single language (i.e. English, French, German, Japanese, etc.)
  • MUI (that is Multilingual User Interface - English base OS and Language Packs)
  • 单一语言(即英语、法语、德语、日语等)
  • MUI(即多语言用户界面 - 英文基础操作系统和语言包)

For single language product lines, InstalledUICulture will always return Operating System language, whereas for MUI it should always return English (United States) aka en-US. Is it useful? I don't know, I never needed such an information. And personally I haven't seen program that took advantage of this property.

对于单语言产品线,InstalledUICulture 将始终返回操作系统语言,而对于 MUI,它应始终返回英语(美国)又名 en-US。有用吗?我不知道,我从来不需要这样的信息。我个人还没有看到利用此属性的程序。

回答by Shahin

Taken from this answer:

取自这个答案

CurrentCulture is the .NET representation of the default user locale of the system. This controls default number and date formatting and the like.

CurrentUICulture refers to the default user interface language, a setting introduced in Windows 2000. This is primarily regarding the UI localization/translation part of your app.

Whatever regional options the system is configured to have will be the "Current" values in your .NET app.

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 CurrentCulture would be German, but I also prefer all my applications in English, so the CurrentUICulture would be English.

CurrentCulture 是系统默认用户语言环境的 .NET 表示。这控制默认数字和日期格式等。

CurrentUICulture 指的是默认用户界面语言,这是 Windows 2000 中引入的设置。这主要与应用程序的 UI 本地化/翻译部分有关。

系统配置为具有的任何区域选项都将是 .NET 应用程序中的“当前”值。

很多时候它们都是一样的。但在我的系统上,它们会有所不同:我更喜欢德语格式的数字和日期,因此 CurrentCulture 将是德语,但我也更喜欢我的所有应用程序都是英语,因此 CurrentUICulture 将是英语。