The localization system has changed quite a bit in ASP.NET Core。当前帖子中不再提供CurrentCulture
。
我正在尝试设置NumberFormat
的{{1}}属性here,以便自定义显示资金的格式,但显然不再可能。那么如何设置CurrentCulture
的{{1}}属性?
以前在asp.net核心之前为此工作的代码:
NumberFormat
然后例如我想在我的代码中的某处:
CurrentCulture
并期望应用CurrentCulture modified = new CultureInfo(Thread.CurrentThread.CurrentCulture.Name);
Thread.CurrentThread.CurrentCulture = modified;
var numberFormat = modified.NumberFormat;
numberFormat.CurrencySymbol = "RM";
numberFormat.CurrencyDecimalDigits = 2;
numberFormat.CurrencyDecimalSeparator = ".";
numberFormat.CurrencyGroupSeparator = ",";
设置。
答案 0 :(得分:5)
您可以在 Startup.cs 中的UseRequestLocalization
方法中添加Configure()
:
public void Configure(IApplicationBuilder app)
{
app.UseIISPlatformHandler();
app.UseRequestLocalization(new RequestCulture(new CultureInfo("es")));
app.Run(async (context) =>
{
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(HtmlEncoder.Default.HtmlEncode(1000.5f.ToString("C")));
});
}
结果:
1.000,50€
此外,更重要的是原始问题:
public void Configure(IApplicationBuilder app)
{
app.UseIISPlatformHandler();
var modified = new CultureInfo(CultureInfo.DefaultThreadCurrentCulture.DisplayName);
modified.NumberFormat.CurrencySymbol = "RM";
modified.NumberFormat.CurrencyDecimalDigits = 2;
modified.NumberFormat.CurrencyDecimalSeparator = ".";
modified.NumberFormat.CurrencyGroupSeparator = ",";
app.UseRequestLocalization(new RequestCulture(modified));
app.Run(async (context) =>
{
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(HtmlEncoder.Default.HtmlEncode(1000.5f.ToString("C")));
});
}
结果:
RM1,000.50