You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Everton Agner <ev...@yahoo.com.br> on 2009/11/18 18:37:33 UTC

Res: Request to a unlocalized URL using the Locale stored on session

Sorry about the previous e-mail title... I was going to change before sending it and I forgot.

Well... here's the problem. I didn't explain it well before:

- My app has 2 Supported Languages : 'pt-BR' (default) and 'en'.

- By default, when the app is acessed with an Unlocalized URL (like http://localhost:8181/myapp/start), the framework will identify the Browser's language, and the current Locale for work will be 'pt-BR' (because of my browser's language)

- When the user changes the current Language, every new URL will use the Locale prefix after the app context... Like http://localhost:8181/myapp/en/start

- If I submit a request to an Unlocalized url - like http://localhost:8181/myapp/start - the framework will not use the stored Locale for work... So, the request will use the Default Locale ('pt-BR', in my case)

There is any way I can change this behavior? We're having these problems because of a Code-Generation API that is being lost with some URL's creation.


Thanks :)

Everton Agner



________________________________
De: Everton Agner <ev...@yahoo.com.br>
Para: Tapestry Users <us...@tapestry.apache.org>
Enviadas: Terça-feira, 17 de Novembro de 2009 18:03:30
Assunto: Localization

There is any way to call a i18n page without using a localized url?

Sample:

    1. User selected some language and that was stored in his session.
    2. When he calls a new url, without the language prefix, the result page will use the selected language (stored in session), instead of the default language.

Thanks,

Everton



      ____________________________________________________________________________________
Veja quais são os assuntos do momento no Yahoo! +Buscados
http://br.maisbuscados.yahoo.com


      ____________________________________________________________________________________
Veja quais são os assuntos do momento no Yahoo! +Buscados
http://br.maisbuscados.yahoo.com

Re: Request to a unlocalized URL using the Locale stored on session

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Thu, 15 Jul 2010 10:56:03 -0300, Adam Zimowski <zi...@gmail.com>  
wrote:

> So I *could* do it like this with Tapestry by extending Tapestry's
> filter, but that's not in the spirit of Tapestry. Furthermore, the
> framework is famous for making things amazingly simple. I think I'm
> missing something. What is it?

You can implement a HttpServletRequestFilter and contribute it to the  
HttpServletRequestHandler service. It's basically a servlet filter, but  
inside the Tapestry infrastructure. All requests, even the ones not  
handled by Tapestry, are passed to it. If you want to intercept just  
Tapestry-handled events, implement a RequestFilter instead.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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


Re: Request to a unlocalized URL using the Locale stored on session

Posted by Adam Zimowski <zi...@gmail.com>.
It seems to me that neither PersistentLocale nor LocalizationSetter
truly force desired locale for the request. After implementing
solution suggested here, I'm still exhibiting undesired bahavior, that
is stripping locale from the URL makes tapestry use browser's locale,
not the one I have forced. In the LocaleRequestFilter service (for
simplicity sake) I am brute forcing french locale for every request:

persistentLocale.set(new Locale("fr"));

(my browser's locale is en_us). When I request my home page, English
locale is nevertheless (I expected french), and then subsequently
Tapestry renders all links and forms with an /fr/ suffix since that's
what I forced. Functionally this is not what I want. Here is how we
currently handle localisation: http://www.avenuesupply.ca - notice it
is URL agnostic.

My team is just starting a huge project to rewrite this very large
ecommerce app from Struts 1 to Tapestry. This codebase has several
deployments, but this particular deployment is localized, and in the
current Struts version, we are using pure Servlet API to accomplish
this, which is actually very, very simple. We have a LocaleFilter
which checks HttpSession for selected language value, and if found, it
creates an instance of HttpServletRequestWrapper with a forced locale.
Struts doesn't even know (or care) what happened. By the time request
hits the framework it has the desired locale.

Here is a snippet from our servlet filter in the current non-Tapestry version:

if(BooleanUtils.isTrue(isLocaleForced)) {
  requestToFilter =
    new LocaleHttpServletRequestWrapper(aRequest, forcedlocale);
  response.setLocale(forcedlocale);
}

I know in Tapestry I can disable URL localisation by contributing
false for SymbolConstants.ENCODE_LOCALE_INTO_PATH.

So I *could* do it like this with Tapestry by extending Tapestry's
filter, but that's not in the spirit of Tapestry. Furthermore, the
framework is famous for making things amazingly simple. I think I'm
missing something. What is it?

Adam

On Wed, Jul 14, 2010 at 11:56 AM, Thiago H. de Paula Figueiredo
<th...@gmail.com> wrote:
> On Wed, 14 Jul 2010 13:36:50 -0300, Adam Zimowski <zi...@gmail.com>
> wrote:
>
>> @Persist
>> private String language;
>
> You should use @SessionState instead and then use the
> ApplicationStateManager to read or set it.
>
> --
> Thiago H. de Paula Figueiredo
> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer, and
> instructor
> Owner, Ars Machina Tecnologia da Informação Ltda.
> http://www.arsmachina.com.br
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

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


Re: Request to a unlocalized URL using the Locale stored on session

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Wed, 14 Jul 2010 13:36:50 -0300, Adam Zimowski <zi...@gmail.com>  
wrote:

> @Persist
> private String language;

You should use @SessionState instead and then use the  
ApplicationStateManager to read or set it.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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


Re: Request to a unlocalized URL using the Locale stored on session

Posted by Adam Zimowski <zi...@gmail.com>.
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
<ev...@yahoo.com.br> wrote:
> Thanks a lot, Juan and Thiago!
>
> We'll try to implement this idea... :)
>
>
>
>
> ________________________________
> De: Juan E. Maya <ma...@gmail.com>
> Para: Tapestry users <us...@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
> <th...@gmail.com> wrote:
>> Em Wed, 18 Nov 2009 15:53:32 -0200, Juan E. Maya <ma...@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-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@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-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Res: Request to a unlocalized URL using the Locale stored on session

Posted by Everton Agner <ev...@yahoo.com.br>.
Thanks a lot, Juan and Thiago!

We'll try to implement this idea... :)




________________________________
De: Juan E. Maya <ma...@gmail.com>
Para: Tapestry users <us...@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
<th...@gmail.com> wrote:
> Em Wed, 18 Nov 2009 15:53:32 -0200, Juan E. Maya <ma...@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-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

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


      ____________________________________________________________________________________
Veja quais são os assuntos do momento no Yahoo! +Buscados
http://br.maisbuscados.yahoo.com

Re: Request to a unlocalized URL using the Locale stored on session

Posted by "Juan E. Maya" <ma...@gmail.com>.
:) 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
<th...@gmail.com> wrote:
> Em Wed, 18 Nov 2009 15:53:32 -0200, Juan E. Maya <ma...@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-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

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


Re: Request to a unlocalized URL using the Locale stored on session

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
Em Wed, 18 Nov 2009 15:53:32 -0200, Juan E. Maya <ma...@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-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Request to a unlocalized URL using the Locale stored on session

Posted by "Juan E. Maya" <ma...@gmail.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);
	}

On Wed, Nov 18, 2009 at 6:37 PM, Everton Agner
<ev...@yahoo.com.br> wrote:
> Sorry about the previous e-mail title... I was going to change before sending it and I forgot.
>
> Well... here's the problem. I didn't explain it well before:
>
> - My app has 2 Supported Languages : 'pt-BR' (default) and 'en'.
>
> - By default, when the app is acessed with an Unlocalized URL (like http://localhost:8181/myapp/start), the framework will identify the Browser's language, and the current Locale for work will be 'pt-BR' (because of my browser's language)
>
> - When the user changes the current Language, every new URL will use the Locale prefix after the app context... Like http://localhost:8181/myapp/en/start
>
> - If I submit a request to an Unlocalized url - like http://localhost:8181/myapp/start - the framework will not use the stored Locale for work... So, the request will use the Default Locale ('pt-BR', in my case)
>
> There is any way I can change this behavior? We're having these problems because of a Code-Generation API that is being lost with some URL's creation.
>
>
> Thanks :)
>
> Everton Agner
>
>
>
> ________________________________
> De: Everton Agner <ev...@yahoo.com.br>
> Para: Tapestry Users <us...@tapestry.apache.org>
> Enviadas: Terça-feira, 17 de Novembro de 2009 18:03:30
> Assunto: Localization
>
> There is any way to call a i18n page without using a localized url?
>
> Sample:
>
>    1. User selected some language and that was stored in his session.
>    2. When he calls a new url, without the language prefix, the result page will use the selected language (stored in session), instead of the default language.
>
> Thanks,
>
> Everton
>
>
>
>      ____________________________________________________________________________________
> Veja quais são os assuntos do momento no Yahoo! +Buscados
> http://br.maisbuscados.yahoo.com
>
>
>      ____________________________________________________________________________________
> Veja quais são os assuntos do momento no Yahoo! +Buscados
> http://br.maisbuscados.yahoo.com

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