I found this thread as a solution to my problem of switching locale. I
can switch locale just fine and setting PersistentLocale works until I
strip the URL from language code. Juan's example below is exactly what
I need, however, because I'm returning to Tapestry after two years of
inactivity, I don't understand how I can access session from custom
request filter he suggested. Do I add session as parameter to the
method? If so, how do I retrieve language I've set in my Layout
component?

More specifically, what happens in my case is this: user switches
locale using a language dropdown (provided in my Layout component),
then language is saved:

@Persist
private String language;

So my language is in session, but how do I access it from request
filter? I have a getLanguage in my Layout component, that's all.

Adam



On Wed, Nov 18, 2009 at 1:44 PM, Everton Agner
<everton_ag...@yahoo.com.br> wrote:
> Thanks a lot, Juan and Thiago!
>
> We'll try to implement this idea... :)
>
>
>
>
> ________________________________
> De: Juan E. Maya <maya.j...@gmail.com>
> Para: Tapestry users <users@tapestry.apache.org>
> Enviadas: Quarta-feira, 18 de Novembro de 2009 16:25:55
> Assunto: Re: Request to a unlocalized URL using the Locale stored on session
>
> :) u r right tiago! It should a RequestFilter :) My memory betrayed me :)
>
> On Wed, Nov 18, 2009 at 7:06 PM, Thiago H. de Paula Figueiredo
> <thiag...@gmail.com> wrote:
>> Em Wed, 18 Nov 2009 15:53:32 -0200, Juan E. Maya <maya.j...@gmail.com>
>> escreveu:
>>
>>> U can store the locale of the user in a cookie or in db (if u already
>>> have a session) and then set the desired locale in a
>>> ComponentRequestFilter. It would be something like this:
>>
>> Nice implementation, but why a ComponentRequestFilter and not a regular
>> RequestFilter?
>>
>> --
>> Thiago H. de Paula Figueiredo
>> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer, and
>> instructor
>> Owner, software architect and developer, Ars Machina Tecnologia da
>> Informação Ltda.
>> http://www.arsmachina.com.br
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
>> For additional commands, e-mail: users-h...@tapestry.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
> For additional commands, e-mail: users-h...@tapestry.apache.org
>
>
>      
> ____________________________________________________________________________________
> Veja quais são os assuntos do momento no Yahoo! +Buscados
> http://br.maisbuscados.yahoo.com

Hi Everton,

U can store the locale of the user in a cookie or in db (if u already
have a session) and then set the desired locale in a
ComponentRequestFilter. It would be something like this:

public class LocaleComponentRenderRequestFilter implements
ComponentRequestFilter {

       private final SecurityContext securityContext;
       private final PersistentLocale persistentLocale;
       private final LocalizationSetter localizationSetter;
       private final Logger logger;

       /**
        * @param securityContext
        * @param persistentLocale
        * @param logger
        * @author jmayaalv
        */
       public LocaleComponentRenderRequestFilter(SecurityContext
securityContext, PersistentLocale persistentLocale, LocalizationSetter
localizationSetter, Logger logger) {
               super();
               this.securityContext = securityContext;
               this.persistentLocale = persistentLocale;
               this.localizationSetter = localizationSetter;
               this.logger = logger;
       }


       /**
        * Changes the locale
        * @author jmayaalv
        */
       private void changeLocale() {
               if (this.securityContext.isLoggedIn() &&
this.persistentLocale.get()
== null) {

                       String language = GET LANGUANTE HERE FROM A
COOKIE, SESSION OR WHATEVER
                       if (StringUtils.isNotBlank(language) &&
!"en".equalsIgnoreCase(language)) {

                               if (logger.isDebugEnabled()) {
                                       logger.debug("Changing user
locale to '{}'", user.getLanguage());
                               }

this.localizationSetter.setLocaleFromLocaleName(language);
                       }
               }

       }

       @Override
       public void handleComponentEvent(ComponentEventRequestParameters
parameters, ComponentRequestHandler handler) throws IOException {
               changeLocale();
               handler.handleComponentEvent(parameters);

       }

       @Override
       public void handlePageRender(PageRenderRequestParameters parameters,
ComponentRequestHandler handler) throws IOException {
               changeLocale();
               handler.handlePageRender(parameters);

       }
}

After this u just need to contribute the chain in ur module:
public static void
contributeComponentRequestHandler(OrderedConfiguration<ComponentRequestFilter>
configuration,
                       @InjectService("LocaleComponentRenderRequestFilter")
ComponentRequestFilter localeFilter) {

               configuration.add("LocaleFilter", localeFilter);

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to