In the previous post we learnt a few tricks regarding internationalization features in Wicket. Today we will go further and will let application to remember some user preferences using HTTP Cookies. Storing some basic data in cookies is an easy and popular way of letting user customize look and feel of web application he uses very often. Some portals use cookies to render modules in order chosen by user so that most interesting information are in the top.

In our ItemDirectory web application we don’t have (yet!) any sophisticated mechanism which could rely on cookies so we will show how it can be used to store and load language selected by the user.

Storing information in cookies

So first thing we need to do is store which language user wants to use. So we must modify changeUserLocaleTo() method in LanguagePanel class:

This mechanism is rather easy to understand. Besides setting session locale we also create and put language cookie with name and age defined as static fields in our Application class. We just have to remember to add our new or modified cookie to response so it will be send back to the browser.

After this we can run jetty with deployed application and check that after clicking there is a cookie with information about selected language.

Loading information from cookies

Now we have some information in cookie waiting for our application to use it. The best moment to utilize such information is while creating new session so application could load preferences from cookie and store it in user session object. So lets play with method in WebApplication class which creates new user session:

So far so good, but the most important things happen in trySetLanguageFromCookie() method:

In this method we first try to find (1) cookie named LANGUAGE_COOKIE_NAME and if we find it, we modify (2) session locale according to data loaded from the cookie. And another thing we could do is refresh (4) just loaded cookie so if user won’t click flag for a long time (as he doesn’t have to because we automatically set proper locale basing on cookie), he won’t lose his preference.

Now, everything should work fine. User can choose language when he enters our application for a first time and later we load this information from cookie, set proper locale and refresh cookie so it won’t expire.