Persilsoft.Localizer.Blazor 1.0.9

dotnet add package Persilsoft.Localizer.Blazor --version 1.0.9
NuGet\Install-Package Persilsoft.Localizer.Blazor -Version 1.0.9
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Persilsoft.Localizer.Blazor" Version="1.0.9" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Persilsoft.Localizer.Blazor --version 1.0.9
#r "nuget: Persilsoft.Localizer.Blazor, 1.0.9"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Persilsoft.Localizer.Blazor as a Cake Addin
#addin nuget:?package=Persilsoft.Localizer.Blazor&version=1.0.9

// Install Persilsoft.Localizer.Blazor as a Cake Tool
#tool nuget:?package=Persilsoft.Localizer.Blazor&version=1.0.9

Persilsoft.Localizer.Blazor

A simple localization tool for Blazor apps


You can configure the culture in your Blazor application as follows, without needing to use this package.

var host = builder.Build();
culture = CultureInfo.CreateSpecificCulture("es-PE");
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;

await host.RunAsync();

However, the package offers you certain additional tools and configurations.
For example, it provides a language selector that you could place in the MainLayout component.

@inherits LayoutComponentBase
@using Persilsoft.Localizer.Blazor.Components

<div class="page">
    <div class="sidebar">
        <NavMenu />
    </div>

    <main>
        <div class="top-row px-4">
            <CultureSelector SupportedCultures="supportedCultures" />
            <a href="https://learn.microsoft.com/aspnet/core/" target="_blank">@Messages.About</a>
        </div>

        <article class="content px-4">
            @Body
        </article>
    </main>
</div>

@code {
    private string[] supportedCultures;

    protected override async Task OnInitializedAsync()
    {
        supportedCultures = new string[] { "es-PE", "en-US" };

        await Task.CompletedTask;
    }
}

Each time the user makes a change in the selector, the selected culture will be stored in the localStorage with the key culture.

Http Handler

If your Blazor application consumes a WebApi that applies localization settings based on the 'Accept-Language' header of HTTP requests, then you could use an HTTP client configurator provided by this package to intercept requests and add this header, taking the culture from the culture key in localStorage. With this, you'll ensure that your Blazor application integrates language configuration in both the Backend and the Frontend.

Let's assume we have the following Gateway to communicate with the WebApi:

public class ApiGateway(HttpClient client)
{
    public async Task<string> GetWelcomeMessage() =>
        await client.GetStringAsync("/localizer/message");
}

We can create an extension method to register our Gateway and configure the Http client to use the request handler.

using Persilsoft.Localizer.Blazor.HttpHandler;
using ServiceCollectionExtensions;

public static class DependencyContainer
{
    public static IServiceCollection AddGateway(this IServiceCollection services,
        Action<HttpClient> httpClientConfigurator)
    {
        Builder.AddLocalizationDelegatingHandler();
        services.AddHttpClient<ApiGateway>(httpClientConfigurator)
            .AddHttpMessageHandler<LocalizationDelegatingHandler>();

        return services;
    }
}

Now, we simply register it in the dependency container.

Action<HttpClient> httpClientConfigurator = client =>
{
    client.BaseAddress = new Uri("https://localhost:7025");
};
builder.Services.AddGateway(httpClientConfigurator);

Gateway demo

Here is an example using the Gateway:

@page "/"

<PageTitle>Localization in Blazor</PageTitle>

<h1>Blazor Demo</h1>

<button class="btn btn-primary" @onclick=Welcome_Click>Welcome</button>
<hr />
@Welcome

@code {
    private string Welcome;

    [Inject]
    private ApiGateway Gateway { get; set; }

    private async Task Welcome_Click()
    {
        Welcome = await Gateway.GetWelcomeMessage();
    }
}

Keep in mind that you can also localize the text of your Blazor pages and components using the Persilsoft.Localizer package.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Persilsoft.Localizer.Blazor:

Package Downloads
Persilsoft.Membership.Blazor

Contains razor clases for use in frontend membership projects

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.9 224 5/25/2024
1.0.8 77 5/25/2024
1.0.7 111 5/22/2024
1.0.6 222 5/18/2024
1.0.5 92 5/18/2024
1.0.4 135 5/17/2024
1.0.3 90 5/1/2024
1.0.0 96 4/13/2024