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?
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.
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
Bye
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.
Profile properties were created specifically to allow you to store user-specific preferences. This would be good place to store this kind of data.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With