You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Thomas Hamacher <th...@qualiads.de> on 2005/11/07 11:57:49 UTC
Changing language (locale) with a button
Hi everyone,
I have the following problem and cannot find a real solution to it. Usually
this should be a common and known problem, so I'm wondering if I anyone has
found a solution to this.
I have internationalized my struts 1.2.7 application through tiles and the use
of ResourceBundle an java.util.Local.
Now I want to add a few buttons (one for every language) with which I can
change the language on every page (as seen in many international webseites).
My first idea was to create an action that changes the locale and redirects
back to page. My action-class looks like this:
public class SwitchLanguageAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
ActionForward forward = null;
String lang =
request.getParameter("lang"); //System.out.println("lang:" + lang);
if( lang == null )
return (mapping.findForward("failed"));
setLocale( request, new Locale(lang.toUpperCase()) );
String referer =
request.getHeader("referer"); //System.out.println("referer:" + referer);
forward = new RedirectingActionForward(referer);
return forward;
}
}
In my JSP-Page I call this action-class like this:
<html:link href="/switchLanguage.do?lang=de"><html:img
srcKey="icons.flags.german" bundle="mediaResources" border="0" /></html:link>
<html:link href="/switchLanguage.do?lang=en"><html:img
srcKey="icons.flags.english" bundle="mediaResources"
border="0" /></html:link>
<html:link href="/switchLanguage.do?lang=nl"><html:img
srcKey="icons.flags.netherland" bundle="mediaResources"
border="0" /></html:link>
The change of the locale works perfectly and the new values are displayed on
the page. The only problem I have right know is, that if my "referer" was a
".do"-page, the execute-method of my action-class is called and executed
again (see my other posting). So this cannot be the solution. If I manually
remove the .do in my referer-string, the action-class isn't called anymore
but the refering page is wrong.
Does anyone has a solution on how to change the language dynamically and can
send me his solution or can help me with my current solution? At least this
problem probably occurs on many, many pages that have mulit-language support.
So I guess there should be a good solution to it? But I couldn't find any on
the web.
Thank you very much in advance
Thomas
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Changing language (locale) with a button
Posted by Danny Lee <ha...@yahoo.de>.
Pretty easy,
I'm storing each actual action in "lastRedirect" - attribute
and each actual parameter in "lastParameter". Affter some critical
actions I do redirect to this saved stuff.
It works well, but there are some non critical problems (esp. with user
doing these actions after clicking on "go back"-browser buttom). So I gonna
find a new solution, when I have time for it.
Cheers
Danny
Thomas Hamacher schrieb:
> Danny,
>
> thank you very much for this ideas. Unfortunately the "display"-stuff together
> with the request.getRequestURL() doesn't work for me either, because I am
> working with tiles and therefor the requestURL is not correct. So I am pretty
> interested in what you do with the "lastAction"-Attribute in your session.
> Maybe this might work for me as well. Do you have any examples how you used
> this?
>
> Thank you very much
>
> Thomas
>
> On Monday 07 November 2005 14:12, Danny Lee wrote:
>
>>Here my solution (some stuff I found on the net and adapted for my
>>needs). I don't actually use the "display" stuff, couse I do it
>>otherway... so you can ignore it.
>>
>>I hope it'll help
>>
>>Cheers
>>
>>Danny
>>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Changing language (locale) with a button
Posted by Thomas Hamacher <th...@qualiads.de>.
Danny,
thank you very much for this ideas. Unfortunately the "display"-stuff together
with the request.getRequestURL() doesn't work for me either, because I am
working with tiles and therefor the requestURL is not correct. So I am pretty
interested in what you do with the "lastAction"-Attribute in your session.
Maybe this might work for me as well. Do you have any examples how you used
this?
Thank you very much
Thomas
On Monday 07 November 2005 14:12, Danny Lee wrote:
> Here my solution (some stuff I found on the net and adapted for my
> needs). I don't actually use the "display" stuff, couse I do it
> otherway... so you can ignore it.
>
> I hope it'll help
>
> Cheers
>
> Danny
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Changing language (locale) with a button
Posted by Danny Lee <ha...@yahoo.de>.
Here my solution (some stuff I found on the net and adapted for my
needs). I don't actually use the "display" stuff, couse I do it
otherway... so you can ignore it.
I hope it'll help
Cheers
Danny
*********************************
1) Action
public class ChangeLocaleAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws
Exception {
ChangeLocaleForm claf = (ChangeLocaleForm)form;
String languageCode = claf.getLanguageCode();
String forwardTo = claf.getDisplay();
System.out.println("forward to:"+forwardTo);
System.out.println("setting language to "+languageCode);
request.getSession().setAttribute(Globals.LOCALE_KEY, new
Locale(languageCode));
return
mapping.findForward((String)request.getSession().getAttribute("lastAction"));
}
2) ACTION FORM
public class ChangeLocaleForm extends ActionForm {
/**
*
*/
private static final long serialVersionUID = 1L;
private String languageCode = null;
private String display = null;
public ChangeLocaleForm() {
languageCode = null;
}
public void setLanguageCode(String inLanguageCode) {
languageCode = inLanguageCode;
}
public String getLanguageCode() {
return languageCode;
}
public String getDisplay() {
return display;
}
public void setDisplay(String display) {
this.display = display;
}
}
2) JSP
<form name="changeLocaleForm" method= "post" action= "../changeLocale.do"
style= "display:inline;">
<input type= "hidden" name= "languageCode">
<input type="hidden" name="display" value="<%=request.getRequestURL()%>" >
<table width= "100% " border= "0 " cellpadding= "0 "
cellspacing= "0 " > <tr><td align= "right">
<input type= "image" src= "images/EN.gif" hspace= "6" border= "0"
onClick= "changeLocaleForm.languageCode.value='en';">
<input type= "image" src= "images/DE.gif" hspace="6"
border= "0" onClick= "changeLocaleForm.languageCode.value='de';">
</td></tr> </table>
</form>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Changing language (locale) with a button
Posted by Thomas Hamacher <th...@qualiads.de>.
Michael,
thank you very much for your fast response. You're idea sounds really
interesting and might be a really good solution. May I ask you a few more
questions:
Concerning 1.: Where exactly should I check for the GET or the POST-Request.
Would you recommend to check for the GET/POST-Method in every execute-method
of an actionclass or is there a better (unique) place to check for that. I
thought about extending the RequestProcessor, but this doesn't seem to work,
becuase I need the mapping.findForward()-information of my execute-method to
know where to go (at least I guess so, if there´s no other way). Is it
necessary to create a FORM to have the POST-Request or is it also possible
the way I showed with the "switchLanguage.do?lang=en"-way? What do you mean
by redirect? Does this have anything to do with the redirect=true/false entry
in the RedirectActionForward or the struts-config.xml or is this a basic
HTTP-Concept?
Concernig 2: What other possibilities do I have to find my refering page if
not with the HTTP-Headers or the RequestURL()? I didn't find another place
for this information. Most of the given objects (e.g. mapping) only showed me
information about the current page, but not the refering-page.
I hope, this aren´t to many questions, but I really didn´t find any
information on this topic.
Thank you in advance
Thomas
On Monday 07 November 2005 18:05, Michael Jouravlev wrote:
> On 11/7/05, Thomas Hamacher <th...@qualiads.de> wrote:
> > Hi everyone,
> >
> > I have the following problem and cannot find a real solution to it.
> > Usually this should be a common and known problem, so I'm wondering if I
> > anyone has found a solution to this.
> >
> > I have internationalized my struts 1.2.7 application through tiles and
> > the use of ResourceBundle an java.util.Local.
> > Now I want to add a few buttons (one for every language) with which I can
> > change the language on every page (as seen in many international
> > webseites). My first idea was to create an action that changes the locale
> > and redirects back to page.
>
> ...
>
> > The change of the locale works perfectly and the new values are displayed
> > on the page. The only problem I have right know is, that if my "referer"
> > was a ".do"-page, the execute-method of my action-class is called and
> > executed again (see my other posting). So this cannot be the solution. If
> > I manually remove the .do in my referer-string, the action-class isn't
> > called anymore but the refering page is wrong.
>
> 1) Do not use referer - it is unreliable, especially with reloads and
> history navigation. Also, many firewalls strip out "refer" header.
>
> 2) To ensure that your action does not execute twice, check the
> request method. When you click "language" button, browser submits a
> POST request. You need to process that. When you redirect to the same
> action, browser sends a GET request, and you do not need to process it
> (unless you have "command links" on a page).
>
> Michael.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Changing language (locale) with a button
Posted by Michael Jouravlev <jm...@gmail.com>.
On 11/7/05, Thomas Hamacher <th...@qualiads.de> wrote:
> Hi everyone,
>
> I have the following problem and cannot find a real solution to it. Usually
> this should be a common and known problem, so I'm wondering if I anyone has
> found a solution to this.
>
> I have internationalized my struts 1.2.7 application through tiles and the use
> of ResourceBundle an java.util.Local.
> Now I want to add a few buttons (one for every language) with which I can
> change the language on every page (as seen in many international webseites).
> My first idea was to create an action that changes the locale and redirects
> back to page.
...
> The change of the locale works perfectly and the new values are displayed on
> the page. The only problem I have right know is, that if my "referer" was a
> ".do"-page, the execute-method of my action-class is called and executed
> again (see my other posting). So this cannot be the solution. If I manually
> remove the .do in my referer-string, the action-class isn't called anymore
> but the refering page is wrong.
1) Do not use referer - it is unreliable, especially with reloads and
history navigation. Also, many firewalls strip out "refer" header.
2) To ensure that your action does not execute twice, check the
request method. When you click "language" button, browser submits a
POST request. You need to process that. When you redirect to the same
action, browser sends a GET request, and you do not need to process it
(unless you have "command links" on a page).
Michael.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org