Culture-Specific Formatting(特定于区域性的格式设置)
In .NET applications, formatting options for currency, numbers, and dates are not related to the application’s UI language specified as the Thread.CurrentUICulture property value. The formatting options that are set in the current user’s operating system or passed by the Internet browser are used. You can also specify a specific formatting culture or customize the default settings.
在.NET应用程序中,货币、数字和日期的格式设置选项与通过Thread.CurrentUICulture属性值指定的应用程序UI语言无关。会使用当前用户操作系统中设置的或由互联网浏览器传递的格式设置选项。您也可以指定特定的格式区域性或自定义默认设置。
Change the Formatting Culture(更改格式区域性)
You can set another formatting culture using the XafApplication.SetFormattingCulture method, which changes the Thread.CurrentCulture value. The following code demonstrates how to do this in a Windows Forms application project’s Program.cs file:
您可以使用XafApplication.SetFormattingCulture方法设置另一种格式区域性,该方法会更改Thread.CurrentCulture的值。以下代码演示了如何在Windows Forms应用程序项目的Program.cs文件中执行此操作:
C#
static void Main() {
// ...
MySolutionWindowsFormsApplication winApplication =
new MySolutionWindowsFormsApplication();
winApplication.SetFormattingCulture("de");
// ...
}
The following image illustrates the result.
下图展示了结果。
Similarly, you can change the formatting culture in an ASP.NET Web Forms application project’s Global.asax.cs file:
同样,您可以在ASP.NET Web Forms应用程序项目的Global.asax.cs文件中更改格式区域性:
C#
protected void Session_Start(Object sender, EventArgs e) {
WebApplication.SetInstance(Session, new MySolutionAspNetApplication());
WebApplication.Instance.SetFormattingCulture("de");
// ...
}
Note
In .NET 5, the libraries used for globalization functionality have been changed. Refer to the following topic for details: Globalization APIs use ICU libraries on Windows.
在.NET 5中,用于全球化功能的库已更改。有关详情,请参考以下主题:Windows上的全球化API使用ICU库。
If a current thread’s culture is set to a culture that includes only the language and not the country (for example, “de” or “en”), the currency symbol renders as an international currency symbol (¤), for example: 100.00 ¤.
如果当前线程的区域性设置为仅包含语言而不包含国家/地区的区域性(例如“de”或“en”),则货币符号会显示为国际货币符号(¤),例如:100.00 ¤。
Refer to the following topics for details:
有关详情,请参考以下主题:
- ICU Localization - Currency symbol.(ICU本地化 - 货币符号。)
- *CurrencySymbol. *
Tip
For ASP.NET Core Blazor UI applications
对于ASP.NET Core Blazor UI应用程序
Refer to the following articles for information on how to change the language at runtime:
有关如何在运行时更改语言的信息,请参考以下文章:
- Localize an XAF Application (.NET)(本地化XAF应用程序(.NET))
- *IXafCultureInfoService *
Override the Default Formatting Options(覆盖默认格式设置选项)
You can override the default formatting options in the XafApplication.CustomizeFormattingCulture event handler. The following code demonstrates how to do this in a Windows Forms application project’s Program.cs file:
您可以在XafApplication.CustomizeFormattingCulture事件处理程序中覆盖默认格式设置选项。以下代码演示了如何在Windows Forms应用程序项目的Program.cs文件中执行此操作:
C#
public static void Main() {
//...
MySolutionWindowsFormsApplication winApplication = new MySolutionWindowsFormsApplication();
winApplication.CustomizeFormattingCulture +=
new EventHandler<CustomizeFormattingCultureEventArgs>(
winApplication_CustomizeFormattingCulture);
// ...
}
static void winApplication_CustomizeFormattingCulture(
object sender, CustomizeFormattingCultureEventArgs e) {
e.FormattingCulture.NumberFormat.CurrencySymbol = "USD";
}
The following image illustrates the currency symbol before and after implementing the code above:
下图展示了执行上述代码前后的货币符号:
The following code demonstrates how to override the default formatting options in an ASP.NET Web Forms application project’s Global.asax.cs (Global.asax.vb) file:
以下代码演示了如何在ASP.NET Web Forms应用程序项目的Global.asax.cs(Global.asax.vb)文件中覆盖默认格式设置选项:
C#
protected void Session_Start(Object sender, EventArgs e) {
WebApplication.SetInstance(Session, new MySolutionAspNetApplication());
WebApplication.Instance.CustomizeFormattingCulture += Instance_CustomizeFormattingCulture;
// ...
WebApplication.Instance.Setup();
WebApplication.Instance.Start();
}
The following image illustrates the currency symbol after implementing the code above:
下图展示了执行上述代码后的货币符号: