Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good way to changing language resources dynamically by request

I have an ASP.NET Web API application which should react to user's Accept-Language header appropriately.

Currently, the strings are stored in the resx and accessed in compile-safe manner through Visual Studio's generated class. What I would like to do is to keep the current approach and create satellite assemblies for each translated version of resx. Then to analyze the user's Accept-Language header to see what languages a user accepts and load the resources for the requested language from the satellite assembly.

I suppose I could implement all this behavior myself by creating a set of language-specific ResourceManager objects with the help of the ResourceSet but then it would not be possible to keep the compile-time safety, since Visual Studio takes care of automatically updating the class for resx file.

What would be the best way to pick the localized language resource dynamically?

like image 758
paulius_l Avatar asked Jun 18 '12 07:06

paulius_l


1 Answers

From reading your question, I don't see anything that ASP.NET doesn't offer automatically. You can configure your ASP.NET (whether WebForms or MVC) to use the accept-language request header and set the appropriate UICulture (which will impact which satellite assembly is loaded by ResourceManager) and Culture (which will impact locale-dependent formatting and parsing such as dates and numbers) appropriately.

To configure your app to use the accept-language list to set both UICulture and Culture for each request, (as per this MSDN page), configure your web.config like this:

<globalization uiCulture="auto" culture="auto" />

There is also an equivalent configuration setting per page.

Then, as per the Resource Fallback process, if your app includes a satellite assembly for the matching culture (or, failing that, its parent neutral culture), it will be used by the Resource Manager. If not, then your default resources will be used (English if that's your base language).

like image 68
Clafou Avatar answered Oct 26 '22 09:10

Clafou