.Net Core Localization View: IViewLocalizer inside Linq expression

I am writing an mvc application in the .net kernel, I have a localization problem, I don’t know how to add IViewLocalizer to my mesh view. Here is my code:

@using NonFactors.Mvc.Grid;
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
@model IEnumerable<WeegreeEmployeeFormsCore.Models.Employee>

@(Html
    .Grid(Model)
    .Build(columns =>
    {
        columns.Add(model => model.Name).Titled(Localizer["Name"]).Sortable(true).Filterable(true);
        columns.Add(model => model.Surname).Titled(Localizer["Surname"]).Sortable(true).Filterable(true);
        columns.Add(model => model.EmploymentDate).Titled(Localizer["Hired"]).Sortable(true).Filterable(true);
        columns.Add(model => model.Country).Titled(Localizer["Country"]).Filterable(true).Sortable(true).Filterable(true);
        columns.Add(model => model.EmploymentForm).Titled(Localizer["EmploymentForm"]).Filterable(true);
        columns.Add(model => $"<a href=\"{Url.Action("Edit", "Form")}/{model.EmployeeId}\">{Localizer["Edit"]}</a>").Encoded(false);
        columns.Add(model => $"<a href=\"{Url.Action("Details", "Form")}/{model.EmployeeId}\">Details</a>").Encoded(false);


    })
    .Pageable(pager =>
    {
        pager.PagesToDisplay = 10;
        pager.CurrentPage = 1;
        pager.RowsPerPage = 10;
    })
    .Sortable()
    .Empty("No data found")

)

when I use {}to insert inside an expression model.EmployeeId, it works - the link works, but when I want to use Localizer to get the label Edit/Edytuj/і etc. instead, I got this in my opinion:   Microsoft.AspNetCore.Mvc.Localization.LocalizedHtmlString

+4
source share
3 answers

, IViewLocalizer["Foo"] LocalizedHtmlString . , , ToString. ToString , Object.ToString() :

var foo = Localizer["Foo"].ToString();
//foo gets assigned "Microsoft.AspNetCore.Mvc.Localization.LocalizedHtmlString"

Razor , LocalizedHtmlString , , :

<p>Hello @Localizer["World"]</p>
//renders <p>Hello World</p>

, , LocalizedHtmlString.Value:

@{
    var text = $"Hello {Localizer["World"].Value}";
}
<p>@text</p>
//renders <p>Hello World</p>

. .Value:

@{
    var text = $"Hello {Localizer["World"]}";
}
<p>@text</p>
//renders <p>Hello Microsoft.AspNetCore.Mvc.Localization.LocalizedHtmlString</p>
+5

.

Localizer string.Format. string.Format. pseudoText > This text will be {0} by the {1} , pseudoText > This text will be {0} by the {1}.

,

@Localizer["pseudoText", "transformed", "lozalizer"].

This text will be transformed by the lozalizer

0

I had a similar problem when trying to pass Localization with multiple values ​​to a partial view. I was able to solve this using the WriteTo () method .

@{
    var writer = new StringWriter();
    Localizer["heading", Model.UserName].WriteTo(writer, HtmlEncoder.Default);
    var heading = writer.ToString();
}

@await Html.PartialAsync("_ModalHeader", new ModalHeaderModel { Heading = heading })
0
source

Source: https://habr.com/ru/post/1656361/


All Articles