【问题标题】:translate asp.net core model Binding messages翻译asp.net核心模型绑定消息
【发布时间】:2018-06-22 16:33:45
【问题描述】:

我正在尝试翻译标准的 asp.net 核心消息,但我的尝试都没有成功。在 ASP.NET MVC 中,我们有一个翻译 Microsoft.AspNet.Mvc 的包。 {Language},但在 asp.net core 中我没有打包 package 来执行此操作,所以我选择按照文档本身的说明进行操作,但我没有成功。下面的代码:

方法寄存器:

 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
    }

方法 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;
    }
}

有谁知道翻译这些消息的任何其他方式,或者是否有翻译这些消息的 .net 标准/核心包?

注意:我也尝试过使用资源文件,但翻译了消息

【问题讨论】:

    标签: c# asp.net asp.net-core .net-standard


    【解决方案1】:

    我会以最糟糕的方式之一,但它可能会奏效。如果您接收每条消息并将其保存为字符串。这使得创建几个测试语言的 IF 语句成为可能。然后,您可以让它读取一个 TXT 文件,该文件定义了每个字符串在不同语言中的含义。示例:将第一条消息保存为 UU001。然后在 TXT 文件中定义:UU001 = "what the string mean in the corresponding language"。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-03
      • 2019-11-14
      • 2021-08-03
      • 1970-01-01
      • 1970-01-01
      • 2022-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多