Discussions

Web tier: servlets, JSP, Web frameworks: Struts or servlet: how to handle localization in the path?

  1. Hello, I'm working on a Struts 1.1 application. I've added support for localization, now I'd want to allow the app automatically select the correct language by specifying the language in the path, such as: http://www.mysite.com/en/login.do and http://www.mysite.com/it/login.do the app should work even when the language identifier is not specified: http://www.mysite.com/login.do and in this case the default language should be used. I've tried using a servlet filter, and an invoker servlet, but with no results. Well, with teh invoker servlet I've able to do it, the problem is that it intercepts everything (including jsp and so on), and the app doesnt work correctly. Is there any other method to implemetn this kind of language selection? Thanks in advance Antonio
  2. Hi I think filters might be of use. However I have to admit the better way to support language switching is to specify a request parameter like http://www.mysite.com/login.do?lng=en once and then retain the language in the session, unless of course you are doing a RESTful application.
  3. Oops, didn't see you actually mentioned servlet filter in your post. Can you tell me why it didn't work for you?
  4. Oops, didn't see you actually mentioned servlet filter in your post. Can you tell me why it didn't work for you?
    Hi, thanks for your reply. Well, it didn't work because I was doing it in the wrong way (I was using HttpServletRequest.getPathInfo(), which always returns null). I've done it again, and it partially works, but I still have a problem. This is how I've implemented it. I use this code to get the language, if specified: path = req.getRequestURI(); index = path.indexOf(req.getContextPath()); if (index != -1) path = path.substring(req.getContextPath().length()); if (path.matches("^.*/../.*")) { language = path.substring(1, 3).toLowerCase(); locale = userSession.getLocale(); if ((locale == null) || !locale.getLanguage().equals(language)) userSession.setLocale(new Locale (language)); } the problem is that if I change the language I'm redirected to the login page. By changing the language I mean manually modifying the language specifier, for instance from: http://www.mysite.com/en/page.do or http://www.mysite.com/page.do to http://www.mysite.com/it/page.do I think this is a struts issue, since I've verified the filter is called, but the form action class isn't.