[英]Has there been a change in how Blazor localization works?
I've been trying to implement resource pages for both English and Norwegian in a Blazor serverside app.我一直在尝试在 Blazor 服务器端应用程序中实现英语和挪威语的资源页面。 I have follow different guides with some different syntax ( Microsoft's guide and an article from c-sharpcorner ), but as you can see in the picture at the bottom it still says "welcome" (which is the keyword) when it should say "Hello.".
我按照不同的指南使用了一些不同的语法( 微软的指南和c-sharpcorner的一篇文章),但正如你在底部的图片中看到的那样,当它应该说“你好”时,它仍然说“欢迎”(这是关键字) ."。 I have the correct imports (according to the docs and guides) and here is my project structure:
我有正确的导入(根据文档和指南),这是我的项目结构:
Here is what Startup.cs looks like now:这是 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");
});
}
}
}
And Counter.razor:和计数器.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++;
}
}
I can't find any discrepancies between my project and those shown in tutorials/docs, so has anything changed with Blazor server-side localization, or am I missing something really obvious?我找不到我的项目与教程/文档中显示的项目之间的任何差异,因此 Blazor 服务器端本地化是否发生了任何变化,还是我遗漏了一些非常明显的东西?
For others having the same issue.对于其他有同样问题的人。 Don't forget to add RequestCultureProviders:
不要忘记添加 RequestCultureProviders:
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-GB");
options.SupportedUICultures = supportedCultures;
// <-- This
options.RequestCultureProviders.Add(new AcceptLanguageHeaderRequestCultureProvider());
});
AcceptLanguageHeaderRequestCultureProvider changes the culture base on the accept-language header. AcceptLanguageHeaderRequestCultureProvider 根据接受语言header 更改区域性。 There are also other providers (see https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.localization.requestcultureprovider ).
还有其他提供程序(请参阅https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.localization.requestcultureprovider )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.