2016-07-14 19 views
0

Ich verwendete den Ansatz, beschrieben here von @Gerardo Grignoli. Aber ich scheitere, wenn ich den IStringLocalizer verwenden möchte.Lokalisierung von IdentityErrorDescriber in ASP.Net Core 1.0

 private readonly IStringLocalizer _sharedLocalizer; 

    public CustomIdentityErrorDescriber(IStringLocalizerFactory stringLocalizerFactory) 
    { 
     _sharedLocalizer = stringLocalizerFactory.Create(typeof(SharedResource)); 
    } 

An anderen Orten in der App funktioniert das gut. mein ConfigureServices von Startup.cs

hier:

public void ConfigureServices(IServiceCollection services) 
    { 
     // Add framework services. 
     services.AddDbContext<ApplicationDbContext>(
      options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 

     //CustomIdentityErrorDescriber uses localization 
     services.AddLocalization(options => options.ResourcesPath = "Resources"); 

     // Configure supported cultures and localization options 
     services.Configure<RequestLocalizationOptions>(
      options => 
      { 
       var supportedCultures = new[] { new CultureInfo("en-US"), new CultureInfo("de-DE") }; 

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

     services.AddIdentity<ApplicationUser, IdentityRole>() 
      .AddErrorDescriber<CustomIdentityErrorDescriber>() 
      .AddEntityFrameworkStores<ApplicationDbContext>() 
      .AddDefaultTokenProviders(); 

     services.AddMvc() 
      .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) 
      .AddDataAnnotationsLocalization(); 

     // Add application services. 
     services.AddTransient<IEmailSender, AuthMessageSender>(); 
     services.AddTransient<ISmsSender, AuthMessageSender>(); 

     services.AddSingleton<IMapper>(sp => _mapperConfiguration.CreateMapper()); 
    } 

Nur der Schlüssel in Localizer Anruf wird angezeigt.

 public override IdentityError PasswordMismatch() { return new IdentityError { Code = nameof(PasswordMismatch), Description = _sharedLocalizer["Shared_IncorrectPassword"] }; } 

Antwort

1

Ich fand es heraus. Hier muss ich verwenden:

_sharedLocalizer = stringLocalizerFactory.Create("SharedResource", location: null); 

Andernfalls wird die resx Datei unter Resources.Resources gesucht

Verwandte Themen