You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "JP PAWLAK(Tiscali)" <jp...@tiscali.fr> on 2002/10/26 13:56:47 UTC

RE : change locale on form-login-page

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 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>