I'm working on an ASP Net Core 2 application that uses Microsoft.Extensions.Localization.IStringLocalizer
and Microsoft.AspNetCore.Mvc.Localization
to provide trilingual content. The resource files (eg Controllers.HomeController.fr.resx
and Controllers.HomeController.nl.resx
are working fine and the following is typical of the Razor Page views:
@using Microsoft.Extensions.Localization
@using Microsoft.AspNetCore.Mvc.Localization
@using System.Globalization;
@inject IStringLocalizer<HomeController> Localizer
@inject IHtmlLocalizer<HomeController> HtmlLocalizer
...
<span style="font-weight: bold">@Localizer["My string 1"]</span>
...
<li>@HtmlLocalizer["My string 2 <span style=\"font-weight: bold;\">with bold</span>."]</li>
In the rest of the application, the culture is determined by the browser header, as described here .
The question
I'm building a feature that has to display content in the language specified by the user in a form, regardless of the browser language header. Note : The browser language header is still used normally to determine the language in which to display the overall page content, but this specific content is provided by a ViewComponent
and is used to create a printable label for giving to users. Therefore I also can't provide the language in the URL lang
parameter as this would affect the language of the entire page.
I want to do something like the following
<span style="font-weight: bold">@Localizer["Usage Instructions", "nl"]</span>
but I don't think this exists (tried it). Ie, set the language/culture on a string-specific basis.
However, from the tip in Visual Studio (see screenshot), it looks like the arguments
might allow me to set the language, I just can't find much documentation for it beyond general localization tutorials such as microsoft one .
The closest I can find is this docs but I still don't see details of the params object[] arguments
Thanks in advance for any suggestions!
IHtmlLocalizer
contains method WithCulture
which creates a localizer with specified culture
@inject IHtmlLocalizer<HomeController> HtmlLocalizer
@{
var nlHtmlLocalizer = HtmlLocalizer.WithCulture(new System.Globalization.CultureInfo("nl"));
}
<li>@nlHtmlLocalizer ["My string 2 <span style=\"font-weight: bold;\">with bold</span>."]</li>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.