我想在Blazor WebAssembly应用程序中本地化字符串,但我想将日期和数字表示法固定为en-US。问题是,当我将DefaultThreadCurrentCulture(它应该控制本地化字符串(设置为DefaultThreadCurrentCulture(它可以控制日期和数字(之外的其他值时,将无法再找到resx文件中的本地化字符串。如果我将两者都设置为相同的值(例如,均为en-US或均为nl-BE(,则一切都会按预期进行。
有人知道我该怎么解决吗?这是bug还是我遗漏了什么?下面是我的测试代码的一部分,其中包含关于CurrentUICulture和CurrentCulture的哪些组合有效或无效的注释。我把这段代码放在Main方法的开头。
private const string English = "en-US";
private const string Dutch = "nl-BE";
[...]
switch (combination)
{
case Combination.English:
// Works fine
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(English);
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(English);
break;
case Combination.Dutch:
// Works fine
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(Dutch);
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(Dutch);
break;
case Combination.DutchEnglish:
// Only the date and the float work (CurrentCulture)
// Resourcestrings are not found
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(Dutch);
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(English);
CultureInfo.CurrentUICulture = new CultureInfo(Dutch);
CultureInfo.CurrentCulture = new CultureInfo(English);
break;
case Combination.EnglishDutch:
// Only the date and the float work (CurrentCulture)
// Resourcestrings are not found
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(English);
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(Dutch);
break;
}
并且为了完整,Index.razor页面的本地化内容:
@page "/"
@using System.Globalization
@inject Microsoft.Extensions.Localization.IStringLocalizer<Index> localizedStrings
<table style="border-spacing: 0.5em; border: 1px solid black;">
<thead style="text-align:left;">
<tr>
<th>Description</th>
<th>Localization source</th>
<th>Localization value (Current/DefaultThread)</th>
<th>Value</th>
<th>Expectation</th>
</tr>
</thead>
<tbody>
<tr>
<td>Localized string</td>
<td>CurrentUICulture</td>
<td>@CultureInfo.CurrentUICulture / @CultureInfo.DefaultThreadCurrentUICulture</td>
<td>@localizedStrings["HelloWorld"]</td>
<td>nl-BE: Dag wereld!, en-US: Hello world!</td>
</tr>
<tr>
<td>Localized float</td>
<td>CurrentCulture</td>
<td>@CultureInfo.CurrentCulture / @CultureInfo.DefaultThreadCurrentCulture</td>
<td>@(0.5)</td>
<td>nl-BE should be with comma, en-US with point</td>
</tr>
<tr>
<td>Localized long date</td>
<td>CurrentCulture</td>
<td>@CultureInfo.CurrentCulture / @CultureInfo.DefaultThreadCurrentCulture</td>
<td>@DateTime.Now.ToLongDateString()</td>
<td>nl-BE: zaterdag 22 augustus 2020, en-US: Saturday, August 22, 2020</td>
</tr>
</tbody>
</table>
如果我在Index.razor上打印区域性,所有值总是按预期设置。因此,这些值不会传播到剃刀页面:
<h3>Current cultures</h3>
<p>CurrentCulture: @CultureInfo.CurrentCulture</p>
<p>CurrentUICulture: @CultureInfo.CurrentUICulture</p>
<p>DefaultThreadCurrentCulture: @CultureInfo.DefaultThreadCurrentCulture</p>
<p>DefaultThreadCurrentUICulture: @CultureInfo.DefaultThreadCurrentUICulture</p>
我很确定这是.net5.0-preview8中正在解决的一个错误,希望本周能发布!
https://github.com/dotnet/aspnetcore/issues/24174
编辑:看来我在解决你的问题上错了——事实并非如此。从使用aspnetcore5预览版8的快速测试来看,本地化工作的唯一时间似乎是currentculture和currentuiculture是否匹配。