How to Use Localization in an ASP.NET Core Web API
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
How to use Localization in ASP.Net Core Web API

How to Use Localization in an ASP.NET Core Web API

To serve people from any culture who use any language, most web applications nowadays have localization support.

Localization is a process that translates content for a specific culture. It can be used to serve a multilingual, worldwide audience and achieve better market reach.

In this blog, we are going to learn about how to use localization in an ASP.NET Core web API with resource (.resx) files.

Prerequisites

The following are the prerequisites to integrate localization in an ASP.NET Core web API:

Create an ASP.NET Core REST API application

Follow these steps to create an ASP.NET Core REST API application in Visual Studio 2019:

Step 1: In Visual Studio 2019 (v16.4 or higher), go to File > New, and then select Project.

Step 2: Choose Create a new project.

Step 3: Select the ASP.NET Core Web Application template.

Step 4:  Enter a Project name, and then click Create. The project template dialog will be displayed.

Step 5: Select the .NET Core, ASP.NET Core 3.1, API template (highlighted in the following screenshot).

Select .NET Core, ASP.NET Core 3.1, API Template
Select .NET Core, ASP.NET Core 3.1, API Template

Configuring start-up

Step 1: Register the necessary services for the localization process in the ConfigureServices method in the Startup.cs class: AddLocalization() and Configure<RequestLocalizationOptions>().

Add Localization Services
Add Localization Services

Step 2: Add the request localization middleware in the Configure method from the Startup.cs class.

Configure Request Localization Middleware
Configure-Request Localization Middleware

Strategy for resource files

Resource files contain localizable strings based on the culture and the controller. We can maintain the resource files in two ways:

  • Single culture resource file per controller.
  • Single culture resource file for all controllers.

Single culture resource file per controller

In this method, you need to maintain a separate resource file for each culture and each controller in any file structure (i.e. dot or path) as mentioned in this documentation.

Example:

If you have two controllers and support for two cultures, then you need to maintain the resource files in the structure shown in the following screenshot.

Here, we have support for the US English and French languages.

Resource File Structure
Resource File Structure

Single culture resource file for all controllers

In this method, you should maintain a single resource file based on culture for all the controllers.

To do this, you need to create an empty class with the same name as the resource file. The namespace of this empty class will be used to find the resource manager and resolve IStringLocalizer<T> abstractions.

Example:

Despite of the number of controllers, you should only maintain one resource file for every culture supported.

Refer to the following screenshot to see this approach in practice.

Resource File Structure
Resource File Structure

How to use localized values in controllers

ASP.NET Core provides built-in support for IStringLocalizer and IStringLocalizer<T> abstractions. These abstractions are used to get the values based on the name of the string resource passed from the respective culture resource file (e.g., HomeController.en-US.resx).

To use localized values in the controllers, you need to inject the IStringLocalizer<T> abstraction based on the resource file structure mapped as shown in the following table.

Controller NameDependency Injection of IStringLocalizer<T>Resource File Structure
HomeControllerIStringLocalizer<HomeController>Resources/Controllers/HomeController.en-US.resx
AboutControllerIStringLocalizer<AboutController>Resources/Controllers/AboutController.en-US.resx
For all controllersIStringLocalizer<Resource>Resource.en-US.resx

Based on injecting the IStringLocalizer<T> abstraction, the string localizer will map the resource file through the value passed to the shared resource (T). This string localizer will then return the mapped value from the resource file based on the name of the string resource you have passed.

IStringLocalizer Abstraction
IStringLocalizer Abstraction

Resources

You can find a copy of these samples in the following GitHub locations:

Conclusion

In this blog, we learned about implementing localization in an ASP .NET Core web API application. This feature helps to serve multilingual people from diverse cultures.

Syncfusion provides over 70 high-performance, lightweight, modular, and responsive ASP.NET Core UI controls such as DataGrid, Charts, and Scheduler, and all of them have built-in support for localization. You can use them to improve your application development.

Please share your feedback on this blog in the comments section. You can also contact us through our support forum, support portal, or feedback portal. We are waiting to hear from you!

Related blogs

Tags:

Share this post:

Comments (8)

abhijeet sharma
abhijeet sharma

can we have comman resource files for all controllers ?

Hi ABHIJEET SHARMA,

Yes we can have a single common resource file for all controllers per culture based. You can refer the below sample for your reference.

Localization Sample: https://github.com/RaguMP/Samples/tree/master/LocalizationSampleSingleResxFile

Regards,
Ragunaathan M P

Hello. I am wondering if I can move single resource culture file to any library project? Are does need to be part of the project where Controllers are present?

thanks,

Hi Uma,

Yes, we can maintain resource files in any library project and can refer in API project. But we need to maintain an empty class with the same name as the resource file where the namespace of this empty class will be used to find the resource manager, and resolve IStringLocalizer abstractions as explained in this blog.

Regards,
Ragunaathan M P

can you provide an example? I am struggling to make it work. I am using the resource file on another project

Hi Rashid,

Kindly find the below sample which I have prepared by referring resource files from another project.

Localization Sample: https://github.com/RaguMP/Samples/tree/master/LocalizationSampleSingleResxFileSeparateProj

Regards,
Ragunaathan M P

Hi Raghunaathan,

In the same approach can i externalize my resource files and put in CDN, so that when i add new language resource files there is no need to rebuild and deploy the application? also in that case can i load the resx file using some options like IConfiguration to map to resource manager and use it?

Regards,
Ravi

Hi Ravi,

In ASP.NET Core, localization resource files (i.e. “.resx”) will be generated with extension “.dll” based on each culture language we maintain, while building the project with “AddLocalization()” services which we add in service collection in Startup class. Hence resource manager can not be configured using “IConfiguration” and so it is better to maintain the resource files locally.

However we need to deploy our application whenever we plan to release new features on regular basis and include the localization changes along with the release.

Regards,
Ragunaathan M P

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed