Hi,@David Thielen
You may follow this part of the document for Blazor Server Localization
I tried with a minimal example based on the document:
The project:
In program.cs:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddLocalization();
builder.Services.AddControllers();
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
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();
var supportedCultures = new[] { "en-US", "es-CL" };
var localizationOptions = new RequestLocalizationOptions()
.SetDefaultCulture(supportedCultures[0])
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
app.UseRequestLocalization(localizationOptions);
app.UseStaticFiles();
app.UseRouting();
app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();
MainLayout.razor:
@inherits LayoutComponentBase
<PageTitle>BlazorServerLocalization</PageTitle>
<div class="page">
<div class="sidebar">
<NavMenu />
</div>
<main>
<div class="top-row px-4">
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
</div>
<article class="content px-4">
<CultureSelector />
@Body
</article>
</main>
</div>
Resource files:
Shared.en-US.resx
Index.en-US.resx
Index component:
@page "/"
@using System.Globalization
@using Microsoft.Extensions.Localization;
@inject IStringLocalizer<Index> Loc
@inject IStringLocalizer<BlazorServerLocalization.Localization.Shared> SharedLoc
<PageTitle>Index</PageTitle>
<h1>@Loc["Hellow"]</h1>
@SharedLoc["Wel"]
<SurveyPrompt Title="How is Blazor working for you?" />
Result:
And for your question:
how to use a different language-culture for different users as some of it seems to be how to set a language-culture for the app as a whole
The Accept-Language header is originally intended to specify the user's language,and the localizer get current culture from culture providers,you may check the document related,