Linq表达式内的IViewLocalizer

Linq表达式内的IViewLocalizer

本文介绍了.Net核心本地化视图:Linq表达式内的IViewLocalizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在.net核心中编写mvc应用程序,我在本地化方面遇到问题,我不知道如何将IViewLocalizer添加到我的网格视图中.这是我的代码:

i'm writing mvc app in .net core, i have problem with localization, i don't know how to add IViewLocalizer to my grid 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")

)

当我使用{}插入表达式model.EmployeeId内时它起作用-链接有效,但是当我想使用Localizer获取题词Edit/Edytuj/змінити etc时.而不是我认为这个: Microsoft.AspNetCore.Mvc.Localization.LocalizedHtmlString

when i use {} to insert inside expression model.EmployeeId it works - link is working, but when i want to use Localizer to get inscription Edit/Edytuj/змінити etc. instead of i got this in my view : Microsoft.AspNetCore.Mvc.Localization.LocalizedHtmlString

推荐答案

这是因为IViewLocalizer["Foo"]返回 LocalizedHtmlString 而不是字符串.因此,当您在字符串插值表达式中包含该表达式时,它将调用其ToString方法.由于尚未在该类中重新定义ToString,因此默认的Object.ToString()实现返回类型名称:

That's because IViewLocalizer["Foo"] returns a LocalizedHtmlString instead of a string. So when you include that in a string interpolation expression, it is calling its ToString method. As ToString has not been redefined in that class, the default Object.ToString() implementation returns the type name:

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

Razor知道在呈现页面时如何处理LocalizedHtmlString实例,因此可以按预期呈现:

Razor knows how to handle LocalizedHtmlString instances when rendering a page, so this renders as expected:

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

如果要手动连接本地化的字符串,则需要确保获得 LocalizedHtmlString.Value 属性:

If you want to manually concatenate the localized string, then you need to make sure you get the LocalizedHtmlString.Value property:

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

与您的方法进行比较,而无需调用.Value:

Compare that with your approach without calling .Value:

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

这篇关于.Net核心本地化视图:Linq表达式内的IViewLocalizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 13:03