Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Changing Locale Based on Domain

DZone's Guide to

Changing Locale Based on Domain

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

Happy New Year to everybody!!.

With Tapestry one of the advantages is that you can override almost everything but then there are always some parts which are less overridable. One such place is setting locale based on some custom criteria. The locale logic is embedded a bit deeper than you expect into the request processing cycle and that might give you an impression that you won’t be able to override it.

Of course I did find the solution (otherwise I would be writing to the very helpful mailing list Tapestry has) and it was so easy that you just got to admire Tapestry. All you have to do is decorate the ComponentEventLinkEncoder in the Application Module

public class AppModule {
    @Match("ComponentEventLinkEncoder")
    public ComponentEventLinkEncoder decorateComponentEventLinkEncoder(Logger logger,
        ComponentEventLinkEncoder delegate, DomainService domainService, ThreadLocale threadLocale) {
        return new DomainLinkTransformerInterceptor(logger, domainService, delegate, threadLocale);
    }
}

In our interceptor we intercept the calls to url decoding and set the locale there.

public class DomainLinkTransformerInterceptor implements ComponentEventLinkEncoder {

    private final Logger logger;

    private final DomainService domainService;

    private final ComponentEventLinkEncoder delegate;

    private ThreadLocale threadLocale;

    public DomainLinkTransformerInterceptor(Logger logger, DomainService domainService,
        ComponentEventLinkEncoder delegate, ThreadLocale threadLocale) {
        this.logger = logger;
        this.domainService = domainService;
        this.delegate = delegate;
        this.threadLocale = threadLocale;
    }

    @Override
    public Link createPageRenderLink(PageRenderRequestParameters parameters) {
        return delegate.createPageRenderLink(parameters);
    }

    @Override
    public Link createComponentEventLink(ComponentEventRequestParameters parameters, boolean forForm) {
        return delegate.createComponentEventLink(parameters, forForm);
    }

    @Override
    public ComponentEventRequestParameters decodeComponentEventRequest(Request request) {
        ComponentEventRequestParameters parameters = delegate.decodeComponentEventRequest(request);
        threadLocale.setLocale(domainService.getCurrentDomain().getLanguage().getLocale());
        logger.debug("For Domain {} setting locale to {}", domainService.getCurrentDomain(),
            threadLocale.getLocale());
        return parameters;
    }

    @Override
    public PageRenderRequestParameters decodePageRenderRequest(Request request) {
        PageRenderRequestParameters parameters = delegate.decodePageRenderRequest(request);
        threadLocale.setLocale(domainService.getCurrentDomain().getLanguage().getLocale());
        logger.debug("For Domain {} setting locale to {}", domainService.getCurrentDomain(),
            threadLocale.getLocale());
        return parameters;
    }

}

and you are done!.


 

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:

Published at DZone with permission of Taha Siddiqi, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}