Developing A Multilingual Application Using .Net Core

Developing A Multilingual Application Using .Net Core

Implementing a multilingual application requires a complex infrastructural setup. So, in this article we will use NuGet package “LazZiya.ExpressLocalization”.

Follow the steps as given below:

Create a .net core application. Select ASP.NET core application and click the “Next” button.

create new project

Assign the name and directory where you want to make an application and press the “Create” button.

configure new project

Select “Web Application” and press the “Create” button.

create new web application

ExpressLocalization Setup

Now Application setup is done. So the next step is to install NuGet package “LazZiya.ExpressLocalization”.

On solution explorer right click on “Dependencies” and select “Nuget Packages”.

solution explorer

Select the “Browse” tab and type “LazZiya.express” and install the package.

browse

Install “LazZiya.TagHelpers” and install the package.

Create a new folder “LocalizationResources”.

LocalizationResources

Add new class “LocSource” inside recently added folder “LocalizationResources”

Add the following code in “Startup.cs” file to setup localization in application.

Open “_ViewImports.cshtml” file and write the following code.

Add the following code where you want to display language selection dropdown list. As of now I have added a language dropdown in the “_Layout.cshtml” file.

layout cshtml

Add resource file for each language that you have added in Startup.cs file

Explorer

Localizing Views

There are several ways to implement localization in View.

Option 1 : – To implement option 1 we have to add tag helper to “ _ViewImports.cshtml”.

Use localize html tag inside view.

<h1 localize-content>Welcome</h1>

OR

<localize>Welcome</localize>

Open Index.cshtml page and add the following code.

Now, add name value collection in all the resource files.

add name value

Use the same process to localize texts in all views.

Option 2: – To implement option 2 we have to inject culture localizer to the views and call its method.

Open Pages “_ViewImports.cshtml” file and inject ISharedCultureLocalizer that already comes with ExpressLocalization.

@using LazZiya.ExpressLocalization

@inject ISharedCultureLocalizer _loc

Below is the example of Option-1 and Option-2.

examples

How to use localization for DataAnnotations:-

ExpressLocalization has inbuilt data annotation attributes. Using these attributes, we can localize DataAnnotations messages.

ExpressLocalization

Now create a new view for registration as below.

new view

Create a new action method in HomeController to open the registration page.

Now open the Registration page and click the “Create” button.

Registration

We hope you have an understanding of how to develop a multilingual application using .Net Core now. In case of any queries, do get in touch with an expert at DEV IT here.

The following two tabs change content below.
Sachin Patel works in the capacity of a technical architect at DEV IT. With over ten years of working with .NET technologies, Sachin is genuinely an expert solutions provider with a firm hand on C# back-end, front-end, database, release management, cloud, etc. He's also certified by Microsoft as a trained MCP.

Latest posts by Sahin Patel (see all)

Leave a Reply

Your email address will not be published.