问题描述
与的CurrentCulture
, InvariantCulture的
,的CurrentUICulture $ C $有什么区别C>和
InstalledUICulture
从 System.Globalization.CultureInfo
?
推荐答案
我会尽量给一点比this 之一。
I would try to give a bit more insightful answer than this one.
的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);
要注意的一件重要的事情是,你不应该使用浮动
和双
存储在货币相关信息第一名(十进制
是正确的选择)。
其他常见的用例的CurrentCulture
是语言环境感知的解析。您的应用程序应该的总是的允许用户提供输入的区域格式:
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的,另一方面应采用可靠地转换上面的文字重新presentation提到的类之一。所以,如果你想例如发送的DateTime
,浮动
,双
或通过网络进行类似物体,将其存储到数据库或某种文本文件(包括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可互换的日期和放大器;时间格式。出于某种原因,微软甚至没有提供这样的模式(最接近的是可排序的模式 - 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
其实就是要求这里省略该参数可能会导致严重的错误。我曾经修复法国操作系统的缺陷。在code是这样的:
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和版本
类要求文化不变的输入。
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
是去好方法(前提是你真的读网络浏览器这个属性)。
同样的,如果你想实现语言切换机制,的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 被连接到默认的操作系统用户界面语言环境。作为MSDN说:
InstalledUICulture is wired to Default OS UI Locale. As MSDN says:
此属性是GetSystemDefaultUILanguage的Windows API中的等价物。
要真正理解这个属性是,我们需要挖掘到一定的理论。有不同的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的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!