Localizer.Blazor
1.0.7
You can use Persilsoft.Localizer
dotnet add package Localizer.Blazor --version 1.0.7
NuGet\Install-Package Localizer.Blazor -Version 1.0.7
<PackageReference Include="Localizer.Blazor" Version="1.0.7" />
paket add Localizer.Blazor --version 1.0.7
#r "nuget: Localizer.Blazor, 1.0.7"
// Install Localizer.Blazor as a Cake Addin
#addin nuget:?package=Localizer.Blazor&version=1.0.7
// Install Localizer.Blazor as a Cake Tool
#tool nuget:?package=Localizer.Blazor&version=1.0.7
Localizer.Blazor
A simple localization tool for Blazor apps
First, you need to register the Localization service
builder.Services.AddLocalization();
Then, set culture settings.
var AppHost = builder.Build();
var jsRuntime = AppHost.Services.GetRequiredService<IJSRuntime>();
var RegionCode = await jsRuntime.InvokeAsync<string>("localStorage.getItem", "culture");
CultureInfo Culture = CultureInfo.CreateSpecificCulture("es-PE");
if (RegionCode != null)
{
Culture = CultureInfo.CreateSpecificCulture(RegionCode);
}
CultureInfo.DefaultThreadCurrentCulture = Culture;
CultureInfo.DefaultThreadCurrentUICulture = Culture;
await AppHost.RunAsync();
Localized messages must be extracted from the properties of the class that interacts with the resource file, for example:
public class Messages
{
public static string Home =>
LocalizationUtils<Messages>.GetValue(nameof(Home));
public static string About =>
LocalizationUtils<Messages>.GetValue(nameof(About));
public static string Hello =>
LocalizationUtils<Messages>.GetValue(nameof(Hello));
public static string Welcome =>
LocalizationUtils<Messages>.GetValue(nameof(Welcome));
}
It is necessary that the name of the resource files follow the following format:
{Extractor}.{Culture}.{resx}
Where:
Extractor: The name class to used to interact with the resource files, for example: Messages
Culture: The culture identifier, for example: en-US, es-PE
resx: This is the file extension for resource files
Finally, you can use them:
@page "/"
<PageTitle>@Messages.Home</PageTitle>
<h1>@Messages.Hello</h1>
@Messages.Welcome
To change the culture settings, we can use the CultureSelector component, for example in the MainLayout:
<CultureSelector SupportedCultures="SupportedCultures" />
@code {
private string[] SupportedCultures;
protected override async Task OnInitializedAsync()
{
SupportedCultures = new string[] { "en-US", "es-PE" };
await Task.CompletedTask;
}
}
Product | Versions 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. |
-
net8.0
- Localizer.Tools (>= 1.2.6)
- Microsoft.AspNetCore.Components.Web (>= 8.0.3)
- Microsoft.Extensions.Http (>= 8.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Localizer.Blazor:
Package | Downloads |
---|---|
ZMembership.Blazor
Membership Blazor |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|