ASP.NET Core MVC Localization by URL (RouteDataRequestCultureProvider)

ASP.NET Core comes with new localization methods. There are 4 types of methods comes with standard library. But one is not working properly. You can read why it is not working in this post.

In this post I’m going to show a simple implementation of localization by URL.

Source Repo! You can find fully functional sample built on ASP.NET Core 2.0 of this post at GitHub repository irensaltali/AspNetCoreMVCLocalizationByURL

  • I need URL based localization.
  • It has to be in this form: example.com/{twoLetterLanguageName}/...
  • It has to be compatible with standard routing.

Documentation! To find out more about localization and globalization in ASP.NET Core please read the official documentation about it at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization.

Actually ASP.NET Core has a class for this but it is not working. So here my way of fixing that and implementation. I’ll set up for two language, Turkish and English. You may set it up for countless more languages same way.

Let’s start with Startup.cs. Modify ConfigureServices() method first.

Above we set path of resource files for localization, method for recognize matching files (according to suffix), and default and supported culture (languages). You don’t have LanguageRouteConstraint class and overrided RouteDataRequestCultureProvider class yet.

Now modify Configure() method.

Above we set localization option and routing map for URL localization.

LanguageRouteConstraint class is below. You may put this into Startup.cs or anywhere you like.

Override RouteDataRequestCultureProvider class is below. It is like ASP.NET's implementation but this one is working :)

As you see above I used two letter language identifier to provide more beautiful URLs.

Finally, we are setting controller method to redirect URL without culture identifier to default culture. We set MapRoute for this in Startup.cs. This mapping routes to RedirectToDefaultLanguage action in Home controller.

After applied this last action method localization is done but there are few things to do.

Add Resource Files

You can think of resource files as translation files. Resource files based Key-Value pairing. You can use any string as key but I strongly recommend to use same string as default language texts. Because these keys will be used as text in case of error in defining current language or referencing resource file.

You can find detailed explanation for Resourse File Naming. You can use two different types naming methods (Dot and Path). I used both of them as you see above.

Example of Localized View

Example of Localized Controller

That’s it.

I hope this post has helped you.

Originally published at irensaltali.com on February 11, 2018.

MSc. Computer Engineer. Founder of @image4io, .NET Software Consultant @kloia_com, co-organizer in @serverlesstr, Community Builder of @awscloud.

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