Tvermose.BlazorLocalizer
10.2.0
dotnet add package Tvermose.BlazorLocalizer --version 10.2.0
NuGet\Install-Package Tvermose.BlazorLocalizer -Version 10.2.0
<PackageReference Include="Tvermose.BlazorLocalizer" Version="10.2.0" />
paket add Tvermose.BlazorLocalizer --version 10.2.0
#r "nuget: Tvermose.BlazorLocalizer, 10.2.0"
// Install Tvermose.BlazorLocalizer as a Cake Addin #addin nuget:?package=Tvermose.BlazorLocalizer&version=10.2.0 // Install Tvermose.BlazorLocalizer as a Cake Tool #tool nuget:?package=Tvermose.BlazorLocalizer&version=10.2.0
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.
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 <i>Program.cs</i> 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 <i>Program.cs</i> 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 IBlazorLocalizer
as 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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net7.0
- Blazored.LocalStorage (>= 4.4.0)
- Microsoft.AspNetCore.Components (>= 7.0.15)
- Microsoft.AspNetCore.Components.Web (>= 7.0.15)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
10.2.0 | 1,817 | 1/11/2024 | |
10.1.0 | 198 | 11/22/2023 | |
10.0.0 | 145 | 10/26/2023 | |
7.0.1 | 1,085 | 6/20/2023 | |
7.0.0 | 163 | 6/20/2023 | |
5.0.10 | 3,260 | 9/17/2021 | |
5.0.0 | 450 | 3/5/2021 | |
1.0.2 | 1,297 | 7/20/2020 | |
1.0.1 | 615 | 7/18/2020 | |
1.0.0 | 462 | 7/8/2020 | |
0.9.3 | 531 | 6/25/2020 | |
0.9.2 | 445 | 6/22/2020 | |
0.9.1 | 468 | 6/20/2020 | |
0.9.0 | 473 | 6/20/2020 |