You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by René Bernhardsgrütter <re...@gmail.com> on 2013/02/05 17:44:50 UTC

localizationSetter.setLocaleFromLocaleName(...) doesn't work correctly

Hi everybody,

I have a problem with cookie stored locales, switched by the user with
T5.3.6. The code snippets are below, commented.

Tapestry loads the LocaleRequestFilter and invokes the service method on
each request. I get the correct language value out of the cookie. The
problem is, that this value isn't stored correctly (as I assume). The
line is
"localizationSetter.setLocaleFromLocaleName(preferredLanguage.toString());".

During the page and component rendering, Tapestry uses always the
English l10n. The default is German (TestComponent.properties => de,
TestComponent_en.properties => en).

Does anyone know where the problem could be?

Any help is appreciated :-)


René


I've tried the following:
AppModule.java:
public static void
contributeApplicationDefaults(MappedConfiguration<String, Object>
configuration) {
    configuration.add(SymbolConstants.ENCODE_LOCALE_INTO_PATH,
false);[...]  // I don't want the language code in the address => to cookie
    configuration.add(SymbolConstants.SUPPORTED_LOCALES, "de,en");
}

   public static void bind(ServiceBinder binder) {
        binder.bind(TimingFilter.class).withId("TimingFilter");
       
binder.bind(LocaleRequestFilter.class).withId("LocaleRequestFilter");[...]
    }

    public void contributeRequestHandler(
            OrderedConfiguration<RequestFilter> configuration,
            final Logger logger,
            final RequestGlobals requestGlobals,
            final Cookies cookies,
            final LocalizationSetter localizationSetter,
            @InjectService("TimingFilter") RequestFilter timingFilter,
            @InjectService("LocaleRequestFilter") RequestFilter
localeRequestFilter) {
        configuration.add("TimingFilter", new TimingFilter(logger));
        configuration.add("LocaleRequestFilter", new
LocaleRequestFilter(requestGlobals, cookies, localizationSetter),
"after:*");
        // This is the RequestFilter for the locales.
    }

In the ...webapp.services.LocaleRequestFilter.java:
public class LocaleRequestFilter implements RequestFilter {

    private RequestGlobals requestGlobals;
    private Cookies cookies;
    private LocalizationSetter localizationSetter;

    public LocaleRequestFilter(RequestGlobals requestGlobals, Cookies
cookies, LocalizationSetter localizationSetter) {
        this.requestGlobals = requestGlobals;
        this.cookies = cookies;
        this.localizationSetter = localizationSetter;
    }

    @Override
    public boolean service(Request request, Response response,
RequestHandler handler) throws IOException {
        TranslatedLanguage preferredLanguage = null; // is an enum
       
        // Here comes some code to read the HTTP header and the cookie
to decide, what to do.
        // This method is invoked and reads _the_correct_ cookie value.
       
        System.out.println("set language : " +
preferredLanguage.toString());
       
localizationSetter.setLocaleFromLocaleName(preferredLanguage.toString());
        // Here could be the problem, because the preferred value isn't
stored.

        return handler.service(request, response);
    }
}

In the component, where I change the cookie value:
   private void onActionFromLanguageMenu() {
       
cookies.writeCookieValue(PlainTraySetting.COOKIE_LANGUAGE_VALUE_NAME,
selectedLanguage.toString());
        // I just write the selected language to the cookie. That works
perfectly.
    }

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


Re: localizationSetter.setLocaleFromLocaleName(...) doesn't work correctly

Posted by René Bernhardsgrütter <re...@gmail.com>.
I've found a solution. It (only?) works with a ComponentRequestFilter
instead the RequestFilter.
The code looks like this:

AppModule.java:
    public static void bind(ServiceBinder binder) {
       
binder.bind(LocaleRequestFilter.class).withId("LocaleRequestFilter");
    }

    public static void contributeComponentRequestHandler(
            OrderedConfiguration<ComponentRequestFilter> configuration,
            @InjectService("LocaleRequestFilter") LocaleRequestFilter
localeRequestFilter) {
        configuration.add("LocaleRequestFilter", localeRequestFilter);
    }

LocaleRequestFilter.java:
public class LocaleRequestFilter implements ComponentRequestFilter {

    private RequestGlobals requestGlobals;
    private Cookies cookies;
    private LocalizationSetter localizationSetter;

    public LocaleRequestFilter(RequestGlobals requestGlobals, Cookies
cookies, LocalizationSetter localizationSetter) {
        this.requestGlobals = requestGlobals;
        this.cookies = cookies;
        this.localizationSetter = localizationSetter;
    }

    private void changeLocaleIfCookieOverridesHttpHeaderValues() {
        // Do some logic to figure out, which Locale should be used in
this (Component or Page-)request.

       
localizationSetter.setLocaleFromLocaleName(preferredLanguage.toString());
    }

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

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

That works, as it should.

It would be great, if someone could explain what the determining
differences for this case are. Why doesn't work the RequestFilter, but
the ComponentRequestFilter does?
Otherwise, no problem. For me, it's done.

Best Regards,
René

On 05.02.2013 17:44, René Bernhardsgrütter wrote:
> Hi everybody,
>
> I have a problem with cookie stored locales, switched by the user with
> T5.3.6. The code snippets are below, commented.
>
> Tapestry loads the LocaleRequestFilter and invokes the service method on
> each request. I get the correct language value out of the cookie. The
> problem is, that this value isn't stored correctly (as I assume). The
> line is
> "localizationSetter.setLocaleFromLocaleName(preferredLanguage.toString());".
>
> During the page and component rendering, Tapestry uses always the
> English l10n. The default is German (TestComponent.properties => de,
> TestComponent_en.properties => en).
>
> Does anyone know where the problem could be?
>
> Any help is appreciated :-)
>
>
> René
>
>
> I've tried the following:
> AppModule.java:
> public static void
> contributeApplicationDefaults(MappedConfiguration<String, Object>
> configuration) {
>     configuration.add(SymbolConstants.ENCODE_LOCALE_INTO_PATH,
> false);[...]  // I don't want the language code in the address => to cookie
>     configuration.add(SymbolConstants.SUPPORTED_LOCALES, "de,en");
> }
>
>    public static void bind(ServiceBinder binder) {
>         binder.bind(TimingFilter.class).withId("TimingFilter");
>        
> binder.bind(LocaleRequestFilter.class).withId("LocaleRequestFilter");[...]
>     }
>
>     public void contributeRequestHandler(
>             OrderedConfiguration<RequestFilter> configuration,
>             final Logger logger,
>             final RequestGlobals requestGlobals,
>             final Cookies cookies,
>             final LocalizationSetter localizationSetter,
>             @InjectService("TimingFilter") RequestFilter timingFilter,
>             @InjectService("LocaleRequestFilter") RequestFilter
> localeRequestFilter) {
>         configuration.add("TimingFilter", new TimingFilter(logger));
>         configuration.add("LocaleRequestFilter", new
> LocaleRequestFilter(requestGlobals, cookies, localizationSetter),
> "after:*");
>         // This is the RequestFilter for the locales.
>     }
>
> In the ...webapp.services.LocaleRequestFilter.java:
> public class LocaleRequestFilter implements RequestFilter {
>
>     private RequestGlobals requestGlobals;
>     private Cookies cookies;
>     private LocalizationSetter localizationSetter;
>
>     public LocaleRequestFilter(RequestGlobals requestGlobals, Cookies
> cookies, LocalizationSetter localizationSetter) {
>         this.requestGlobals = requestGlobals;
>         this.cookies = cookies;
>         this.localizationSetter = localizationSetter;
>     }
>
>     @Override
>     public boolean service(Request request, Response response,
> RequestHandler handler) throws IOException {
>         TranslatedLanguage preferredLanguage = null; // is an enum
>        
>         // Here comes some code to read the HTTP header and the cookie
> to decide, what to do.
>         // This method is invoked and reads _the_correct_ cookie value.
>        
>         System.out.println("set language : " +
> preferredLanguage.toString());
>        
> localizationSetter.setLocaleFromLocaleName(preferredLanguage.toString());
>         // Here could be the problem, because the preferred value isn't
> stored.
>
>         return handler.service(request, response);
>     }
> }
>
> In the component, where I change the cookie value:
>    private void onActionFromLanguageMenu() {
>        
> cookies.writeCookieValue(PlainTraySetting.COOKIE_LANGUAGE_VALUE_NAME,
> selectedLanguage.toString());
>         // I just write the selected language to the cookie. That works
> perfectly.
>     }


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