本文介绍了CurrentCulture、InvariantCulture、CurrentUICulture 和 InstalledUICulture 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CurrentCultureInvariantCultureCurrentUICultureSystem.Globalization中的InstalledUICulture有什么区别.CultureInfo?

What is the difference between CurrentCulture, InvariantCulture, CurrentUICulture and InstalledUICulture from System.Globalization.CultureInfo?

推荐答案

我会尝试给出比 这个.

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

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

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

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

One important thing to note is that you should never use float nor double for storing currency related info in the first place (decimal is the right choice).
The other common use case for CurrentCulture is Locale-aware parsing. Your application should always allow users to provide input in their regional format:

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

请注意,我总是提供 IFormatProvider 参数,尽管它是隐含的并假定为 CultureInfo.CurrentCulture.原因是,我想和其他开发者交流:这是要展示给最终用户的东西.这也是 FxCop 将省略此参数视为错误的原因之一.

Please note that I always provide IFormatProvider parameter, 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.

InvariantCulture 应该用于可靠地将上述类之一转换为其文本表示形式.因此,如果您想通过网络传输 DateTimefloatdouble 或类似对象,请将其存储到数据库或某种文本文件中(包括 XML),您应该始终使用 InvariantCulture:

InvariantCulture on 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, double or similar object via network, store it to database or some kind of text file (including XML), you should always use 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);

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

A thing to note here is that date/time format offered by InvariantCulture is 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:

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

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

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

原因很简单:在法语操作系统上格式化的浮点数(使用法语区域格式)被格式化为 10,5 之类的东西,而 Version 类需要 Culture-invariant 输入.

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

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

CurrentUICulture is 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), CurrentUICulture is good way to go (provided that you actually read this property from web browser).
Likewise if you want to implement language switching mechanism, CurrentUICulture is the one that should be overridden.

InstalledUICulture 已连接到默认 OS UI 区域设置.正如 MSDN 所说:

InstalledUICulture is wired to Default OS UI Locale. As MSDN says:

此属性相当于 Windows API 中的 GetSystemDefaultUILanguage.

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

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

  • 单一语言(即英语、法语、德语、日语等)
  • MUI(即多语言用户界面 - 英语基础操作系统和语言包)

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

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.

这篇关于CurrentCulture、InvariantCulture、CurrentUICulture 和 InstalledUICulture 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 05:47