如何在ASP.Net Core中设置全球化文化?

5

我在使用ASP.NET Core MVC时遇到了一些小数问题。在常规的ASP.NET应用程序中,我通过将以下内容添加到web.config文件来解决该问题:

  <system.web>
    ...
    <globalization uiCulture="en" culture="en-US"/>
  </system.web>

但是,由于核心应用程序中没有web.config文件,我不太确定该怎么做。在core中,最接近此的是什么?


您可以向核心应用程序添加Web.config文件。 - Tony Abrams
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.0#localization-middleware - LazZiya
请查看以下链接:https://dev59.com/HZ3ha4cB1Zd3GeqPT2cY - Shahjahan
可能的解决方案在:https://dev59.com/KFkS5IYBdhLWcg3wDyn5#72435434 - Hicham O-Sfh
1个回答

7
在Asp.Net Core中,本地化是在Startup.ConfigureServices方法中配置的,并且可以应用于整个应用程序:
services.AddLocalization(options => options.ResourcesPath = "Resources");

services.AddMvc()
  .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
  .AddDataAnnotationsLocalization();

请求的当前文化设置在本地化中间件中。本地化中间件在Startup.Configure方法中启用。在可能检查请求文化的任何中间件之前(例如,app.UseMvcWithDefaultRoute()),必须配置本地化中间件。

var supportedCultures = new[]
{
 new CultureInfo("en-US"),
 new CultureInfo("fr"),
};

app.UseRequestLocalization(new RequestLocalizationOptions
{
   DefaultRequestCulture = new RequestCulture("en-US"),
   // Formatting numbers, dates, etc.
   SupportedCultures = supportedCultures,
   // UI strings that we have localized.
   SupportedUICultures = supportedCultures
 });

 app.UseStaticFiles();
 // To configure external authentication, 
 // see: http://go.microsoft.com/fwlink/?LinkID=532715
app.UseAuthentication();
app.UseMvcWithDefaultRoute();

如需更多细节,您可以参考官方文档


你在Startup.Configure中早期设置它的注释让我免去了很多痛苦。 - ScottB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接