Is there a Blazor server localization sample somewhere

David Thielen 3,121 Reputation points
2023-09-21T03:44:28.2666667+00:00

Hi all;

Is there a good Blazor server localization sample somewhere. I've read the ASP.NET Core Blazor globalization and localization page and it's good. But in a lot of places it's not clear what is for WASM and what is for server side. And it's also not clear 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.

thanks - dave

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,563 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,581 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ruikai Feng - MSFT 2,576 Reputation points Microsoft Vendor
    2023-09-21T08:08:59.78+00:00

    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:

    User's image

    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

    User's image

    Index.en-US.resx

    User's image

    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:

    9.21

    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,


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.