【问题标题】:How To Set Globalization Culture in ASP.Net Core?如何在 ASP.Net Core 中设置全球化文化?
【发布时间】:2019-09-24 14:05:08
【问题描述】:

我在 asp net core mvc 中遇到了十进制数的问题。通过将其添加到 web.config,我让它在常规的 asp 网络应用程序中工作:

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

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

【问题讨论】:

标签: asp.net asp.net-mvc asp.net-core asp.net-web-api


【解决方案1】:

在 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();

更多详情可以参考the official documentation

【讨论】:

    猜你喜欢
    • 2020-08-20
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    相关资源
    最近更新 更多