ASP.NET Core MVC Localization and It’s BUG!!

Localization is big thing for international websites. And I needed use it when I was building MottoJoy.com.

There is very good explanation/example for that in ASPNET repo on GitHub. It in a solution named as Entropy and here is the link: https://github.com/aspnet/Entropy/tree/dev/samples/Mvc.LocalizationSample.Web

But there are some missed points.

There are 4 types of localization methods comes with standard library. You may also implement your own.

  • By URL query strings QueryStringRequestCultureProvider
  • By request header AcceptLanguageHeaderRequestCultureProvider
  • By cookie CookieRequestCultureProvider (This one is used in Entropy repository)
  • And by routing data RouteDataRequestCultureProvider

I tried all of them and they work very well except ‘localization by routing data’.

First Question Is ‘Which One To Use?’

I care too much about SEO. And I think when building website it is all about SEO. Driving traffic/visitor/customer to website for free. So that is most important thing in this matter: ‘SEO point of view’.

When we say SEO, of course Google is our king. There is an article about ‘Multi-regional and multilingual sites’ by Google. One of the most important thing we see in this article is in these lines:

Keep the content for each language on separate URLs. Don’t use cookies to show translated versions of the page. Consider cross-linking each language version of a page.

Thus, ‘localization by cookie’ and ‘localization by request header’ is out of question.

Also in this article there is table for ‘URL structures’ and at it’s last row it says URL parameters is not recommended for localization. And that leaves ‘localization by query strings’ out of question, too.

We Have Been Left With ‘Localization by Routing Data’ and It Has A BUG!!

Actually some developers wouldn’t call it a bug maybe. It is not crashing but it is not working either. So it is pretty good bug for me.

When you use RouteDataRequestCultureProvider class as RequestCultureProviders it fails without crashing or raising error. After spending two hours to find what causes that problem, I come across with this post on Stack Overflow. And i got an idea to solve it.

Problem

When localization classes are called ASP MVC wouldn’t be generated routing data for HttpContext class. So RouteDataRequestCultureProvider trying to decide which culture to use with null routing data and of course every time it tries to resolve routing data, it can't find anything to resolve and returns default culture.

Solution

You can find how I solve this problem in this post.

That’s it.

I hope this post has helped you.

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

Written by

MSc. Computer Engineer. Founder of @noteducom and @image4io, 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