我在最新版本的VS 2019中使用blazor 3.1。
到目前为止,我已经可以本地化页面标签(标题,表格字段等)。
在ListEmployee.razor
页面中,我能够本地化表格标题等。在AddEmplyeeValidation.razor
页面中,我能够本地化表单标签,但是在本地化验证消息时遇到问题。
用于验证消息的Employee.cs
验证消息是在此文件和Resources/Data
文件夹中定义的,其名称定义为Data.Employee.ar.resx
并且Data.Employee.ar.resx
无法正常工作
使用System.ComponentModel.DataAnnotations;
命名空间BlazorSPA1.Data {公共类Employee {[MaxLength(50)]公共字符串ID { 组; }
[Required (ErrorMessage ="Name is RRRequired")]
[StringLength(20, ErrorMessage = "Name is too long.")]
public string Name { get; set; }
[Required]
[StringLength(20)]
public string Department { get; set; }
[MaxLength(100)]
public string Designation { get; set; }
[MaxLength(100)]
public string Company { get; set; }
[MaxLength(100)]
public string City { get; set; }
}
}
我如何才能基于“添加员工”表单的语言从资源文件中获取验证消息。
@page "/addemployeeValidation"
@inject NavigationManager NavigationManager
@inject IEmployeeService EmployeeService
@inject IStringLocalizer<AddEmployeeValidation> L
<h2>Create Employee</h2>
<hr />
<EditForm Model="@employee" OnValidSubmit="@CreateEmployee">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label for="Name" class="control-label">@L["Name"]</label>
<input for="Name" class="form-control" @bind="@employee.Name" />
<ValidationMessage For="@(()=> employee.Name)" />
</div>
<div class="form-group">
<label for="Department" class="control-label">@L["Department"]</label>
<input for="Department" class="form-control" @bind="@employee.Department" />
</div>
<div class="form-group">
<label for="Designation" class="control-label">@L["Designation"]</label>
<input for="Designation" class="form-control" @bind="@employee.Designation" />
</div>
<div class="form-group">
<label for="Company" class="control-label">@L["Company"]</label>
<input for="Company" class="form-control" @bind="@employee.Company" />
</div>
<div class="form-group">
<label for="City" class="control-label">@L["City"]</label>
<input for="City" class="form-control" @bind="@employee.City" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Save" />
<input type="button" class="btn" @onclick="@Cancel" value="Cancel" />
</div>
</div>
</div>
</EditForm>
@code {
Employee employee = new Employee();
protected async Task CreateEmployee()
{
await EmployeeService.CreateEmployee(employee);
NavigationManager.NavigateTo("listemployees");
}
void Cancel()
{
NavigationManager.NavigateTo("listemployees");
}
}
我读了几篇文章,尝试了几件事,但似乎无济于事
Startup.cs中的代码
services.AddServerSideBlazor(options => options.DetailedErrors = true);
services.AddLocalization(options => options.ResourcesPath = "Resources");
var supportedCultures = new List<CultureInfo> { new CultureInfo("en"), new CultureInfo("ar") };
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en");
options.SupportedUICultures = supportedCultures;
});
我正在使用以下示例进行本地化,但未显示如何本地化错误消息 https://www.c-sharpcorner.com/article/localization-in-blazor-server/
文件夹结构图片
英文版的资源文件示例以同样的方式我也有阿拉伯文文件
在下面的屏幕截图中,您将从资源文件中提取字段名称,但对于Validation消息,仅以英文显示,因为它不起作用