You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by "oliver.stef" <ov...@gmail.com> on 2012/06/10 18:07:20 UTC
set locale in cookie
Hi,
I'm bulidng my application and i want the user to set is prefered language.
My problem is that i want to save the user selecteion in a cookie (i need to
set the cookie locale, no?) but i havn't found any working implementation
that can show me how it's done.
Is there any one that can help? please...
Thanks!
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/set-locale-in-cookie-tp4649827.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: set locale in cookie
Posted by Daniel Suckow <da...@googlemail.com>.
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 <ov...@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-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
--
Daniel Suckow
Re: set locale in cookie
Posted by "oliver.stef" <ov...@gmail.com>.
YAP!!! WORKING!!!!
MARTIN - Thanks again!
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/set-locale-in-cookie-tp4649827p4649833.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: set locale in cookie
Posted by "oliver.stef" <ov...@gmail.com>.
YAP!!! WORKING!!!!
MARTIN - Thanks again!
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/set-locale-in-cookie-tp4649827p4649834.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: set locale in cookie
Posted by Martin Grigorov <mg...@apache.org>.
On Sun, Jun 10, 2012 at 7:57 PM, oliver.stef <ov...@gmail.com> wrote:
> 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?
You can also provide your own MySession that does this in its constructor.
> 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-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
--
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: set locale in cookie
Posted by "oliver.stef" <ov...@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-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: set locale in cookie
Posted by Martin Grigorov <mg...@apache.org>.
On Sun, Jun 10, 2012 at 7:32 PM, oliver.stef <ov...@gmail.com> wrote:
> Hi Martin,
>
> Thank you for your fast replay!
>
> What do you mean by saying: " ...parse it and set it in the session. "
> How to do it?
The cookie value will be a String like "en" or "en_US" or "en_US_variation".
Create a Locale object with:
new Locale("en")
new Locale("en", "US")
new Locale("en", "US", "variation")
and then do: Session.get().setLocale(locale)
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/set-locale-in-cookie-tp4649827p4649829.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
--
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: set locale in cookie
Posted by "oliver.stef" <ov...@gmail.com>.
Hi Martin,
Thank you for your fast replay!
What do you mean by saying: " ...parse it and set it in the session. "
How to do it?
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/set-locale-in-cookie-tp4649827p4649829.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: set locale in cookie
Posted by Martin Grigorov <mg...@apache.org>.
Hi,
Use CookieUtils.save("myCookie", locale.toString()) to set the cookie.
Later use CookieUtils.load("myCookie") to load it and then you'll need
to parse it and set it in the session.
On Sun, Jun 10, 2012 at 7:07 PM, oliver.stef <ov...@gmail.com> wrote:
> Hi,
>
> I'm bulidng my application and i want the user to set is prefered language.
>
> My problem is that i want to save the user selecteion in a cookie (i need to
> set the cookie locale, no?) but i havn't found any working implementation
> that can show me how it's done.
>
> Is there any one that can help? please...
>
> Thanks!
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/set-locale-in-cookie-tp4649827.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
--
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org