Latoken lists BXT

BXT is open for trading on LATOKEN! Trading for BXT/ETH and BXT/LA trading pairs is now live. TRADE NOW. Latoken users can begin trading BXT and LA on the website at Latoken.com. To deposit and trade…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Blazor WebAssembly I18n from Start to Finish

Blazor is a strong C#-based framework from Microsoft that allows us to create an interactive, client-side web user interface (UI) using .NET. In this tutorial, we will explore how to implement internationalization (i18n) in a Blazor WebAssembly app.

This guide will walk you through the Blazor WebAssembly i18n process from start to finish. We will do so by creating a sample form to add the data of an employee joining a company. The employee data saved via the form will eventually be displayed in a table. We will serve our app in three languages: English, French, and Arabic. We will allow the user to select the locale of the app using a drop-down list.

An open-source .NET web framework, Blazor makes it possible to create client-side applications using C# and HTML. It enables us to create rich and interactive UIs using C# instead of JavaScript. It further allows us to render the UI as HTML and CSS with extensive browser support, including mobile browsers.

Blazor provides a full-stack .NET development experience by allowing us to use .NET throughout our application. We can create both the server and client-side of the application using the same language, such as C#. It also allows us to share the common model class across the client and server.

Blazor has a component-based architecture — a component in Blazor can be defined as a UI element, such as a form, nav menu, data grid, etc.

Let us have a look at the features that make Blazor an amazing framework for web development.

To display and update the UI changes in the browser, Blazor uses different renderers. These renderers are more commonly referred to as hosting models. Currently, the Blazor framework supports two hosting models, as explained below:

This is the primary hosting model of Blazor, which allows running the client-side in the browser with the help of WebAssembly. When the Blazor WebAssembly app is built and executed, the app, its dependencies, and the Mono .NET runtime are downloaded to the browser. The app is executed directly on the browser UI thread. UI updates and event handling occur within the same process.

The Blazor Server model allows the Blazor application to run on the server on top of the full .NET Core runtime. When the app is launched, a small JavaScript file is downloaded, which establishes a real-time, two-way SignalR connection with the server. Any user interaction with the app is then transmitted back to the server over the SignalR connection for the server to process. After the server is done, the UI updates are transmitted back to the client and applied to the DOM.

The Blazor framework mainly uses C# or Razor code to create its components, but there are still a few scenarios where we need to access JavaScript:

So, how can Blazor access JavaScript code? The ability to access a JavaScript method using a high-level language such as C#, and vice versa, is achieved through JavaScript Interop. JavaScript interop is a feature of WebAssembly, and therefore Blazor can implement it.

We can use the following two ways to work on a Blazor app.

In this tutorial, we will be using Visual Studio 2019. Please install the following software:

Open Visual Studio 2019, click on “Create a new project”. Select “Blazor App” and click on the “Next” button. Refer to the image shown below.

On the next window, put BlazorWebassemblyI18n as the project name and click on the “Create” button. You’ll be then asked to select the type of Blazor app, as well as the .NET version. Select “Blazor WebAssembly App”. Set the .NET version to .NET 5.0 from the dropdown on the top. Click on the “Create” button to create a new Blazor WebAssembly application (refer to the image below).

We will now install the Localization package, providing application localization services and default implementation based on ResourceManager to load localized assembly resources.

To install the package, navigate to Tools > NuGet Package Manager > Package Manager Console. It will open the Package Manager Console. Run the command as shown below.

We will add a new component to our Blazor project, allowing us to select the app language during runtime.

To add a new component, right-click on the BlazorWebassemblyI18n/Pages folder and then select Add > Razor Component. Put the name of the file as LanguageSelector.razor. Click on the Add button. Refer to the image shown below.

Put the following code inside the LanguageSelector.razor file.

We have created a drop-down list to display the supported language for our app. We are supporting three languages — English, French, and Arabic. Upon selecting the value from the drop-down, we will store the selected language in the local storage with the help of JS interop and reloads the app.

Open the BlazorWebassemblyI18n/wwwroot/index.html file and add the following lines of code at the end of the <body> tag.

This method is used to get and set the app culture from the local storage.

We will add the language selector component to the MainLayout component. This will make sure that it is accessible on all the pages.

To add a Blazor component to another, we need to use the file name of the component as the tag name. Therefore, we will add the following line to the BlazorWebassemblyI18n/Shared/ MainLayout.razor page. Add this line inside the div element having the class as top-row.

To support the feature of changing the application language dynamically, we need to configure BlazorWebAssemblyLoadAllGlobalizationData in the project file. Open the BlazorWebassemblyI18n\BlazorWebassemblyI18n.csproj file and add the following code to it:

To create the EmployeeData component, we will add the EmployeeData.razor file to the BlazorWebassemblyI18n/Pages folder. Add the following lines at the top of the file.

We have defined the route for this component as “/employee”. We have also injected the IStringLocalizer class and set an alias name to “Localize”. We will use the alias name to localize the strings in this component.

Add the following piece of code in the @code section of the file.

The SaveEmployeeToLocalStorage method will add the newly added employee to the lstEmployees variable. We will then serialize lstEmployees to string and save it to the local storage with the help of JS Interop. This will ensure that the employee data does not get lost when the app loads.

The OnInitializedAsync is a Blazor lifecycle method that is invoked when the component is initialized. If the data exists in the local storage, we will fetch the list of employees and set the lstEmployees variable.

We will use interpolation to display the title of the page. The title will contain the company name i.e. Phrase. The method setTitle will fetch the translated content from the resource file and then we will use the string format to add the company name dynamically.

Finally, add the following HTML code to the component.

We have defined a form that will allow the user to add a new employee record. To localize the labels in the form, we are using the Localize alias of the IStringLocalizer class. We have defined a table just below the form, which will display the list of employees. The character “C” is the currency format specifier that will convert a number to a string to represents a currency amount. The integer succeeding the character “C” is known as the precision specifier which indicates the desired number of decimal places. Clicking on the Save button will submit the form which will then invoke the SaveEmployeeToLocalStorage method.

To get and set the employee data to the local storage we will add the following JS code in the BlazorWebassemblyI18n/wwwroot/index.html file at the end of the <body> tag.

For this sample app, we have saved the employee data in local storage. However, in a real-world app, the data should be saved to a database.

Right-click on the project and select Add > New Folder and name the folder “Resources”. Again, right-click on the Resources folder and select Add > New Item. In the Add New Item window, select the Resources File template and put the name of the file as App.resx. Click on the Add button to create the file (see below):

A resource file is used to manage the translated content. The resource file will contain a set of key-value pairs for the translation. The key will be the text to be translated, and the value will be the text after translation. We will add the Name and Value field for all the text we want to translate (refer to the image below):

We have created a language selector component to select the app language during runtime. We have also created a set of language resource files to be used for each language. Now we need to configure the app to dynamically set the culture of the app. Add the following lines inside the Main method of the BlazorWebassemblyI18n/Program.cs file.

We will fetch the current app language from the local storage using JS interop and set the app culture. The culture will define the locale for the app. This piece of code will help us to set the locale as the app loads.

The app will load only the resource file required for a particular locale. When we change the language from the dropdown, the app will reload with the resource file for another locale.

Before running the app, we will add the link of our EmployeeData component in the navigation menu. Open BlazorWebassemblyI18n/Shared/NavMenu.razor file, and add the following code to it.

Launch the application. Click on the Employee Data button on the nav menu on the left. You can see the output as displayed below.

In this tutorial, we explored how to internationalize a Blazor WebAssembly app. We created a sample form to accept employee data for a company. The app is served in three languages — English, French, and Arabic.

Add a comment

Related posts:

Can I Ask for Alone Time for My Birthday?

My husband asked me what I wanted to do for my upcoming birthday. I responded, facetiously, “to be left alone.” My husband stared at me for a second and responded with a “fine.” Sitting in awkward…

Elasticsearch Quick Start

Elasticsearch is an open-source tool for a store and search engine for all types of data ( textual, numerical, geospatial, structured, and unstructured data) in real-time. Elasticsearch based on…

The Wilted Willow

Buds swell and sway as the night carries on whispering comforting glimpses of the suns warm gaze of the morrow The shrill of her branches die whilst her bough creaks; swaying never faltering to the…