【发布时间】:2020-07-03 05:09:56
【问题描述】:
我一直在尝试在 Blazor 服务器端应用程序中实现英语和挪威语的资源页面。我按照不同的指南使用了一些不同的语法(Microsoft's guide 和来自c-sharpcorner 的文章),但正如你在底部的图片中看到的那样,它仍然会说“欢迎”(这是关键字),当它应该说“你好。”。我有正确的导入(根据文档和指南),这是我的项目结构:
这是 Startup.cs 现在的样子:
namespace LocalizingTest
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-GB"),
new CultureInfo("nb")
};
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-GB");
options.SupportedUICultures = supportedCultures;
});
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton<WeatherForecastService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseRequestLocalization();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}
}
还有 Counter.razor:
@page "/counter"
@inject IStringLocalizer<Counter> Translator
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<p>@Translator["welcome"]</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
我找不到我的项目与教程/文档中显示的项目之间的任何差异,因此 Blazor 服务器端本地化是否发生了任何变化,还是我遗漏了一些非常明显的东西?
【问题讨论】:
-
您是如何在浏览器中设置语言的?
-
您可能想要使用文档的 Blazor 版本:docs.microsoft.com/en-us/aspnet/core/blazor/…
-
@HenkHolterman 我还没有为浏览器做任何设置;它应该仍然能够从docs.microsoft.com/en-gb/aspnet/core/fundamentals/…中提到的默认本地化文件中找到值
-
@MisterMagoo 谢谢你的建议。它引用了已经经常使用的文档,但我会尝试提到的 cookie 和 bind:culture(尽管如果它有什么不同我会感到惊讶)
-
抓住你的帽子! ??????
标签: c# .net-core localization blazor blazor-server-side