Skip to content

Latest commit

 

History

History
76 lines (55 loc) · 2.61 KB

README.md

File metadata and controls

76 lines (55 loc) · 2.61 KB

BlazorLocalizer

A library to provide asynchronous localization in Blazor WebAssembly applications. Caches localized values in memory and i localStorage for a configurable amount of time.

Nuget

Installing

You can install from NuGet using the following command:

Install-Package Tvermose.BlazorLocalizer

Or via the Visual Studio package manager.

Setup

You will need to implement the interface BlazorLocalizer.IResourceProvider and register the implementation with the service collection in your Program.cs in Blazor WebAssembly.

Likewise you will need to register the IBlazorLocalizer by using the extension method AddBlazorLocalization().

public static async Task Main(string[] args)
{
    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    builder.RootComponents.Add<App>("app");
    
    builder.Services.AddTransient<BlazorLocalizer.IResourceProvider, YourResourceProviderImplementation>();
    builder.Services.AddBlazorLocalization();

    await builder.Build().RunAsync();
}

Configuration

BlazorLocalizer provides options that can be modified by you at registration in your Program.cs file in Blazor WebAssembly. Localized values will per default be cached for 3 days using localStorage. This can be modified or disabled using the LocalStorageOptions as shown below.

public static async Task Main(string[] args)
{
    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    builder.RootComponents.Add<App>("app");
    
    builder.Services.AddTransient<BlazorLocalizer.IResourceProvider, YourResourceProviderImplementation>();
    builder.Services.AddBlazorLocalization(config => {
      config.LocalStorageOptions.CacheInvalidation = TimeSpan.FromDays(1);
      config.LocalStorageOptions.CacheDisabled = false;
    });

    await builder.Build().RunAsync();
}

Usage (Blazor WebAssembly)

To use BlazorLocalization in Blazor WebAssembly, inject the IBlazorLocalizeras in the example below.

@inject BlazorLocalizer.IBlazorLocalizer loc

<p title=@_title>
  @loc["Localize this text using the calling class fullname as category"]
  @loc["Localize this text", "Example.Category"]
</p>

@code {

    private string _title = "Title for localize this text";
  
    protected override async Task OnInitializedAsync()
    {
        _title = await loc.L(_title, "Example.Category");
    }

}

If you do not provide a category for the localized text, the calling class fullname will be used as category.

Usage (Blazor Server)

Not currently supported.