struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "JP PAWLAK\(Tiscali\)" <jp.paw...@tiscali.fr>
Subject RE : change locale on form-login-page
Date Sat, 26 Oct 2002 11:56:47 GMT
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:
<mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail:
<mailto:struts-user-help@jakarta.apache.org>




--
To unsubscribe, e-mail:   <mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-user-help@jakarta.apache.org>


Mime
View raw message