Suv*_*tha 5 c# asp.net-core asp.net-core-localization asp.net-core-2.0
是否可以根据区域设置更改时间格式?
让我们看下面的场景
所以我的最终问题是,如何在asp.net core c#中根据区域设置设置时间?
我已经完成了带有日期的本地化,但我还必须完成时间。
上图显示了上午/下午。同样,我需要以 24 小时格式显示时间段,这是根据区域设置决定的。
好吧,我希望这就是你想要的。
首先,您需要支持实际的文化并在应用程序启动时配置它们。
public void ConfigureServices(IServiceCollection services)
{
/*boilerplate code omitted*/
// Configure supported cultures and localization options
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("de-DE"),
new CultureInfo("fr"),
new CultureInfo("ar-YE")
};
// State what the default culture for your application is. This will be used if no specific culture
// can be determined for a given request.
options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
// You must explicitly state which cultures your application supports.
// These are the cultures the app supports for formatting numbers, dates, etc.
options.SupportedCultures = supportedCultures;
// These are the cultures the app supports for UI strings, i.e. we have localized resources for.
options.SupportedUICultures = supportedCultures;
});
}
Run Code Online (Sandbox Code Playgroud)
那么你需要实际使用请求本地化
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
Run Code Online (Sandbox Code Playgroud)
现在,每当您将Date
对象从应用程序推送到客户端时,它都会在当前客户端区域设置中解析它。
如果您使用的是 Google Chrome 并想对此进行测试,您只需转到chrome://settings/languages
更改浏览器区域设置并更改设置即可。重新启动 Chrome,您应该会立即看到更改。
归档时间: |
|
查看次数: |
6132 次 |
最近记录: |