3
votes

I'm trying to implement Localization, but when run only the Name returns.

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddLocalization(options => options.ResourcesPath = "Resources");
    services.AddMvc()
        .AddViewLocalization()
        .AddDataAnnotationsLocalization();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    .....

    List<CultureInfo> supportedCultures = new List<CultureInfo>
    {
        new CultureInfo("no"),
        new CultureInfo("en")
    };

    app.UseRequestLocalization(new RequestLocalizationOptions
    {
        DefaultRequestCulture = new RequestCulture("no"),
        SupportedCultures = supportedCultures,
        SupportedUICultures = supportedCultures
    });

    app.UseStaticFiles();

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

The project contains one View named Index.cshtml, located in "Views\Home". The Resources folder contains two resource files: Views.Home.Index.en.resx and Views.Home.Index.no.resx

Index.cshtml:

@inject IOptions<RequestLocalizationOptions> LocalizerOptions
@inject IViewLocalizer Localizer

@{
    var requestCulture = Context.Features.Get<IRequestCultureFeature>();
    var cultureItems = LocalizerOptions.Value.SupportedUICultures
        .Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName })
        .ToList();
}

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

<div id="section_box_about" class="section_box_about" style="margin-top:0px;">
    @Localizer["HeaderAbout"]
</div>

<div class="container" style="width:96%;margin:auto;">
    @Localizer["About"]

    <br />
    <br />
    <br />
    @requestCulture.RequestCulture.Culture.Name
</div>

@Localizer["About"] returns "About"

@requestCulture.RequestCulture.Culture.Name returns "no"

1

1 Answers

3
votes

Could you add Localization.AspNetCore.TagHelpers nuget package. After I added this I got localization working.

See also: Localization in ASP.Net core MVC not working - unable to locate resource file with similar issue.