[英]translate asp.net core model Binding messages
I'm trying to translate standard asp.net core messages but none of my attempts have been successful. 我正在尝试翻译标准的asp.net核心消息,但是我的尝试都没有成功。 In ASP.NET MVC we had a package which translated Microsoft.AspNet.Mvc.
在ASP.NET MVC中,我们有一个翻译了Microsoft.AspNet.Mvc的程序包。 {Language}, but in asp.net core I did not package package to do this so I chose to do as indicated in the documentation itself, but I did not succeed.
{语言},但是在asp.net核心中,我没有打包软件包来执行此操作,因此我选择按照文档本身中的指示进行操作,但未成功。 Below code:
下面的代码:
Method Register: 方法注册:
public static void Register(IServiceCollection services, IConfiguration configuration)
{
#region aplication
services.AddMvc().AddRazorOptions(options => { options.AddNavigationBootstrap3Views(); });
services.TraduzirMensagensModelValidation();
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new List<CultureInfo> { new CultureInfo("pt-BR"), };
options.DefaultRequestCulture = new RequestCulture(culture: "pt-BR", uiCulture: "pt-BR");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
services.AddMemoryCache();
services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");
services.AddSingleton(provider => configuration);
#endregion
#region Presentation
services.AddAuthentication(configuration);
services.AddTransient<IUserClaimsPrincipalFactory<Conta>, ApplicationUserClaimsPrincipalFactory>();
services.AddAuthorization(options => HelperAutorization.Register(options));
services.AddCustonNavigation(configuration.GetSection("NavigationOptions"));
#endregion
}
Method TraduzirMensagensModelValidation: 方法TraduzirMensagensModelValidation:
public static class LocalizationModelValidation
{
public static IServiceCollection TraduzirMensagensModelValidation(this IServiceCollection services)
{
services.AddLocalization(options => { options.ResourcesPath = "Resources"; });
services.AddMvc(o =>
{
o.ModelBindingMessageProvider.SetAttemptedValueIsInvalidAccessor((x, y) => "O valor '{0}' não é válido para {1}.");
o.ModelBindingMessageProvider.SetMissingBindRequiredValueAccessor(x => "Não foi fornecido um valor para o campo {0}.");
o.ModelBindingMessageProvider.SetMissingKeyOrValueAccessor(() => "Campo obrigatório.");
o.ModelBindingMessageProvider.SetMissingRequestBodyRequiredValueAccessor(() => "É necessário que o body na requisição não esteja vazio.");
o.ModelBindingMessageProvider.SetNonPropertyAttemptedValueIsInvalidAccessor((x) => "O valor '{0}' não é válido.");
o.ModelBindingMessageProvider.SetNonPropertyUnknownValueIsInvalidAccessor(() => "O valor fornecido é inválido.");
o.ModelBindingMessageProvider.SetNonPropertyValueMustBeANumberAccessor(() => "O campo deve ser um número.");
o.ModelBindingMessageProvider.SetUnknownValueIsInvalidAccessor((x) => "O valor fornecido é inválido para {0}.");
o.ModelBindingMessageProvider.SetValueIsInvalidAccessor((x) => "O valor fornecido é inválido para {0}.");
o.ModelBindingMessageProvider.SetValueMustBeANumberAccessor(x => "O campo {0} deve ser um número.");
o.ModelBindingMessageProvider.SetValueMustNotBeNullAccessor(x => "O valor nulo é inválido.");
}).AddDataAnnotationsLocalization().AddViewLocalization();
return services;
}
}
Would anyone know any other way to translate these messages or if there is a .net standard / core package that translates these messages? 谁会知道其他任何方式翻译这些消息,或者是否有.net标准/核心软件包来翻译这些消息?
Note: I also tried with a resources file but translated the messages 注意:我也尝试使用资源文件,但翻译了消息
I would do it one of the worst ways possible, but it might work. 我会以最糟糕的方式之一来做到这一点,但它可能会起作用。 If you take every message and save it as a string.
如果您将每条消息都保存为字符串。 This makes it possible to make a couple of IF statements that tests for the language.
这样就可以制作几个测试该语言的IF语句。 You can then make it read a TXT file that defines what each string means in different language.
然后,您可以使其读取一个TXT文件,该文件定义每个字符串用不同语言表示的含义。 EXAMPLE: save the first message as UU001.
示例:将第一条消息另存为UU001。 Then define it in a TXT file reading: UU001 = "what the string means in the corresponding language".
然后在读取的TXT文件中定义它:UU001 =“字符串在相应语言中的含义”。
Hope it helps a little. 希望它能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.