views:

133

answers:

1

I'm about to take a look at how to implement internationalisation for an ASP.NET MVC project. I'm looking at how to allow the user to change languages. My initial though is a dropdownlist containing each of the supported langauages. Whoever a few questions have come to mind:

  1. How to store the list of supported languages? (e.g. just "en", "English"; "fr", "French" etc.) An xml file? .config files?
  2. If I store this in a file I'll have to cache this (at startup I guess). So, what would be best, load the xml data into a list (somehow) and store this list in the System.Web.Cache? Application State?
  3. How then to load this data into the view (for display in a dropdown)? Give the view direct access to the cache?

Just want to make sure I'm going in the right direction here...

Thank you.

A: 

First tell us please what you understand under "internationalization"?

a) You want to only have UI in several languages

b) You want to have the content in several languages

Have a look at this other question where I did some explanation on the topic: Advice on ASP.NET Multi-Lingual Strategy

To your questions:

  1. I would advice storing a list of supported languages in DB where you can identify them by 2-character code. See ISO 3166-1 alpha-2 country codes.

  2. If you're coming from the ASP.NET WebForms world, then the first thing to unlearn would be the statefulness and cache when you're dealing with ASP.NET MVC. You can store a simple single value in session or cookies that will indicate the currently active languages. The server side will generate a view in the required language based on this submitted values.

  3. You instantiate a model in your controller to be passed to the view. In the model you can have a property - a list with available languages. The view can (and supposed to) consume the data to display from its model.

Regarding where you could store a cached version of languages. You can consider having somewhere a static list that will be initialized at the first request (like weak references). It will be shared then across all requests for a given application domain, meaning all the users of your web app will see the same values (until the domain gets recycled or the server restarted). If you were to use the server session, then the list would be created again for each new user session.

Developer Art
I just mentioned the internationalisation part only as background, the problem here is more general than that - and internationalisation doesn't really come into it, rather just how best populate the dropdown.
UpTheCreek
Thanks, just to clarify a couple of the points...1) I'd rather not go to the db for this. It will be needed on every page, and not change much. 2) Regarding caching, I mean caching the list of available languages, not the users selection (which will be stored in cookie)
UpTheCreek
See updates in the end.
Developer Art