我创建了用于本地化的资源文件。在加载页面时,它设置了当前的区域性信息,但从下拉列表中更改了语言,它不会更改。
这是我的代码,
本地化。剃刀:
@page "/"
@using System.Globalization
@using Microsoft.AspNetCore.Localization
@inject IStringLocalizer<Localization> localizer
<h3>@localizer["Title"]</h3>
@localizer["Content"]
<select @onchange="OnSelected">
<option>Select...</option>
<option value="en">English</option>
<option value="de">German</option>
</select>
@code {
private void OnSelected(ChangeEventArgs e)
{
// Code
}
}
更改所选语言需要在OnSelected事件中写入什么?
提前谢谢。
主布局。剃刀:
@inherits LayoutComponentBase
@inject NavigationManager NavigationManager
<div class="sidebar">
<NavMenu />
</div>
<div class="main">
<div class="top-row px-4">
<select @onchange="OnSelected">
<option value="en">English</option>
<option value="de">German</option>
</select>
</div>
<div class="content px-4">
@Body
</div>
</div>
@code {
private void OnSelected(ChangeEventArgs e)
{
var selectedCulture = e.Value.ToString();
CultureInfo.CurrentUICulture =
CultureInfo.GetCultures(CultureTypes.AllCultures)
.First(c => c.Name == selectedCulture);
}
}
本地化。剃刀:
@page "/"
@inject IStringLocalizer<Localization> localizer
<h3>@localizer["Title"]</h3>
@localizer["Content"]
程序.cs:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
启动.cs:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddLocalization(options => { options.ResourcesPath = "Resources"; });
var supportedCultures = new List<CultureInfo> { new CultureInfo("en"), new CultureInfo("de") };
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("de");
options.SupportedUICultures = supportedCultures;
});
}
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.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRequestLocalization(app.ApplicationServices.GetRequiredService<IOptions<RequestLocalizationOptions>>().Value);
app.UseRouting();
app.ApplicationServices
.UseBootstrapProviders()
.UseFontAwesomeIcons();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}
您需要设置当前区域性:
@page "/"
@using System.Globalization
@using Microsoft.AspNetCore.Localization
@inject IStringLocalizer<Localization> localizer
<h3>@localizer["Title"]</h3>
@localizer["Content"]
<select @onchange="OnSelected">
<option>Select...</option>
<option value="en">English</option>
<option value="de">German</option>
</select>
@code {
private void OnSelected(ChangeEventArgs e)
{
// Code
var selectedCulture = e.Value.ToString();
CultureInfo.CurrentCulture = CultureInfo.GetCultures(CultureTypes.AllCultures)
.First(c => c.Name == selectedCulture);
}
}