-
Notifications
You must be signed in to change notification settings - Fork 2
Steps to localize your ASP.NET Core app
Jon P Smith edited this page Jan 7, 2023
·
8 revisions
I didn't find it obvious how to set up .NET localization in ASP.NET Core, so here is a quick list of what you have to set up to help you. The information is already out there, so this is a list of links to Microsoft documentation.
- Configure localization which sets up the localization services and the name of the folder that holds the resource files (see step 3 later).
- Configure middleware which sets up what cultures you want to support, and ways to get the user's culture.
- Create a new class, or use an existing class (e.g.
HomeController) to define part of the resource files’ name - I refer to this class as TResource. These TResource classes must be in the ASP.NET Core project. -
Add the resource files you need where each resource file that has a filename of
{typeof(TResource).{culture name of entries}.resx
I found the Microsoft document didn't have nice step-by-step section on how to setup localization in an ASP.NET Core app, here are some other articles that you might find useful:
- Building Multilingual Applications in ASP.NET Core – excellent step-by-step article
- Localization in ASP.NET Core Web API – another excellent step-by-step article (.NET 6)
- ASP.NET Core Localization Deep Dive – shows all the different localization parts
NOTE: If you are using the DefaultLocalizer and SimpleLocalizer for all the localization, then you won't need a resource file for the defaultCulture messages, as the strings / FormattableStrings used in these services already have the correct message.
If you want examples of adding localization to an ASP.NET Core app, then have a look at:
-
The
Startupin Microsoft's example code. Note this old, (netcoreapp2.1) but localization haven't changed. -
The
Programin my .NET 6 example in the Net.LocalizeMessagesAndErrors repo. This uses the culture of the request to show English or French/ -
The
Programin Example1 app in the AuthP repro. This uses a cookie to set the culture. You can select between English (default) and French. - Other example code in the listed articles.