.net 文化和 UICulture 有什么区别?

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

What is the difference between Culture and UICulture?

.netcultureuiculture

提问by Niklas

Could someone give me a bit more information on the difference between Cultureand UICulturewithin the .NET framework? What they do and when to use what?

有人能给我更多关于.NET 框架之间CultureUICulture内部差异的信息吗?他们做什么以及何时使用什么?

回答by dee-see

Cultureaffects how culture-dependent data (dates, currencies, numbers and so on) is presented. Here are a few examples:

Culture影响文化相关数据(日期、货币、数字等)的呈现方式。这里有一些例子:

var date = new DateTime(2000, 1, 2);
var number = 12345.6789;

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
Console.WriteLine(date); // 02.01.2000 00:00:00
Console.WriteLine(number.ToString("C")); // 12.345,68 

Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-CA");
Console.WriteLine(date); // 2000-01-02 00:00:00
Console.WriteLine(number.ToString("C")); // 12 345,68 $

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Console.WriteLine(date); // 1/2/2000 12:00:00 AM
Console.WriteLine(number.ToString("C")); // ,345.68

Culture also affects parsing of user input in the same way:

文化也以同样的方式影响用户输入的解析:

const string numberString = "12.345,68";
decimal money;

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
money = decimal.Parse(numberString); // OK!

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
money = decimal.Parse(numberString); // FormatException is thrown, TryParse would return false

Beware of cases where the parsing succeedsbut the result is not what you would expect it to be.

请注意解析成功但结果与您预期不同的情况。

const string numberString = "12.345";
decimal money;

Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
money = decimal.Parse(numberString); // 12345

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
money = decimal.Parse(numberString); // 12.345, where the . is a decimal point

UICultureaffects which resource file (Resources.lang.resx) is going to be loaded to by your application.

UICulture它会影响资源文件(参考资料。的.resx)将是由你的应用程序加载到。

So to load German resources (presumably localized text) you would set UICultureto the German culture and to display German formatting (without any impact on which resources are loaded) you would set Culture.

因此,要加载德语资源(大概是本地化文本),您需要设置UICulture为德语文化并显示德语格式(对加载的资源没有任何影响),您需要设置Culture.

回答by Niklas

Culture and UICulture

Culture and UICulture

Values are pairs of two-letter strings, the first is for defining language and the second for defining the region. Example:

值是成对的双字母字符串,第一个用于定义语言,第二个用于定义区域。例子:

en-GBhere enrepresents Englishand GBrepresents Great Briton

en-GB这里en代表EnglishGB代表Great Briton

en-UShere enrepresents Englishand USrepresents United States

en-US这里en代表EnglishUS代表United States

Use Culturefor Culture dependent functions like date, time. and UICultureis for correct resource file loading.

使用Culture像日期,时间文化相关功能。并且UICulture用于正确加载资源文件。

回答by Hooman

Just a small matter to consider in addition to @Vache's awesome explanation: You can set both UICulture and Culture at (page level and application level).

除了@Vache 很棒的解释之外,还有一个小问题需要考虑:您可以在(页面级别和应用程序级别)设置 UICulture 和 Culture。

In order to set them at application level, simply add globalization session in web.config

为了在应用程序级别设置它们,只需在 web.config 中添加全球化会话

e.g. <globalization uiCulture="es" culture="es-MX" />

例如 <globalization uiCulture="es" culture="es-MX" />

And to set them at the page level, which is good to add on a specific (individual) page, set the Culture and UICulture attributes within @ page directive

并在页面级别设置它们,这有助于在特定(个人)页面上添加,在 @ page 指令中设置 Culture 和 UICulture 属性

e.g. <%@ Page UICulture="es" Culture="es-MX" %>

例如 <%@ Page UICulture="es" Culture="es-MX" %>

回答by Mohammed Nazer

The UICulture property might change for each Web browser, whereas the Culture stays constant.

UICulture 属性可能会因每个 Web 浏览器而改变,而 Culture 保持不变。

The Culture value can be set to specific cultures only, such as en-US or en-GB. This prevents the requirement to identify the correct currency symbol to use for en, where en-US and en-GB have different currency symbols. Users can set the UI culture and culture in their browsers.

文化值只能设置为特定的文化,例如 en-US 或 en-GB。这阻止了识别用于 en 的正确货币符号的要求,其中 en-US 和 en-GB 具有不同的货币符号。用户可以在浏览器中设置 UI 文化和文化。