Dotnet Core Localization

Emre Erkoca
Feb 20 · 3 min read

I had to make a multi-language application. Because there are a lot of languages and you might want to be global for more customers. I wanted to write a simple example before localization. I read documents and I felt a little bit confused. Then, I wanted to share my sample code.

You can store your text messages in a resource file. A resource record is composed of two parts. Name and value. Actually, there is one more part (comment) but probably you won’t use this part.

An example resource file looking like this:

You can create resource files for different languages.

Resource.resx, Resource.de.resx, Resource.es.resx, etc.

If you want to apply localization properly, names must be same in your resource files. When the localizer doesn’t find resource content, it returns null. I’ll explain this project step by step. https://github.com/emreerkoca/LocalizationExample

It’s a simple Dotnet Core Web API project.

  1. Add Resources folder to your project.
  2. Add your resource files.

3. Add a new folder to your project as “Localize” and add a public class to Localize folder.

If you want you can use different names but your resource names and your resources must be suitable. You will reach resource content through this empty class.

Resource name: Localize.Resource.de.resx

The namespace of your class is LocalizationExample.Localize and the class name is Resource.

4. Startup Configuration

Add this code block to into ConfigureServices(IServiceCollection services) method in Startup.cs file.

It’s adding supported languages for localizers. Showing your resource files and setting the default language.

Add this code block to Configure(IApplicationBuilder app, IWebHostEnvironment env) method.

It’s for automatically setting culture info for your localizer. You can set culture info by any request header value.

5. Showing Localized Messages

I used IStringLocalizer interface for reaching resource files. You can see an example usage here. Add dependencies and call the resource field name.

Get() method will return “Hello!” because English is the default culture and English resource value is “Hello!” When you add “culture=de” to your request URL, you’ll change the culture as “de” and you’ll see “Hallo!” message.

If you want you can detect the current language from the “Accept-Language” header. You can change your configuration.

Change your ConfigureServices configuration part like this:

It’s reading the “Accept-Language” header value and getting the default language. Put a breakpoint after the language variable and see values. It’s changeable for different browser languages. If you feel confused when you read CustomRequestCultureProvider, ProviderCultureResult, etc. you can read them from official Microsoft documents.

Now, you don’t have to add a culture parameter to request URL. You can try to get values from English and German browsers.

You can read this document for detailed information.

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-5.0

I have nothing to say after this. I hope it might be helpful.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store