Localization 2

Today I am diving deeper into localization for ASP.Net.  This post follows the my previous post, “Localization and bit of SEO“.   I have followed much better written article by Nadeem Afana http://afana.me/post/aspnet-mvc-internationalization.aspx, and updating my old post.

Once you have set your ASP.Net MVC to work with multiple languages with resource files, then you will want to set default language of user, and also let user choose language and keep that preference in browser.

Let’s first see what user’s default language is from HTTP request header.

I have gone to this link https://www.whatismybrowser.com/detect/what-http-headers-is-my-browser-sending, then get following information.


This means my HTTP request header say I prefer Japanese.  Most browser let you set this from settings. Chrome, Edge, Firefox.

Let’s make a ActionFulterAttribute that you can call to make your controller Internationalize.

This will take care of getting default browser’s preferred language and cookie stored preferred languages.

Next is to to write CultureHelper class to help handle culture related handlings.

*Note that I am returning null for default culture.  If you simply want to return preset default language you can uncomment it and return default culture, but I am using returning null here and let Internationalized filter handle the default language.

Now that we can handle default cultures, let’s make a controller to write prefer culture setting to cookie.

OK, great, now let’s make sure URL reflect culture settings.  Routing is done at App_Start/RouteConfig.cs

Now URL always include culture code,  and serve web service in user’s preferred language.  The last step is to add ActionFilter to your controller.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.