Juwel
Juwel

Reputation: 41

Asp.net core 2.0 localization not working in partial view.

I am implementing convention-based localization with embedded resource file.Here is my sample code.

Parent View(Details.cshtml)

@using Microsoft.AspNetCore.Mvc.Localization
@model SampleLocalization.Models.BoatDetailsViewModel
@inject IViewLocalizer Localizer

<section class="location-map">
    <h4 class="section-title">
        @Localizer["Location"]
    </h4>
    <input type="hidden" asp-for="Latitude" />
    <input type="hidden" asp-for="Longitude" />
    <section id="map_canvas" style="min-height:525px;"></section>
</section>
@{
    await Html.RenderPartialAsync("_SendMessagePartial", 
    Model.SendMessageViewModel);
} 

Partial view(_SendMessagePartial.cshtml)

@using Microsoft.AspNetCore.Mvc.Localization 
@model SampleLocalization.Models.BoatSendMessageViewModel 
@inject IViewLocalizer Localizer 

<h5 >@Localizer["Send Message"]</h5> 

@Localizer["Send Message"] is not properly translated

Resource file structure as like below

Note: Resource folder structure is as same as view folder structure

Upvotes: 4

Views: 3573

Answers (1)

Eugen Kotov
Eugen Kotov

Reputation: 514

Basically, the problem is because it is a different file, so translation should be in Solution/Resources/Views/Boat/_SendMessagePartial.de-DE.resx. The workaround is to create shared resoource file. You can add empty SharedResource class into the root of the project and then use IStringLocalizer<SharedResource> instead of IViewLocalizer in your view.

Here are docs related to that https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-2.2

Upvotes: 6

Related Questions