【问题标题】:Blazor, Accept-Language HeaderBlazor,接受语言标头
【发布时间】:2023-02-11 19:09:48
【问题描述】:

在应用程序中开始本地化,默认使用德语,附加英语:

string[] supportedCultures = new string[] { "de-DE", "en-US" };
RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions()
    .SetDefaultCulture(supportedCultures[0])
    .AddSupportedCultures(supportedCultures)
    .AddSupportedUICultures(supportedCultures);
app.UseRequestLocalization(localizationOptions);

与例如Index.resx & Index.en-US.resx

因此,如果浏览器显然设置为“英语(美国)”,它只会显示英语资源,但对于其他文化它也应该显示英语。

所以resx改名为Index.resx & Index.en.resx,代码改为

string[] supportedCultures = new string[] { "de-DE", "en" };
RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions()
    .SetDefaultCulture(supportedCultures[0])
    .AddSupportedCultures(supportedCultures)
    .AddSupportedUICultures(supportedCultures);
app.UseRequestLocalization(localizationOptions);

按预期工作,每种英语文化都会获得英语资源。

下一个合乎逻辑的步骤,将 de-DE 更改为 de 只是为了制服:

string[] supportedCultures = new string[] { "de", "en" };
RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions()
    .SetDefaultCulture(supportedCultures[0])
    .AddSupportedCultures(supportedCultures)
    .AddSupportedUICultures(supportedCultures);
app.UseRequestLocalization(localizationOptions);

它完全坏了。没有显示任何英文资源。设置断点显示字符串被解析得很好并且区域性都按预期设置,但没有显示英语资源。

我知道我可以选择第二个选项并且一切正常,但我真的很想了解这里发生了什么,这是一个错误还是我只是理解这完全错误?

【问题讨论】:

    标签: blazor .net-6.0


    【解决方案1】:

    我很困惑你也没有在你的第一个变体中遇到这个问题。但是在没有测试的情况下,我发现您将默认文化设置为 supportedCultures[0] aka de-DE 或 de,具体取决于您的示例,因此浏览器中设置的除英语之外的每种语言都应显示德语资源或更一般的默认资源资源文件。

    【讨论】:

      猜你喜欢
      • 2012-12-05
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多