Persilsoft.Localizer.Blazor 1.0.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package Persilsoft.Localizer.Blazor --version 1.0.6                
NuGet\Install-Package Persilsoft.Localizer.Blazor -Version 1.0.6                
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.6" />                
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.6                
#r "nuget: Persilsoft.Localizer.Blazor, 1.0.6"                
#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.6

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

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.18 232 10/16/2024
1.0.17 115 10/16/2024
1.0.16 84 10/15/2024
1.0.15 103 10/13/2024
1.0.14 214 8/28/2024
1.0.13 148 8/17/2024
1.0.12 87 7/29/2024
1.0.11 83 7/29/2024
1.0.10 252 7/20/2024
1.0.9 369 5/25/2024
1.0.8 111 5/25/2024
1.0.7 144 5/22/2024
1.0.6 264 5/18/2024
1.0.5 126 5/18/2024
1.0.4 180 5/17/2024
1.0.3 129 5/1/2024
1.0.0 124 4/13/2024