0

I used the approach, described here from @Gerardo Grignoli. But I fail if I want to use the IStringLocalizer.

        private readonly IStringLocalizer _sharedLocalizer;

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

At other places in the app this works fine.

Here my ConfigureServices from Startup.cs:

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

Only the key used in localizer call is displayed.

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

1 Answers1

1

I found it out. Here I have to use:

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

Otherwise the resx file is searched under Resources.Resources