我希望您能为Blazor应用程序提供本地化翻译,并且在查找视图中的键资源值时遇到了一些问题。
请问该如何解决此问题?
以下是我的代码:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => { options.ResourcesPath = "Resources"; });
services.Configure<RequestLocalizationOptions>(
options =>
{
List<CultureInfo> supportedCultures =
new List<CultureInfo>
{
new CultureInfo("bg-BG"),
new CultureInfo("en-US")
};
options.DefaultRequestCulture = new RequestCulture("bg-BG");
// Formatting numbers, dates, etc.
options.SupportedCultures = supportedCultures;
// UI string
options.SupportedUICultures = supportedCultures;
});
services.AddRazorPages();
services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });
services.AddApplicationRepositoryServices();
services.AddSingleton<WeatherForecastService>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRequestLocalization( app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value);
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.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
Login.razor
@page "/login"
@using Microsoft.Extensions.Localization
@inject IStringLocalizer<Login> Localizer
<button type="submit" class="btn btn-primary w-100">@Localizer["LoginButtonText"]</button>
页面位置
项目
-- 页面
---- 帐户
------ Login.razor
资源文件位置
项目
-- 资源
---- 页面
------ 帐户
-------- Login.bg-BG.resx