0

I need to implement localization for resource (.resx) messages in a MVC application, but have been failing to do so.

Reviewing the steps I am following:

1. Create a Localization project and add as many resource (.resx) files as the languages you need to support. Add the messages you want to display, in all the chosen languages. Select "Public" at Visual Studio's "access modifier" option. Make sure you name the resource files according to the required convention, i.e., MyResource.resx for the default language, MyResource-fr.resx for french localization, MyResource.ro for romanian localization, an so forth.

2. Detect and parse the Accept-Language http header from the http request. Pick the one with the highest 'q' factor, whenever present. If none is present or all q values are the same, pick the 1st language accepted.

(Update: I just learned (see @Zealous comment) that adding <globalization enableClientBasedCulture="true" culture="auto" uiCulture="auto"/> to web.config will do exactly the same, so no need for the manual work).

3. Make the application controllers inherit from a base controller (could have been done via filter as well) that changes the references of Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture to new instances created on the fly, according to the most preferred language accepted, as extracted from the preceeding step.

4. Add a reference to the Localization project created in the step above to the one that needs to reference the messages. Upon referencing a message, simply refer to the resource default file name 'dot' the message you want to display. In our example, it would be MyResource.Welcome.

Expected results: upon sending http requests set with Accept-Language to 'fr', receive a response with "Bienvenue", whereas when seting Accept-Language to 'en' (or adding several languages and playing with the q factor), receive a response with "Welcome".

Somehow my responses are always using the default resource file (in this case, English).

What am I missing or am doing wrong?

Veverke
  • 9,208
  • 4
  • 51
  • 95
  • 1
    Complete Localization explained here: http://devproconnections.com/aspnet/aspnet-mvc-localization and http://geekswithblogs.net/shaunxu/archive/2012/09/04/localization-in-asp.net-mvc-ndash-upgraded.aspx – Zealous System May 06 '15 at 09:21
  • @Zealous: indeed, using the web config settings I can drop all the "base controller" stuff I did manually. I checked the thread culture and ui culture, both are correctly set, but I still get the default language. Just like the result I was getting from the start. – Veverke May 06 '15 at 09:54

1 Answers1

0

Problem solved, leaving the answer here for others:

My accept-language header was set to "he, he-IL" and the CLR was always picking the default language resource. I had a resource for the default language and another for he-IL.

I was thinking that for the CLR to determine the proper resource file both he and he-IL were the same. This is not true. Although I learned that "he" alone is legacy and should not appear anymore these days, the solution was to either add a 3rd resource to map the "he" header - named MyResource.he.resx - or to remove "he" from the accept-language header leaving only "he-IL", that was already mapped.

Veverke
  • 9,208
  • 4
  • 51
  • 95