.NET 7.0 在 Date 类型的 inputTagHelper 中设置文化。

huangapple go评论78阅读模式
英文:

.NET 7.0 set culture for inputTagHelper in type Date

问题

我正在运行一个带有Razor的ASP.NET Core 7.0 Web应用程序,但我找不到在日期输入标签助手中更改本地化的方法。

如何将此文本切换到西班牙语?

谢谢提前!

英文:

I'm running an ASP.NET Core 7.0 web app with Razor and I can't find a way to change localization in input tag helper for type date.

How can I switch this text to Spanish?

Thanks in advance!

.NET 7.0 在 Date 类型的 inputTagHelper 中设置文化。

答案1

得分: 1

看上去你已经整理好了一份指南,这是你的西班牙文化设置代码:

builder.Services.Configure<RequestLocalizationOptions>(options =>
{
    var supportedCultures = new[]
    {
        new CultureInfo("es-ES"),
    };
    options.DefaultRequestCulture = new RequestCulture("es-ES");
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;
});

var app = builder.Build();

var localizationOptions = app.Services.GetRequiredService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(localizationOptions.Value);

然后是设置浏览器默认语言为西班牙语。最后,在Razor页面中,使用asp-format属性来格式化日期,确保使用适合西班牙文化的格式字符串。测试页面的代码如下:

@page
@model TestModel
@{
    ViewData["Title"] = "Test";
}

<h1>@ViewData["Title"]</h1>

<form method="post">
    <div class="form-group">
        <label asp-for="DateOfBirth"></label>
        <input asp-for="DateOfBirth" class="form-control" type="date" asp-format="{0:d}" />
        <span asp-validation-for="DateOfBirth" class="text-danger"></span>
    </div>

    @*<button type="submit" class="btn btn-primary">Submit</button>*@
</form>

还有Test.cshtml.cs的代码:

using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WorkRazor.Pages
{
    public class TestModel : PageModel
    {
        private readonly ILogger<PrivacyModel> _logger;

        [Required]
        [DataType(DataType.Date)]
        [Display(Name = "Date of Birth")]
        public DateTime DateOfBirth { get; set; }

        public TestModel(ILogger<PrivacyModel> logger)
        {
            _logger = logger;
        }

        public void OnGet()
        {

        }
    }
}
英文:

Step1: Add the following codes in Program.cs file to use Spanish culture:

builder.Services.Configure&lt;RequestLocalizationOptions&gt;(options =&gt;
{
var supportedCultures = new[]
{
    new CultureInfo(&quot;es-ES&quot;),
};
options.DefaultRequestCulture = new RequestCulture(&quot;es-ES&quot;);
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});

var app = builder.Build();

var localizationOptions = app.Services.GetRequiredService&lt;IOptions&lt;RequestLocalizationOptions&gt;&gt;();
app.UseRequestLocalization(localizationOptions.Value);

Step2: Set Spanish as your browser's default language.

Step3: In Razor page, use the asp-format attribute to format the date using the appropriate format string for the Spanish culture. Here is the code of my test razor page:

@page
@model TestModel
@{
    ViewData[&quot;Title&quot;] = &quot;Test&quot;;
}

&lt;h1&gt;@ViewData[&quot;Title&quot;]&lt;/h1&gt;

&lt;form method=&quot;post&quot;&gt;
    &lt;div class=&quot;form-group&quot;&gt;
        &lt;label asp-for=&quot;DateOfBirth&quot;&gt;&lt;/label&gt;
        &lt;input asp-for=&quot;DateOfBirth&quot; class=&quot;form-control&quot; type=&quot;date&quot; asp-format=&quot;{0:d}&quot; /&gt;
    &lt;span asp-validation-for=&quot;DateOfBirth&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
&lt;/div&gt;

@*&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Submit&lt;/button&gt;*@

</form>

and here is the code of the Test.cshtml.cs:

using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WorkRazor.Pages
{
    public class TestModel : PageModel
{
    private readonly ILogger&lt;PrivacyModel&gt; _logger;

    [Required]
    [DataType(DataType.Date)]
    [Display(Name = &quot;Date of Birth&quot;)]
    public DateTime DateOfBirth { get; set; }

    public TestModel(ILogger&lt;PrivacyModel&gt; logger)
    {
        _logger = logger;
    }

    public void OnGet()
    {

    }
}
}

Here is my test result:
.NET 7.0 在 Date 类型的 inputTagHelper 中设置文化。

huangapple
  • 本文由 发表于 2023年2月27日 07:00:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定