WMBlazorInternationalization 1.0.1
dotnet add package WMBlazorInternationalization --version 1.0.1
NuGet\Install-Package WMBlazorInternationalization -Version 1.0.1
<PackageReference Include="WMBlazorInternationalization" Version="1.0.1" />
paket add WMBlazorInternationalization --version 1.0.1
#r "nuget: WMBlazorInternationalization, 1.0.1"
// 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
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 isen
, but you can choose anyone ornull
;fileName
: The default isLocale
, but you can choose anyone ornull
;filePath
: The default isi18ntext/
, but you can choose anyone ornull
;storageType
: The default islocalStorage
, but you can choosesessionStorage
andnull
. 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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. 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. |
-
net5.0
- Microsoft.AspNetCore.Components.Web (>= 5.0.0)
- System.Net.Http.Json (>= 5.0.0)
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 | 514 | 2/8/2021 |