WMBlazorInternationalization 1.0.1

Install-Package WMBlazorInternationalization -Version 1.0.1
dotnet add package WMBlazorInternationalization --version 1.0.1
<PackageReference Include="WMBlazorInternationalization" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WMBlazorInternationalization --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: WMBlazorInternationalization, 1.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install WMBlazorInternationalization as a Cake Addin
#addin nuget:?package=WMBlazorInternationalization&version=1.0.1

// Install WMBlazorInternationalization as a Cake Tool
#tool nuget:?package=WMBlazorInternationalization&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

WM Blazor Internationalization

Summary

This package is an another way to localize text and html blocks in your Blazor Web App! (internationalization, localization, translation, ...)

  • Dotnet Version: 5.0
  • It works on both Blazor Server and Blazor WebAssembly.
  • Your translations come from a json file.

Live Demo

https://welisonmenezes.github.io/wm-blazor-internationalization/

NuGet Package

https://www.nuget.org/packages/WMBlazorInternationalization/

GitHub Repository

https://github.com/welisonmenezes/wm-blazor-internationalization

Configuration

First, import the namespaces in _Imports.razor

@using WMBlazorInternationalization
@using WMBlazorInternationalization.WMBI

Then, add the WMBInternationalization component to wrap all the components in your App.razor. You can do it by wrapping the Router component of the aplication.

Note that the ChildContent must wrap the app content, the ChildLoading must wrap the content shown when the localization is loading and the ChildError must wrap ther error content when the localization is fail.

<WMBInternationalization>
    <ChildContent>
        <Router AppAssembly="@typeof(Program).Assembly">
            <Found Context="routeData">
                <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
            </Found>
            <NotFound>
                <LayoutView Layout="@typeof(MainLayout)">
                    <p>Sorry, there's nothing at this address.</p>
                </LayoutView>
            </NotFound>
        </Router>
    </ChildContent>
    <ChildLoading>
        <p>Loading your localization data.</p>
    </ChildLoading>
    <ChildError>
        <p>An error was occurred while loading your localization.</p>
    </ChildError>
</WMBInternationalization>

Parameters

You can also customize some things by passing parameters

Example:

<WMBInternationalization defaultLanguage="pt" defaultFileName="Locale" defaultFilePath="i18ntext/" storageType="sessionStorage">
    <ChildContent>
        <Router AppAssembly="@typeof(Program).Assembly">
            <Found Context="routeData">
                <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
            </Found>
            <NotFound>
                <LayoutView Layout="@typeof(MainLayout)">
                    <p>Sorry, there's nothing at this address.</p>
                </LayoutView>
            </NotFound>
        </Router>
    </ChildContent>
    <ChildLoading>
        <p>Loading your localization data.</p>
    </ChildLoading>
    <ChildError>
        <p>An error was occurred while loading your localization.</p>
    </ChildError>
</WMBInternationalization>
  • defaultLanguage: The default is en, but you can choose anyone or null;
  • fileName: The default is Locale, but you can choose anyone or null;
  • filePath: The default is i18ntext/, but you can choose anyone or null;
  • storageType: The default is localStorage, but you can choose sessionStorage and null. When null the selected language will not be persisted;

When parameters are null, the default values will be assumed.

Setting up the translations

Inside the wwwroot directory, create a folder called i18ntext and, inside that, create a json file for each language you want. That is important to name this files as following:

Locale.[language-code].json

Examples:

Locale.en.json
Locale.pt.json
Locale.pt-br.json
Locale.es.json

Note that i18next and Locale values can be changed as shown above.

Create localized text source files as JSON

Example:

{
    "Key1": "Localized text 1",
    "Key2": "Localized text 2",
    ...
}

Using in pages and components

Inside your component, get the WMBInternationalization functionalities by the CascadingParameter named CascadeWMBI

@code {
    [CascadingParameter(Name = "CascadeWMBI")]
    protected WMBInternationalization WMBI { get; set; }
}

Now you has access to 3 methods: GetTranslation, SetLanguage and GetCurrentLang.

GetTranslation

By GetTranslation you can read any key property from the json file regarding current language.

Example:

<h1>@WMBI.GetTranslation("HelloWorld")</h1>

The param is the json key you want to access from the Locale json file.

SetLanguage

By SetLanguage you can choose another language.

Example:

WMBI.SetLanguage("pt");

The param is the language code you want to be used.

GetCurrentLang

By GetCurrentLang you can see the current language code.

Example:

@WMBI.GetCurrentLang()

By this method you can even choose show some html blocks depending on the selected language.

Example:

@if(@WMBI.GetCurrentLang() == "en")
{
    <img src="img/reino-unido.png">
}

@if(@WMBI.GetCurrentLang() == "pt")
{
    <img src="img/brasil.png">
}

For live demo, download it from github and run, either BlazorServerDemo or BlazorWasmDemo project.

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
1.0.1 93 2/8/2021