【问题标题】:Time format based on locale in asp.net core在 asp.net core 中基于语言环境的时间格式
【发布时间】:2018-09-10 13:21:35
【问题描述】:

是否可以根据地区更改时间格式?

让我们来看看下面的场景

  • 挪威 - 广泛使用 24 小时制
  • 瑞典 - 广泛使用 24 小时制时间格式
  • 瑞士德国地区 - 广泛使用 12 小时格式
  • 德国 - 广泛使用 24 小时制

所以我的最终问题是,如何在 asp.net core c# 中根据语言环境设置时间?

我已经完成了日期的本地化,但我也必须这样做。

上图显示上午/下午。同样,我需要以 24 小时格式显示时隙,这取决于区域设置。

【问题讨论】:

  • 您能否更详细地说明应该更改时间格式的位置以及取决于哪个参数?你如何确定某人在哪里?
  • @Marco 我已经更新了问题
  • 从请求标头中您可以找到 HTTP_ACCEPT_LANGUAGE 语言环境并使用以下代码 DateTime myDate = DateTime.Now; string us = myDate.ToString(new CultureInfo("en-US")); string uk = myDate.ToString(new CultureInfo("en-GB")); Console.WriteLine(我们); Console.WriteLine(英国);它将给出完整的日期和时间。但你只能花时间..

标签: c# asp.net-core asp.net-core-2.0 asp.net-core-localization


【解决方案1】:

好的,我希望这是你想要的。

首先,您需要支持实际的文化并在应用程序启动时对其进行配置。

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;
    });
}

那你需要实际使用请求本地化

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?}");
    });
}

现在,每当您将 Date 对象从应用程序推送到客户端时,它都会在当前客户端区域设置中对其进行解析。

如果您使用的是谷歌浏览器并且想要对此进行测试,您只需转到chrome://settings/languages 更改您的浏览器区域设置并更改设置。重新启动 Chrome,您应该会立即看到更改。

参考:https://github.com/aspnet/Entropy/blob/2fcbabef58c2c21845848c35e9d5e5f89b19adc5/samples/Localization.StarterWeb/Startup.cs

【讨论】:

  • 我的客户要求是根据区域设置更改时间格式,而不是通过更改 chrome 设置
  • 更改 chrome 中的设置仅用于测试目的。切换到 en-US 以查看您的 web 应用如何根据区域设置显示不同的时间格式。
【解决方案2】:

如果你已经做好本地化,你不需要做额外的步骤来以本地格式显示时间,

但以防万一;您可以在配置本地化时定义特定文化的时间格式,方法是提供您自己的格式,如下所示(代码从@Marco 的回复中修改):

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") {                    
                DateTimeFormat = {
                    // change long time pattern to 24 hours 
                    // e.g. 13:50:21
                    LongTimePattern = "HH:mm:ss",

                    // short time pattern as 12 hours
                    // e.g. 01:50:21 PM
                    ShortTimePattern = "hh:mm tt"
                },
            }
    };

    // 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;
});
}

在您看来,您必须调用相关模式:

DateTime.Now.ToLongTimeString()

DateTime.Now.ToShortTimeString()

【讨论】:

    猜你喜欢
    • 2021-09-26
    • 1970-01-01
    • 2011-11-25
    • 2016-03-29
    • 2018-06-25
    • 2019-08-10
    • 2022-07-28
    • 1970-01-01
    • 2014-03-16
    相关资源
    最近更新 更多