0
votes

I've read dozens of articles and solutions on persisting a selected value of a DropDownList. None of the articles seem to address my need exactly. Here is my requirement:

I have a DropDownList in _Layout.cshtml (sitewide page header) that allows a user to select their desired language.

<form>
    <select id="lang">
        <option value="en-US" selected="selected">English</option>
        <option value="fr-CA">Fran&#231;ais</option>
        <option value="pt-BR">Portugu&#234;s</option>
        <option value="es-MX">Espa&#241;ol</option>
    </select>
</form>

My intention is to use the Westwind.Globalization package (NuGet) to perform localization.

By default the setting should be English, but when a user makes a selection from the language dropdown I want to persist their selection to a cookie which will be used for not only for the current session but also on return visits.

If the user is authenticated I will also want to save their selection in their user profile which I have already added a column for.

What is the best practice for persisting a value for the current session, return visits, and to save to the user's profile if they are authenticated when the desired language is changed?

1
To be honest it sounds like you have it all figured out. Persist it to the database when possible, but if the user is anonymous just throw it in a cookie.Justin Helgerson
I think I know what needs to be done, just not sure how to accomplish it in code - like, in the controller? Does jQuery come into the picture? Or even where/how to code writing/reading the cookie.rwkiii

1 Answers

0
votes

If users of your website are anonymous, you can use a cookie to store the language preference.

If users of your website are not anonymous, in addition to the cookie you can store the language preferences of a user along with that user's database record.

The reason I say "in addition to the cookie" is the following. Suppose a user already has an account on your website and uses it regularly. They visit your website and have not logged in yet. The language preference should be drawn from the cookie until the user is logged in so that the landing page, the log in page, etc. are in the user's language. Once a user has logged in, update the cookie with the user's language preferences retrieved from your database.