You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ignacio de Córdoba <ic...@skios.es> on 2009/02/01 17:11:33 UTC

How to change locale inside an Action method

Hi there,
I've been searching through struts2 docs and old threads here but I've found
no way of changing current locale from inside an action. I know I can pass a
parameter in the request, and maybe other ways to change it... but I am
looking for a method that allows me to change current locale from inside the
action.
I used to do setLocale(...) but that method doesn't seem to be implemented
in ActionSupport.

Thanks for any help,
Ignacio
-- 
View this message in context: http://www.nabble.com/How-to-change-locale-inside-an-Action-method-tp21776908p21776908.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: How to change locale inside an Action method

Posted by Ignacio de Córdoba <ic...@skios.es>.
Great attitude yours :-)

I'll explain my (simplified) case... I am migrating a struts1 content
manager to struts2; maybe I still stick to struts1 concepts but... imagine
there are several articles in the users screen. They might be written in
different languages; an article language is set in a property in the
ArticleEJB, so the view layer doesn't know which language they are written.
When the user clicks in an article, if current locale is not the one in that
article, I want my action to change the session locale. I might not want to
change that locale depending on other article properties.
I know this can be done by getting the locale from the article and adding it
as a request_locale parameter in the link but I will not always want to
change locale, only for some articles. I want that decision to be taken in
that Action, not in the view JSP layer or in previous Action.

I am already using the request_locale i18n interceptor aproach in other
circumstances, when I know in advance I want to change the locale. It's
perfect for that but maybe not for the case exposed.

Ignacio

-- 
View this message in context: http://www.nabble.com/How-to-change-locale-inside-an-Action-method-tp21776908p21778002.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: How to change locale inside an Action method

Posted by Wes Wannemacher <we...@wantii.com>.
On Sunday 01 February 2009 12:10:00 Ignacio de Córdoba wrote:
> OK. Thanks again... after reading some other docs I'll be doing:
>
> ActionContext.getContext().setLocale(locale);
> session.put(com.opensymphony.xwork2.interceptor.I18nInterceptor.DEFAULT_SES
>SION_ATTRIBUTE,locale); session.put(Config.FMT_LOCALE, locale); //Also for
> JSTL, not only Struts.
> session.put("javax.servlet.jsp.jstl.fmt.locale.session", locale);//Just to
> be really sure
>
> (I hope this will "really" change locale)

The only question I have is what's wrong with leaving it up to the 
I18nInterceptor (setting the request_locale parameter)? If it's not fitting 
your needs, consider filing an improvement JIRA. It seems that other than 
setting it for JSTL, you are pretty much simply duplicating the logic of the 
interceptor.

Localization is often one of the things that I do not test as much as 
everything else. I personally don't have any apps that need properly 
localized. So, if you have a use-case that we should be considering, let us 
know so that we can make it work :)


-- 

Wes Wannemacher
Author - Struts 2 In Practice 
Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more
http://www.manning.com/wannemacher


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


Re: How to change locale inside an Action method

Posted by Ignacio de Córdoba <ic...@skios.es>.
OK. Thanks again... after reading some other docs I'll be doing:

ActionContext.getContext().setLocale(locale);
session.put(com.opensymphony.xwork2.interceptor.I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE,locale);	
session.put(Config.FMT_LOCALE, locale); //Also for JSTL, not only Struts.
session.put("javax.servlet.jsp.jstl.fmt.locale.session", locale);//Just to
be really sure

(I hope this will "really" change locale)

Ignacio
-- 
View this message in context: http://www.nabble.com/How-to-change-locale-inside-an-Action-method-tp21776908p21777642.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: How to change locale inside an Action method

Posted by Wes Wannemacher <we...@wantii.com>.
On Sunday 01 February 2009 12:02:19 Ignacio de Córdoba wrote:
> Thanks for your reply,
> I had read the i18n docs you refer but still find no info about changing
> the session locale inside an Action.
> Anyway, after reading the source of the Interceptor, I understand that
> doing:
>
> session.put(i18nInterceptor.DEFAULT_SESSION_ATTRIBUTE,myNewLocale);
>
> in my Action should change the locale in the session. Do you believe that's
> right and nothing extra needs to be done?
>


Sorry, I guess I wasn't clear, you also have to change it on the 
ActionContext...

ActionContext context = ActionContext.getContext();
context.setLocale(myNewLocale);

The setLocale method was moved to the ActionContext.

-Wes

-- 

Wes Wannemacher
Author - Struts 2 In Practice 
Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more
http://www.manning.com/wannemacher


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


Re: How to change locale inside an Action method

Posted by Ignacio de Córdoba <ic...@skios.es>.
Thanks for your reply,
I had read the i18n docs you refer but still find no info about changing the
session locale inside an Action.
Anyway, after reading the source of the Interceptor, I understand that
doing:

session.put(i18nInterceptor.DEFAULT_SESSION_ATTRIBUTE,myNewLocale);

in my Action should change the locale in the session. Do you believe that's
right and nothing extra needs to be done?

thanks,
Ignacio
-- 
View this message in context: http://www.nabble.com/How-to-change-locale-inside-an-Action-method-tp21776908p21777515.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: How to change locale inside an Action method

Posted by Wes Wannemacher <we...@wantii.com>.
On Sunday 01 February 2009 11:11:33 Ignacio de Córdoba wrote:
> Hi there,
> I've been searching through struts2 docs and old threads here but I've
> found no way of changing current locale from inside an action. I know I can
> pass a parameter in the request, and maybe other ways to change it... but I
> am looking for a method that allows me to change current locale from inside
> the action.
> I used to do setLocale(...) but that method doesn't seem to be implemented
> in ActionSupport.
>

Locale manipulation is done in the I18nInterceptor. It does two things, 
watches for the request_locale parameter, and sets the locale on the action 
and in the session if it has changed. To change it from within the action, you 
would change it on the ActionContext... However, that will only change it for 
the duration of the current request. To change it for future requests, you 
have to change the locale object on the session as well. Take a look at the 
docs here - 

http://struts.apache.org/2.x/docs/i18n-interceptor.html

As well as the source here - 

http://svn.opensymphony.com/fisheye/browse/xwork/trunk/src/java/com/opensymphony/xwork2/interceptor/I18nInterceptor.java?r=1833 


-- 

Wes Wannemacher
Author - Struts 2 In Practice 
Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more
http://www.manning.com/wannemacher


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