gpt4 book ai didi

.net - 何时使用 CultureInfo.GetCultureInfo(String) 或 CultureInfo.CreateSpecificCulture(String)

转载 作者:行者123 更新时间:2023-12-03 07:34:53 24 4
gpt4 key购买 nike

我什么时候应该调用 CultureInfo。 CreateSpecificCulture (字符串)而不是 CultureInfo。 GetCultureInfo (字符串)。 MSDN文档不是很清楚。

还有没有一种方法可以更便宜地检查文化名称是否有效?

认为如果您将“en”而不是“en-GB”传递给 CultureInfo.CreateSpecificCulture(String),您会收到错误,但 CultureInfo.GetCultureInfo(String) 并不介意。例如。如果您只传递一种语言,CultureInfo.GetCultureInfo(String) 就可以应付。 但是我对此仍然不清楚。

最佳答案

文化分为三组:不变文化、中性文化和特定文化。文化en是一种中立文化,而文化en-US是一种特定的文化。

GetCultureInfo将为您提供您所要求的任何文化,因此,如果您要求中性文化,您也会获得中性文化,例如 en .

CreateSpecificCulture将从中立文化中创建特定文化,因此如果您调用 CreateSpecificCulture("en") CultureInfo返回的是en-US文化。我不确定中性文化如何映射到特定文化,但 BCL 或 Windows 内必须有一些表包含这些映射并决定它是 en-US而不是en-GB被返回。指定特定区域性作为 CreateSpecificCulture 的参数会给你具体的 CultureInfo就像 GetCultureInfo确实如此。

但是所创建的特定文化有一个有点令人惊讶的特征:

If the culture identifier of the specific culture returned by this method matches the culture identifier of the current Windows culture, this method creates a CultureInfo object that uses the Windows culture overrides. The overrides include user settings for the properties of the DateTimeFormatInfo object returned by the DateTimeFormat property and the NumberFormatInfo object returned by the NumberFormat property.

这意味着如果 CreateSpecificCulture 返回特定的区域性与用户在 Windows 的区域和语言控制面板中选择的文化相匹配,然后对该文化的任何用户自定义都包含在 CultureInfo 中。回。例如。用户可以更改数字中使用的长日期模式或小数分隔符。另一种思考这个问题的方法是,当 CreateSpecificCulture返回与 CurrentCulture 名称匹配的区域性它实际上会返回这种文化,包括任何用户自定义。

据我所知GetCultureInfo没有此属性,并且将始终返回未修改的 CultureInfo .

要检查文化是否有效,我将使用 GetCultureInfo .

关于.net - 何时使用 CultureInfo.GetCultureInfo(String) 或 CultureInfo.CreateSpecificCulture(String),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/986754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com