1

I cannot figure how to work with Localization on an ASP.NET Core MVC website : I've follow this website but I think I forgot something :

Startup :

 public void ConfigureServices(IServiceCollection services)
        {
        services.AddMvc(
                config =>
                {
                    config.Filters.Add<ActionFilter>();
                })
                .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                .AddDataAnnotationsLocalization();

        services.Configure<RequestLocalizationOptions>(option =>
            {
                var supportedCultures = new[]
                {
                    new CultureInfo("en"),
                    new CultureInfo("fr"),
                };
                option.DefaultRequestCulture = new RequestCulture(culture: "fr", uiCulture: "fr");
                option.SupportedCultures = supportedCultures;
                option.SupportedUICultures = supportedCultures;
            });
        }

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
        var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
            app.UseRequestLocalization(locOptions.Value);

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

HomeController :

    public class HomeController : Controller
    {

    private readonly IStringLocalizer<HomeController> _localizer;

            public HomeController(IStringLocalizer<HomeController> localizer)
                {
                    _localizer = localizer;
                }
    public IActionResult Index()
            {
                return View()
            }
}

Index.cshtml

@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer

@{
    ViewData["Title"] = Localizer["Home"];
}

<h1>News</h1>
<h2>@Localizer["Home"]</h2>

And I have a Resources Folder with a file HomeController.fr.resx inside where Home => Accueil relation is defined.

The function Request.HttpContext.Features.Get<IRequestCultureFeature>().RequestCulture.Culture.Name return fr but the page always display Home instead of Accueil.

Am I missing something to do for the localization ?

1 Answer 1

1

I finally figure why it's not working, I just forget to add the Localization.AspNetCore.TagHelpers NugetPackage.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.