Excellent, even better!  Thank you!

It's becoming obvious to me that I need to spend a few hours just looking at what's available in the Action (and other) base classes. I've been just learning what I need to each step of the way, but maybe a little time reading through the javadocs would be useful.

Thanks again!

From: "Joe Hertz" <[EMAIL PROTECTED]>
Reply-To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
To: "'Struts Users Mailing List'" <[EMAIL PROTECTED]>
Subject: RE: Sharing what I've learned: locale switching
Date: Fri, 14 May 2004 18:09:01 -0400

Suggestion:

Rather than using the GLOBALS.Locale_Key in the session, use the
action.setLocale() method. This way you won't have to be changing your
code if the internals change.

For example, to decide between, say English and Russian, you can do this
and never mess with the httpSession directly.

    Locale russian = new Locale("ru");
    Locale english = Locale.ENGLISH;

    if (condition == true)
    {
      this.setLocale(request, english);
    }
    else
    {
      this.setLocale(request, russian);
    }

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


_________________________________________________________________
Express yourself with the new version of MSN Messenger! Download today - it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/



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



Reply via email to