Supporting many languages and regional settings is crucial when developing applications for people worldwide. This procedure, known as localization, makes your program more user-friendly for people from other cultures and nations. Using a real-world example, this tutorial will teach you how to add localization to an ASP.NET Core Web API.
What is Localization?
Localization (often shortened to L10n) means adapting your application to support different languages and regional settings. For web APIs, this means sending responses in the user’s preferred language. ASP.NET Core makes this easy with built-in localization support.
Let’s build a simple Web API that returns user information in different languages. We’ll work with a basic user profile that includes name, age, and gender fields. Our example will show how to display the gender field in both English and French, based on what language the client requests.
Creating the Sample Application
Set Up the Project
First, create a new Web API project using either the .NET CLI or Visual Studio. I am using .NET 9 in this article.
Define the Data Models
Create the User class and Gender enum to represent our data.
Add Sample Data
Create a helper class to provide some test data.
Set Up Resource Files
Resource files store our translated text. Here’s how to set them up.
- Create a Resources folder in your project
- Add a marker class called SharedResource.
Add two resource files
- SharedResource.en.resx for English
- SharedResource.fr.resx for French
Add these translations to your resource files.
SharedResource.en.resx.
- Female → Female
- Male → Male
- Other → Other
SharedResource.fr.resx.
- Female → Femme
- Male → Homme
- Other → Autre
Create the Localization Service
We’ll create a custom service to handle translations.
In .NET Core, the IStringLocalizer<T> interface is commonly used to handle string localization. In our custom localizer service (AppLocalizer), instead of directly using IStringLocalizer<T>, we inject an instance of IStringLocalizerFactory. This factory allows us to dynamically create an IStringLocalizer specifically tied to the SharedResource class. We can now use this AppLocalizer class for localizing any translation string from our shared resource file.
Configure the Application
Update your Program.cs to enable localization.
By configuring the request localization middleware, we are performing the following.
- We define the supported cultures by our API. We are defining English (en) and French (fr) as the supported cultures.
- We set the default culture to English by calling the SetDefaultCulture(supportedCultures[0]), which specifies the fallback culture if no/unsupported culture is specified in the request.
- app.UseRequestLocalization(localizationOptions) activates middleware to,
- Detect the culture from incoming requests (e.g., Accept-Language header).
- Set the application’s culture for formatting and resource localization.
Create the API Endpoint
First, create a DTO to shape our response.
Then create the controller.
The controller class accepts an instance of IAppLocalizer through dependency injection, and we can use that instance to localize the Gender property.
Testing the API
You can test the API using cURL or any API testing tool. Here’s how to test for different languages.
For English
For French
We can see that the gender field is localized.
If you don’t specify a language or use an unsupported one, the API will default to English.
Summary
We’ve created a Web API that can respond in different languages using ASP.NET Core’s localization features. The key points we covered.
- Setting up resource files for different languages
- Creating a custom localization service
- Configuring the application to handle language preferences
- Building an API endpoint that returns localized data
This approach makes it easy to add support for more languages later – just add new resource files for each language you want to support.
Best ASP.NET 8.0.8 Core Hosting
The feature and reliability are the most important things when choosing a good ASP.NET Core hosting. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core , their servers are optimized for PHP web applications such as the latest ASP.NET Core version. The performance and the uptime of the ASP.NET Core hosting service are excellent, and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. At HostForLIFEASP.NET, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its data centers are equipped with top equipment like cooling system, fire detection, high-speed Internet connection, and so on. That is why HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core . And the engineers do regular maintenance and monitoring works to assure its ASP.NET Core hosting are security and always up.