asp.net - 如何在 ASP.Net Core 中设置全球化文化?
问题描述
我在 asp net core mvc 中遇到了十进制数的问题。通过将其添加到 web.config,我让它在常规的 asp 网络应用程序中工作:
<system.web>
...
<globalization uiCulture="en" culture="en-US"/>
</system.web>
但是,由于核心应用程序中没有 web.config,我不太确定该怎么做。核心中最接近的近似值会是什么样子?
解决方案
在 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();
更详细的可以参考官方文档。
推荐阅读
- java - 从数据库中检索的日期值使用 java.util.date 显示前一天的值
- jenkins - Jenkins 管道构建分支触发器
- javascript - 在句柄函数上反应钩子状态未定义
- business-objects - 在 WebI 报告中过滤高于 X% 组的行
- flutter - 如何在颤动中将自定义小部件传递给另一个自定义小部件?
- linux - 在 Linux (Ubuntu) 中使用 MI Box 进行 WIFI 调试。设备无法识别
- probabilistic-programming - Pyro Paramstore 的不同访问方法给出不同的结果
- vue.js - 带有 prop 数组的 Vue.js 无限循环
- html - 容器内的网格列
- python - 如何从.gitignore 中提取文件夹?