I have a language dropdown in my layout component which user selects
to switch desired locale. Upon switching, locale is changed without
affecting the URL and without storing it in the cookie. The locale is
stored in the session. I created the following filter which I then
contributed:

public class LocaleHttpServletRequestFilter implements
HttpServletRequestFilter {

        private Logger log;
        
        public LocaleHttpServletRequestFilter(Logger aLogger) {
                log = aLogger;
        }
        
        public boolean service(
                        HttpServletRequest aRequest,
                        HttpServletResponse aResponse,
                        HttpServletRequestHandler aHandler) throws IOException {

                HttpSession session = aRequest.getSession();

                HttpServletRequest request = null;
                String locale = null;

                if(session != null) {
                        locale = (String)session.getAttribute("YOUR ATTRIBUTE 
ID");
                }

                if(locale != null) {
                        log.debug("FORCED: " + locale);
                        request =
                                new LocaleHttpServletRequestWrapper(aRequest, 
new Locale(locale));
                }
                else {
                        request = aRequest;
                }
                
                return aHandler.service(request, aResponse);
        }

}

For this to work, you must store your language selection in the
session. Unfortunately, you must use T 5.2 and @SessionAttribute
annotation  because otherwise session ID will be page specific and not
work as intented.

Here is my Layout:

public class Layout {

        @Inject
        private Messages messages;
        
        @InjectService("_LocaleList")
        private List<SupportedLocale> supportedLocales;
        
        @Persist
        private String language;
        
        @Inject
        private PersistentLocale persistentLocale;
        
        @Inject
        private Logger log;
        

        public Map<String, String> getLanguagesModel() {
                Map<String, String> languages = null;
                if(supportedLocales.size() > 0) {
                        languages = new LinkedHashMap<String, String>();
                        for(SupportedLocale locale : supportedLocales) {
                                if(locale.isPartOfDropDown()) {
                                        String code = locale.getLocaleCode();
                                        languages.put(code, 
messages.get("language-" + code));
                                }
                        }
                }
                return languages;
        }
        
        @SetupRender
        void initialize() {

                String lang = null;
                
                if(persistentLocale != null) {
                        Locale locale = persistentLocale.get();
                        if(locale != null) {
                                lang = locale.getLanguage();
                                setLanguage(lang);
                        }
                }
                
                log.debug("language: " + lang);
        }
        
        public void setLanguage(String aLanguage) {
                if(log.isDebugEnabled()) log.debug(aLanguage);
                language = aLanguage.toLowerCase();
                persistentLocale.set(new Locale(language));
        }

        public String getLanguage() {
                return language;
        }
}



On Tue, Aug 3, 2010 at 3:52 AM, Thomas Cucchietti
<[email protected]> wrote:
> Hi all!
>
> I have problems with the new locale management (encoding the locale in the
> URL) and i'm currently trying to use the previous locale management (using
> cookie) but it's not as simple as I first thought...
>
> Does someone tried to implement the solution described by immutability in
> the following thread : Possible to revert cookie
> ?<http://markmail.org/message/bz2sqom2xydju6ao?q=revert+cookie+list:org.apache.tapestry.users+order:date-backward>
>
> In case, if someone has another way to achieve that...
>
> Thanks for the help :)
>
> Best Regards,
> Thomas.
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to