Localization in Asp.Net Core

前端 未结 1 1528
别那么骄傲
别那么骄傲 2021-01-13 17:57

I have been following the instructions on this page to setup Localization:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization

However, fo

相关标签:
1条回答
  • I had this same problem around a week ago. I solved it with this:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLocalization(options => options.ResourcesPath = "Resources");
    
        services.AddMvc()
            .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
            .AddDataAnnotationsLocalization();
    
        services.Configure<RequestLocalizationOptions>(options =>
        {
            var supportedCultures = new[] { new CultureInfo("en"), new CultureInfo("es") };
    
            options.DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en");
            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;
        });
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
        app.UseRequestLocalization(locOptions.Value);
    
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }
    
        app.UseStaticFiles();
        app.UseMvcWithDefaultRoute();
    }
    

    One more comment, though. In ASP.NET Core, the default culture resource should not be created, only specific ones:

    So, given

    Views/Shared/Layout.cshtml
    

    Only create

    Resources/Views/Shared/Layout.es-MX.resx
    

    Do notice, also, that you are limiting Spanish to Mexican by this, so you should either have a es fallback, or add both es and es-MX.

    0 讨论(0)
提交回复
热议问题