Open Closed

How to prevent Localize to output HTMLEncoded string when using razor pages #1850


0
mrall created

Im using the standard way to output localized content (@L["somekey"]) - this works fine, but when my textes contain html-tags the appear as text and are not interpreted as html.

I know thats the default behaviour of .net localization, but how can I out put the text "raw" without htmlencode?

kind regards


2 Answer(s)
  • 0
    EngincanV created
    Support Team

    Hi @mrall, probably you are using IHtmlLocalizer<MyResource> please change it to IStringLocalizer<MyResource>. After that change, your localization value won't be html-encoded, it will be view as raw text.

    @inject IStringLocalizer<MyResource>

  • 0
    mrall created

    Hi,

    according to the docs of IHtmlLocalizer and IStringLocalizer, the opposite is true, https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-5.0

    "Use the IHtmlLocalizer implementation for resources that contain HTML. IHtmlLocalizer HTML encodes arguments that are formatted in the resource string, but doesn't HTML encode the resource string itself."

    I tried your suggestion but that didn't solve my problem.

    The point is, that within Razor-Pages Text ouputted with the "@"-Operator always gets HTML-Encoded, so the solution to my problem was using "@Html.Raw(...)"

    @Html.Raw(L["key:Text01"].Value)

    That did the trick.

    cheers