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.
https://www.nuget.org/packages/WMBlazorInternationalization/
https://github.com/welisonmenezes/wm-blazor-internationalization
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>
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.
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.
Example:
{
"Key1": "Localized text 1",
"Key2": "Localized text 2",
...
}
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
.
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.
By SetLanguage
you can choose another language.
Example:
WMBI.SetLanguage("pt");
The param is the language code you want to be used.
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">
}