3

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 Answer 1

3

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.

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

2 Comments

thank you very much @hakany! Added the package and suddenly it worked! great :)
Note that this package is not an official Microsoft package - and it is not compatible with .NET Core 2.0.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.