You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Gemes Tibor <ge...@regens.hu> on 2002/10/26 10:56:54 UTC

change locale on form-login-page

Is this possible?

I added a few links to the login page, which changes the Locale. The user sets 
its locale in the session at the org.apache.struts.action.LOCALE_KEY, but 
this takes effect after the successful login. How could I use the selected 
Locale for the login-page itself? 

The problem is that the Locale change was successful, thou it is not 
recognisable to the user untill the login. I want to display the form-login 
page i18n'ed as well as all other pages are.

Tia,

Tib

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE : change locale on form-login-page

Posted by micael <ca...@harbornet.com>.
Hmm, I'll try again, that did not work:


     String language = null;
     String localeValue = request.getParameter("locale");

     if(localeValue != null) {
         language = localeValue.substring(0, 2);
         response.setContentType("text/html;charset=utf-8");
         Locale newLocale = new Locale(language);
         session.setAttribute(Action.LOCALE_KEY, newLocale);
     }



At 09:26 PM 10/26/2002 -0700, you wrote:
>In my earlie answer, I assumed you would do this.  But, if not, just do 
>what I said and use code that does the equivalent of the following:
>
><%@ page language="java" import="java.util.*,org.apache.struts.action.*" 
>contentType="text/html;charset=utf-8" %>
><%@ taglib uri="template" prefix="template" %>
><%@ taglib uri="html" prefix="html" %>
><% String language = null; String localeValue = 
>request.getParameter("locale"); if(localeValue != null) { language = 
>localeValue.substring(0, 2); 
>response.setContentType("text/html;charset=utf-8"); Locale newLocale = new 
>Locale(language); session.setAttribute(Action.LOCALE_KEY, newLocale); } %> 
>In the content, return to the same page, and this will, as I said before, 
>work. At 01:56 PM 10/26/2002 +0200, you wrote: >Hi, > >First, Struts 
>doesn't automatically store a LOCALE.KEY unless a session >is required by 
>a page. > >This can easily be solved by extending the 
>requestProcessor. >In the above example, this is done. But additionally, 
>it checks a cookie >to see if the user has requested a Locale change. In 
>this case, a simple >session cookie can change immediately the session 
>Locale. >This javascript script will reload the current page with the new 
>Locale >which will continue to be used in the session. Because the Locale 
>is >changed in the requestProcessor before treating the action 
>itself. > > > >The requestProcessor extending code : > >public class 
>RequestProc extends RequestProcessor { >... > protected void processLocale 
>( > HttpServletRequest request, > HttpServletResponse response) { > > 
>HttpSession session = request.getSession(); > Locale locale = 
>request.getLocale(); > Locale sessionLocale 
>= >(Locale)session.getAttribute(Action.LOCALE_KEY); > >// Cookie part > // 
>Cookie search > String lang = null; > Cookie cookies[] = 
>request.getCookies(); > if (cookies == null) return; > for (int i = 0; i< 
>cookies.length ; i++) { > if ("lang".equals( cookies[i].getName()) ) { > 
>lang = cookies[i].getValue(); > break; > } > } > > // Search the complete 
>Locale > if (lang !=null) { > if (lang.equals("fr")) locale = 
>Locale.FRANCE; > else if (lang.equals("en")) locale = Locale.UK; > else if 
>(lang.equals("de")) locale = >Locale.GERMANY; > } >// End cookie part > // 
>Update the session Locale > if (sessionLocale == null 
>|| >(sessionLocale.getLanguage() != locale.getLanguage() ) ) { > 
>log.info("Setting user locale to " + locale); > session.setAttribute( 
>Action.LOCALE_KEY, >locale); > } > } >... >} > >If you don't will use the 
>cookie add-on, simply skip the "cookie part". >But this will ensure you 
>have a session Locale stored in the session. >And remember that this is 
>done BEFORE the the struts-action is called. > >Hopping this will help 
>you, >Jean-Pierre PAWLAK > >-----Message d'origine----- >De : Gemes Tibor 
>[mailto:gemes@regens.hu] >Envoyé : samedi 26 octobre 2002 10:57 >À : 
>struts-user@jakarta.apache.org >Objet : change locale on 
>form-login-page > >Is this possible? > >I added a few links to the login 
>page, which changes the Locale. The >user sets >its locale in the session 
>at the org.apache.struts.action.LOCALE_KEY, >but >this takes effect after 
>the successful login. How could I use the >selected >Locale for the 
>login-page itself? > >The problem is that the Locale change was 
>successful, thou it is not >recognisable to the user untill the login. I 
>want to display the >form-login >page i18n'ed as well as all other pages 
>are. > >Tia, > >Tib > >-- >To unsubscribe, e-mail: > >For additional 
>commands, e-mail: > > > > > >-- >To unsubscribe, e-mail: >For additional 
>commands, e-mail: Micael 
>------------------------------------------------------- This electronic 
>mail transmission and any accompanying documents contain information 
>belonging to the sender which may be confidential and legally privileged. 
>This information is intended only for the use of the individual or entity 
>to whom this electronic mail transmission was sent as indicated above. If 
>you are not the intended recipient, any disclosure, copying, distribution, 
>or action taken in reliance on the contents of the information contained 
>in this transmission is strictly prohibited. If you have received this 
>transmission in error, please delete the message. Thank you -- To 
>unsubscribe, e-mail: For additional commands, e-mail:

Micael

-------------------------------------------------------

This electronic mail  transmission and any accompanying documents contain 
information belonging to the sender which may be confidential and legally 
privileged.  This information is intended only for the use of the 
individual or entity to whom this electronic mail transmission was sent as 
indicated above. If you are not the intended recipient, any disclosure, 
copying, distribution, or action taken in reliance on the contents of the 
information contained in this transmission is strictly prohibited.  If you 
have received this transmission in error, please delete the message.  Thank you 



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE : change locale on form-login-page

Posted by micael <ca...@harbornet.com>.
In my earlie answer, I assumed you would do this.  But, if not, just do 
what I said and use code that does the equivalent of the following:

<%@ page language="java" import="java.util.*,org.apache.struts.action.*" 
contentType="text/html;charset=utf-8" %>
<%@ taglib uri="template" prefix="template" %>
<%@ taglib uri="html" prefix="html" %>
<html:html locale="true">

<%
     String language = null;
     String localeValue = request.getParameter("locale");

     if(localeValue != null) {
         language = localeValue.substring(0, 2);
         response.setContentType("text/html;charset=utf-8");
         Locale newLocale = new Locale(language);
         session.setAttribute(Action.LOCALE_KEY, newLocale);
     }

%>
<template:insert template="../template/template_work.jsp">
   <template:put name="title" content="../title/language_title.jsp"/>
   <template:put name="navigation" 
content="../navigation/standard_navigation.jsp"/>
   <template:put name="content" content="../content/language_content.jsp"/>
</template:insert>

</html:html>


In the content, return to the same page, and this will, as I said before, work.

At 01:56 PM 10/26/2002 +0200, you wrote:
>Hi,
>
>First, Struts doesn't automatically store a LOCALE.KEY unless a session
>is required by a page.
>
>This can easily be solved by extending the requestProcessor.
>In the above example, this is done. But additionally, it checks a cookie
>to see if the user has requested a Locale change. In this case, a simple
>session cookie can change immediately the session Locale.
>This javascript script will reload the current page with the new Locale
>which will continue to be used in the session. Because the Locale is
>changed in the requestProcessor before treating the action itself.
>
><script language="javascript">
>function setLang(lg) {
>   document.cookie = "lang="+lg;
>   location.reload();
>}
>function setLangFr() { setLang("fr"); }
>function setLangDe() { setLang("de"); }
>function setLangEn() { setLang("en"); }
></script>
>
>The requestProcessor extending code :
>
>public class RequestProc extends RequestProcessor {
>...
>         protected void processLocale (
>                 HttpServletRequest request,
>                 HttpServletResponse response) {
>
>                 HttpSession session = request.getSession();
>                 Locale locale = request.getLocale();
>                 Locale sessionLocale =
>(Locale)session.getAttribute(Action.LOCALE_KEY);
>
>// Cookie part
>                 // Cookie search
>                 String lang = null;
>                 Cookie cookies[] = request.getCookies();
>                 if (cookies == null) return;
>                 for (int i = 0; i< cookies.length ; i++) {
>                         if ("lang".equals( cookies[i].getName()) ) {
>                                 lang = cookies[i].getValue();
>                                 break;
>                         }
>                 }
>
>                 // Search the complete Locale
>                 if (lang !=null) {
>                         if (lang.equals("fr")) locale = Locale.FRANCE;
>                         else if (lang.equals("en")) locale = Locale.UK;
>                         else if (lang.equals("de")) locale =
>Locale.GERMANY;
>                 }
>// End cookie part
>                 // Update the session Locale
>                 if (sessionLocale == null ||
>(sessionLocale.getLanguage() != locale.getLanguage() ) ) {
>                         log.info("Setting user locale to " + locale);
>                         session.setAttribute( Action.LOCALE_KEY,
>locale);
>                 }
>         }
>...
>}
>
>If you don't will use the cookie add-on, simply skip the "cookie part".
>But this will ensure you have a session Locale stored in the session.
>And remember that this is done BEFORE the the struts-action is called.
>
>Hopping this will help you,
>Jean-Pierre PAWLAK
>
>-----Message d'origine-----
>De : Gemes Tibor [mailto:gemes@regens.hu]
>Envoyé : samedi 26 octobre 2002 10:57
>À : struts-user@jakarta.apache.org
>Objet : change locale on form-login-page
>
>Is this possible?
>
>I added a few links to the login page, which changes the Locale. The
>user sets
>its locale in the session at the org.apache.struts.action.LOCALE_KEY,
>but
>this takes effect after the successful login. How could I use the
>selected
>Locale for the login-page itself?
>
>The problem is that the Locale change was successful, thou it is not
>recognisable to the user untill the login. I want to display the
>form-login
>page i18n'ed as well as all other pages are.
>
>Tia,
>
>Tib
>
>--
>To unsubscribe, e-mail:
><ma...@jakarta.apache.org>
>For additional commands, e-mail:
><ma...@jakarta.apache.org>
>
>
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>

Micael

-------------------------------------------------------

This electronic mail  transmission and any accompanying documents contain 
information belonging to the sender which may be confidential and legally 
privileged.  This information is intended only for the use of the 
individual or entity to whom this electronic mail transmission was sent as 
indicated above. If you are not the intended recipient, any disclosure, 
copying, distribution, or action taken in reliance on the contents of the 
information contained in this transmission is strictly prohibited.  If you 
have received this transmission in error, please delete the message.  Thank you 



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: change locale on form-login-page

Posted by micael <ca...@harbornet.com>.
You just return the login page to the login page in the form that changes 
the locale.

At 10:05 AM 10/26/2002 -0700, you wrote:


>On Sat, 26 Oct 2002, Gemes Tibor wrote:
>
> > Date: Sat, 26 Oct 2002 10:56:54 +0200
> > From: Gemes Tibor <ge...@regens.hu>
> > Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> > To: struts-user@jakarta.apache.org
> > Subject: change locale on form-login-page
> >
> > Is this possible?
> >
> > I added a few links to the login page, which changes the Locale. The 
> user sets
> > its locale in the session at the org.apache.struts.action.LOCALE_KEY, but
> > this takes effect after the successful login. How could I use the selected
> > Locale for the login-page itself?
> >
>
>How are you planning on letting the user choose the locale before they've
>logged in?  That sounds like a catch-22.
>
>When Struts sees a request that does not have a Locale in the session
>already, it looks at the Accept-Language header included in the request.
>In turn, this is set by the language preferences you've selected in your
>browser -- the first one you list will be the one that Struts picks, and
>will stay there until you change it (by replacing the seesion attribute).
>
>Therefore, if the login page is the very first page that the user sees,
>tell them to change their browser settings.  And don't forget to include:
>
>   <html:html locale="true">
>     ... contents of the login page ...
>   </html:html>
>
>if the user is linking directly to the login page rather than going
>through the controller servlet.
>
> > The problem is that the Locale change was successful, thou it is not
> > recognisable to the user untill the login. I want to display the form-login
> > page i18n'ed as well as all other pages are.
> >
> > Tia,
> >
> > Tib
>
>Craig
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>

Micael

-------------------------------------------------------

This electronic mail  transmission and any accompanying documents contain 
information belonging to the sender which may be confidential and legally 
privileged.  This information is intended only for the use of the 
individual or entity to whom this electronic mail transmission was sent as 
indicated above. If you are not the intended recipient, any disclosure, 
copying, distribution, or action taken in reliance on the contents of the 
information contained in this transmission is strictly prohibited.  If you 
have received this transmission in error, please delete the message.  Thank you 



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE : change locale on form-login-page

Posted by "JP PAWLAK(Tiscali)" <jp...@tiscali.fr>.
Hi,

First, Struts doesn't automatically store a LOCALE.KEY unless a session
is required by a page.

This can easily be solved by extending the requestProcessor.
In the above example, this is done. But additionally, it checks a cookie
to see if the user has requested a Locale change. In this case, a simple
session cookie can change immediately the session Locale. 
This javascript script will reload the current page with the new Locale
which will continue to be used in the session. Because the Locale is
changed in the requestProcessor before treating the action itself.

<script language="javascript">
function setLang(lg) {
  document.cookie = "lang="+lg;
  location.reload();
}
function setLangFr() { setLang("fr"); }
function setLangDe() { setLang("de"); }
function setLangEn() { setLang("en"); }
</script>

The requestProcessor extending code :

public class RequestProc extends RequestProcessor {
...
	protected void processLocale (
		HttpServletRequest request,
		HttpServletResponse response) {

		HttpSession session = request.getSession();
		Locale locale = request.getLocale();
		Locale sessionLocale =
(Locale)session.getAttribute(Action.LOCALE_KEY);

// Cookie part
		// Cookie search
		String lang = null;
		Cookie cookies[] = request.getCookies();
		if (cookies == null) return;
		for (int i = 0; i< cookies.length ; i++) {
			if ("lang".equals( cookies[i].getName()) ) {
				lang = cookies[i].getValue();
				break;	
			}			
		}
		
		// Search the complete Locale
		if (lang !=null) {
			if (lang.equals("fr")) locale = Locale.FRANCE; 
			else if (lang.equals("en")) locale = Locale.UK; 
			else if (lang.equals("de")) locale =
Locale.GERMANY; 
		}
// End cookie part		
		// Update the session Locale
		if (sessionLocale == null ||
(sessionLocale.getLanguage() != locale.getLanguage() ) ) {
			log.info("Setting user locale to " + locale);
			session.setAttribute( Action.LOCALE_KEY,
locale);
		}		
	}
...
}

If you don't will use the cookie add-on, simply skip the "cookie part".
But this will ensure you have a session Locale stored in the session.
And remember that this is done BEFORE the the struts-action is called.

Hopping this will help you,
Jean-Pierre PAWLAK

-----Message d'origine-----
De : Gemes Tibor [mailto:gemes@regens.hu] 
Envoyé : samedi 26 octobre 2002 10:57
À : struts-user@jakarta.apache.org
Objet : change locale on form-login-page

Is this possible?

I added a few links to the login page, which changes the Locale. The
user sets 
its locale in the session at the org.apache.struts.action.LOCALE_KEY,
but 
this takes effect after the successful login. How could I use the
selected 
Locale for the login-page itself? 

The problem is that the Locale change was successful, thou it is not 
recognisable to the user untill the login. I want to display the
form-login 
page i18n'ed as well as all other pages are.

Tia,

Tib

--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: change locale on form-login-page

Posted by Gemes Tibor <ge...@regens.hu>.
oups duplicated c&p 
oups duplicated c&p 

:) 

sorry sorry

Tib Tib

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: change locale on form-login-page

Posted by Gemes Tibor <ge...@regens.hu>.
2002. október 26. 19:05 dátummal Craig R. McClanahan ezt írtad:

> When Struts sees a request that does not have a Locale in the session
> already, it looks at the Accept-Language header included in the request.
> In turn, this is set by the language preferences you've selected in your
> browser -- the first one you list will be the one that Struts picks, and
> will stay there until you change it (by replacing the seesion attribute).
>
> Therefore, if the login page is the very first page that the user sees,
> tell them to change their browser settings.  And don't forget to include:

This made me think a bit:

 I created a page which is not protected by security-constraint. The only task 
it does is to set up the default locale, asks wheter the users needs other 
locale, and provide a link to a protected area (thus prompting for 
username/passwd). This way the container security is not involved in the 
process changing the locale, only after it. It works in a lot of browsers (eg 
mozilla), but fails to work with others (eg konqueror and lynx). 

If I use mozilla I see in the manager of tomcat that the user creates only one  
session (proper way of working). But for the browsers it fails new sessions 
are created for EVERY request submitted by them. This is not the supposed 
behaviour i think.

I never done setting up site w/o auth. Probably I misconfigured sg. I know 
that locale change works like a charm if everything is secured and the locale 
change takes place _after_ login.

This made me think a bit:

 I created a page which is not protected by security-constraint. The only task 
it does is to set up the default locale, asks wheter the users needs other 
locale, and provide a link to a protected area (thus prompting for 
username/passwd). This way the container security is not involved in the 
process changing the locale, only after it. It works in a lot of browsers (eg 
mozilla), but fails to work with others (eg konqueror and lynx). 

If I use mozilla I see in the manager of tomcat that the user creates only one  
session (proper way of working). But for the browsers it fails new sessions 
are created for EVERY request submitted by them. This is not the supposed 
behaviour i think.

I never done setting up site w/o auth before. Probably I misconfigured 
something. I know that locale change works like a charm if everything is 
secured and the locale change takes place _after_ login.

Tib


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: change locale on form-login-page

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Sat, 26 Oct 2002, Gemes Tibor wrote:

> Date: Sat, 26 Oct 2002 10:56:54 +0200
> From: Gemes Tibor <ge...@regens.hu>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: struts-user@jakarta.apache.org
> Subject: change locale on form-login-page
>
> Is this possible?
>
> I added a few links to the login page, which changes the Locale. The user sets
> its locale in the session at the org.apache.struts.action.LOCALE_KEY, but
> this takes effect after the successful login. How could I use the selected
> Locale for the login-page itself?
>

How are you planning on letting the user choose the locale before they've
logged in?  That sounds like a catch-22.

When Struts sees a request that does not have a Locale in the session
already, it looks at the Accept-Language header included in the request.
In turn, this is set by the language preferences you've selected in your
browser -- the first one you list will be the one that Struts picks, and
will stay there until you change it (by replacing the seesion attribute).

Therefore, if the login page is the very first page that the user sees,
tell them to change their browser settings.  And don't forget to include:

  <html:html locale="true">
    ... contents of the login page ...
  </html:html>

if the user is linking directly to the login page rather than going
through the controller servlet.

> The problem is that the Locale change was successful, thou it is not
> recognisable to the user untill the login. I want to display the form-login
> page i18n'ed as well as all other pages are.
>
> Tia,
>
> Tib

Craig


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>