Additionally, if you want other key(other than org.apache.struts.action.LOCALE 
=Globals.LOCALE_KEY) to store your locale,

you can do this:

session.setAttribute("myNewMeaningfulKey",myLocaleObject), then in your JSP you would 
use this:
<bean:message key="myPropertyKey" locale="myNewMeaningfulKey" />. This is the way the 
locale attribute in message tag is used in struts. 

Thanks.

Larry Zhang

-----Original Message-----
From: Wang, Yuanbo [mailto:[EMAIL PROTECTED]
Sent: Friday, May 14, 2004 2:40 PM
To: Struts Users Mailing List
Subject: RE: Sharing what I've learned: locale switching


Thanks for sharing the information. Basically struts using the following
method to decide which Locale is in the session, then load the
corresponding resource bundle:

protected Locale getLocale(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        Locale locale =
(Locale)session.getAttribute("org.apache.struts.action.LOCALE");
        if(locale == null)
            locale = defaultLocale;
        return locale;
    }

So to switch the Locale dynamically, update the Locale object saved in
the session. 

Thanks,
Yuanbo


-----Original Message-----
From: None None [mailto:[EMAIL PROTECTED] 
Sent: Friday, May 14, 2004 2:16 PM
To: [EMAIL PROTECTED]
Subject: Sharing what I've learned: locale switching


Because this might be helpful to others, and because I probably would
have 
spent another couple of hours figuring it out on my own without the help
of 
some people on this lsit, I wanted to give back as much as I could.  So,

here's a consolidated bit of info I've learned about switching language
in 
your Struts apps...

What I have is a file manager application, more or less just for me to
learn 
Struts.  I wanted to have the ability to switch languages on-the-fly.
To do 
this, I've done the following:

(1) I created two files and placed them in WEB-INF/classes.  They are 
ofmResources_en.properties and ofmResources_de.properties (en for
English, 
de for German).  These files contain various text strings in both
language.  
For instance, there is a lable on the screen for file uploads which is 
defined as follows:

labels.uploadFile=Upload a file:

and for the German version:

labels.uploadFile=hochladen Sie eine Datei:

(2) I added the following entry to web.xml, as an init parameter of the 
ActionServlet:

    <init-param>
        <param-name>application</param-name>
        <param-value>ofmResources</param-value>
    </init-param>

As near as I can tell, NO entries are required in struts-config.xml.
You 
also do NOT need to do anything for each version of the resource file.
As 
long as they are named xxxxx_ll.properties, where xxxxx is the value of
the 
application parameter above, and ll is a valid country code, that's all 
there is to it.

(3) Next, I added some flag graphics to my web pages, one an American
flag, 
one a German flag.  Here is the HTML for them:

    <form name="changeLocaleForm" method="post"
action="changeLocale.ofm" 
style="display:inline;">
        <input type="hidden" name="languageCode">
        <table width="100%" border="0" cellpadding="0" 
cellspacing="0"><tr><td align="right">
            <input type="image" src="img/flag_usa.gif" hspace="6"
border="0" 
onClick="changeLocaleForm.languageCode.value='en';">
            <input type="image" src="img/flag_germany.gif" hspace="6" 
border="0" onClick="changeLocaleForm.languageCode.value='de';">
        </td></tr><?table>
    </form>

Pretty trivial stuff there.

(4) Next, I created an ActionForm called ChangeLocaleActionForm as
follows:

    package com.mycompany.ofm.actionforms;
    import org.apache.struts.action.*;
    public class ChangeLocaleActionForm extends ActionForm {
        private String languageCode = null;
        public ChangeLocaleActionForm() {
            languageCode = null;
        }
        public void setLanguageCode(String inLanguageCode) {
            languageCode = inLanguageCode;
        }
        public String getLsnguageCode() {
            return languageCode;
        }
    }

(5) Next, I created an accompanying Action:

    package com.mycompany.ofm.actions;
    import java.util.*;
    import javax.servlet.http.*;
    import com.omnytex.ofm.actionforms.*;
    import org.apache.struts.*;
    import org.apache.struts.action.*;
    public class ChangeLocaleAction extends Action {
        public ActionForward execute(ActionMapping mapping, ActionForm
form, 
HttpServletRequest request, HttpServletResponse response) throws
Exception {
            ChangeLocaleActionForm claf = (ChangeLocaleActionForm)form;
                String languageCode = claf.getLsnguageCode();
                request.getSession().setAttribute(Globals.LOCALE_KEY,
new 
Locale(languageCode));
                return mapping.findForward("showPathContents");
        }
    }

As it turns out as someone here informed me, there is "automagically" a 
Locale in session, created based on what is sent by the browser.  So, by

default on my system the value en_US is stored in session under the name

Globals.LOCALE_KEY.  By the way, as near as I can tell, the _US portion
of 
the language code doesn't matter (I'm sure it MATTERS, but for what I'm 
describing it doesn't).  So, this allows one to switch the locale (read:

language) of the app by clicking a flag.  No big deal.

(6) To make use of this all, there are two concerns... One is messages
in a 
JSP rendered with the <bean:message> tag, the other is messages returned

from an Action that you want to display to the user.

For the JSP side of things, it's simple... you just do this...

    <bean:message key="labels.uploadFile"/>

Struts uses the Locale stored in session to pull the key from the
correct 
resource file.  Yeah, it's that easy!  As I said previously, the fact
that 
to start my Locale contains en_US doesn't seem to matter... Struts looks
to 
be smart enough to look for a properties file with just _en in the
name... I 
presume that if I named the file ofmResources_en_US.properties it would
work 
as well, but I haven't verified that.

For messages returned from an Action, I have found that this code does
what 
I want:

    lpcaf.setMessage(getResources(request).getMessage(
                     
(Locale)request.getSession().getAttribute(Globals.LOCALE_KEY),
                     "messages.deleteFailed"));

This is just setting a message in an ActionForm that is returned to the 
view.  In the view I do:

    <body <logic:notEmpty name="listPathContentsActionForm" 
property="message">onLoad="alert('<bean:write 
name="listPathContentsActionForm" property="message" 
/>');"</logic:notEmpty>>

This just pops up a JavaScript alert with the message I returned from
the 
Action, in the correct language associated with the current Locale.
Cool!

So, hopefully that helps someone somewhere along the way.  It really is 
pretty cool to be able to switch the language of this app just by
clicking a 
flag!

_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar - get it now! 
http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to