在Razor页面中为ASP.NET Core 2 Localizer指定语言

时间:2019-03-12 12:30:12

标签: c# asp.net-core asp.net-core-mvc

我正在开发一个使用Microsoft.Extensions.Localization.IStringLocalizerMicrosoft.AspNetCore.Mvc.Localization提供三种语言内容的ASP Net Core 2应用程序。资源文件(例如Controllers.HomeController.fr.resxControllers.HomeController.nl.resx正常运行,以下是Razor页面视图的典型代表:

@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>

在应用程序的其余部分,区域性由浏览器标头确定,如here所述。

问题

我正在构建一项功能,该功能必须以用户指定的语言以表格形式显示内容,而不管浏览器的语言标题如何。 注意:浏览器语言标头通常仍用于确定显示总体页面内容所用的语言,但是此特定内容由ViewComponent提供并用于创建可打印的内容给用户的标签。因此,我也无法在URL lang参数中提供语言,因为这会影响整个页面的语言。

我想做类似以下的事情

<span style="font-weight: bold">@Localizer["Usage Instructions", "nl"]</span>

但是我不认为这存在(尝试过)。即,根据字符串设置语言/区域性。

但是,从Visual Studio的技巧(请参见屏幕截图)来看,arguments似乎可以让我设置语言,但除了{ {3}}。

microsoft one

我能找到的最接近的是Visual Studio hint,但是我仍然看不到params object[] arguments

的详细信息

提前感谢您的任何建议!

0 个答案:

没有答案