You can use RequestCycleListener to change locale setting in case . In
Application init():

getRequestCycleListeners().add(new RequestLocaleListener());

    public class RequestLocaleListener extends AbstractRequestCycleListener
{

        public static final String LOCALE_COOKIE = "LC";

        public void onBeginRequest(RequestCycle cycle) {
            if
(WebRequest.class.isAssignableFrom(cycle.getRequest().getClass())) {
                WebRequest webrequest = (WebRequest) cycle.getRequest();
                Cookie localeCookie = webrequest.getCookie(LOCALE_COOKIE);
                if (localeCookie != null) {
                    for (Locale l : Locale.getAvailableLocales()) {
                        // expect language as cookie value
                        if
(localeCookie.getValue().equalsIgnoreCase(l.getLanguage())) {
                            Session.get().setLocale(l);
                            break;
                        }
                    }
                }
            }
        }
    }

2012/6/10 oliver.stef <ova...@gmail.com>

> Thanks again!
> It's now much clearer!!
>
> for some reason i have error on CookieUtils... so i implement it like this:
> (on my HomePage.java)
>
> form.add(new SubmitLink("English"){
>        @Override
>        public void onSubmit() {
>                getSession().setLocale(new Locale("en_US"));
>
>            Cookie languageCookie = new
> Cookie(WicketApplication.LANGUAGE_COOKIE_NAME, "en_US");
>
>  languageCookie.setMaxAge(WicketApplication.LANGUAGE_COOKIE_AGE);
>                    ((WebResponse)getResponse()).addCookie(languageCookie);
>                }
>        });
>
> *one last thing:*
> so the "Session.get().setLocale(locale)" i should implement in
> WicketApplication?
> like this:
> @Override
>    public Session newSession(Request request, Response response) {
>
>        Session session = super.newSession(request, response);
>        session = trySetLanguageFromCookie(session, request, response);
>
>        return session;
>    }
>
>        private Session trySetLanguageFromCookie(Session session, Request
> request,
> Response response) {
>
>                Cookie[] cookies = ((WebRequest) request).getCookies();
>
>        if (cookies == null || cookies.length == 0) {
>            return session;
>        }
>
>        for (Cookie cookie : cookies) {
>            if (LANGUAGE_COOKIE_NAME.equals(cookie.getName())) {
>                session.setLocale(new Locale(cookie.getValue()));
>
>                cookie.setMaxAge(LANGUAGE_COOKIE_AGE);
>
>                ((WebResponse)response).addCookie(cookie);
>                break;
>            }
>        }
>
>        return session;
>    }
>
> 10x!
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/set-locale-in-cookie-tp4649827p4649831.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>


-- 
Daniel Suckow

Reply via email to