views:

272

answers:

6

I am developing a multilingual site and was wondering what is the best way to store the language chosen by the user?

Either via QueryString or should it be in Session..or any other options?

+1  A: 

Profile properties were created specifically to allow you to store user-specific preferences. This would be good place to store this kind of data.

Kevin Babcock
A: 

There is a property that is passed when a browser makes a request that you could use. This property is referenced in your code behind by referencing:

Request.UserLanguages // returns an array

Alternatively, you could prompt the user to specify a prefered language, and save it in a cookie.

In your Page.Load handler put something like the following:

string prefLan;    
if(Request.Cookies["preferedLanguage"] != null)
        prefLan = Server.HtmlEncode(Request.Cookies["preferedLanguage"].Value);
regex
A: 

Querystring is not the right place for this, unless it is read initially from the querystring and stored somewhere else.

Store it in a cookie, or if cookies are turned off, store it in session or viewstate.

Kevin's answer "profile properties" is an easy way to let an established library do the heavy lifting for you with minimal effort on your part.

Chris Ballance
+1  A: 

Hi, I think it very much depends on how your application handles (desires to handle) the user languages. If your users have to log on to your site, then you probably have an account settings page somewhere with an appropriate user profile object or something similar behind. In such a case I guess you would save these settings on the DB, and when the user comes back to your site you get the user-information somehow (i.e. from a cookie), you load your user-profile object from the DB. The information would be kept in a session since I think that would be most suited in this case. If your users don't have to login, so you basically cannot directly identify them, I would store the language settings in a cookie. This gives your user the value that he will always find the site in his preferred language when he comes back later (given that he doesn't delete the cookies and the cookie's lifetime is long enough). As a third possibility you could just identify the users language according to his default browser settings (just as "regex" mentioned above).

As I said, it really depends on your application's needs. What you have to keep in mind though is that

  • objects stored in the session are kept on the server-side, using server memory. So don't keep useless things there in memory if you don't need them. However the session object is suited for storing data related to a user's visit and configurations.
  • Data stored in cookies, or the Viewstate object are send back and forth between the client browser and web server. So this causes additional traffic and may decrease the performance.

Bye

Juri
+1  A: 

if you consider this scenario where a user has browsed a series of pages in "en" and the language info is stored in cookie session and pages have been cached (http, browser, app)..

when the language is switched by the user to "cy", a change would happen for the current page, but when the user jumps back to a page they have previously visited (where the header caches expired have not expired) it would load up the page in "en" as the querystring does not state the language - for it to serve content in that language.

unlikely that a user would want to change languages so frequently - but as a developer, its a scenraio that should be handled.

any ideas please feel free to shout.

raq
+2  A: 

Another consideration not mentioned in any of the other answers, is Search Engine Friendliness. If you use a different url for each language like

http://{en|cy}.example.com/subdir/ or http://example.com/{en|cy}/subdir) 
then search engines can index your site in multiple languages.

Ofri Raviv