struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "JP PAWLAK\(Tiscali\)" <>
Subject RE : change locale on form-login-page
Date Sat, 26 Oct 2002 11:56:47 GMT

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;
function setLangFr() { setLang("fr"); }
function setLangDe() { setLang("de"); }
function setLangEn() { setLang("en"); }

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 =

// 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();
		// 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 =
// End cookie part		
		// Update the session Locale
		if (sessionLocale == null ||
(sessionLocale.getLanguage() != locale.getLanguage() ) ) {"Setting user locale to " + locale);
			session.setAttribute( Action.LOCALE_KEY,

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 [] 
Envoyé : samedi 26 octobre 2002 10:57
À :
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,
this takes effect after the successful login. How could I use the
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
page i18n'ed as well as all other pages are.



To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message