1 6 package org.roller.presentation.velocity; 7 8 import java.util.Locale ; 9 10 import javax.servlet.ServletConfig ; 11 import javax.servlet.ServletContext ; 12 import javax.servlet.ServletException ; 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpServletResponse ; 15 import javax.servlet.http.HttpSession ; 16 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 import org.apache.struts.Globals; 20 import org.apache.velocity.Template; 21 import org.apache.velocity.context.Context; 22 import org.roller.presentation.LanguageUtil; 23 import org.roller.presentation.RollerContext; 24 25 40 public class LanguageServlet extends BasePageServlet 41 { 42 static final long serialVersionUID = -6548723098429557183L; 43 44 47 private static Log mLogger = 48 LogFactory.getFactory().getInstance(LanguageServlet.class); 49 50 53 public void init(ServletConfig config) throws ServletException 54 { 55 super.init(config); 56 57 ServletContext ctx = config.getServletContext(); 59 String supportedLanguages = 60 config.getInitParameter(LanguageUtil.SUPPORTED_LANGUAGES); 61 if (supportedLanguages != null 62 && supportedLanguages.trim().length() > 0) 63 { 64 ctx.setAttribute( 66 LanguageUtil.SUPPORTED_LANGUAGES, 67 LanguageUtil.extractLanguages(supportedLanguages)); 68 } 69 } 70 71 74 public Template handleRequest( 75 HttpServletRequest request, 76 HttpServletResponse response, 77 Context ctx) throws Exception 78 { 79 mLogger.debug("Processing language change..."); 80 ServletContext servletContext = RollerContext.getServletContext(); 81 82 Locale [] supportedLanguages = 83 LanguageUtil.getSupportedLanguages(servletContext); 84 85 if (supportedLanguages == null || supportedLanguages.length == 0) 86 { 87 ctx.put("languageError", "Unable to switch language: no supported languages defined."); 89 return super.handleRequest(request, response, ctx); 91 } 92 93 String newLang = request.getParameter("language"); 94 mLogger.debug("New language in Request: " + newLang); 95 if (newLang == null || newLang.length() == 0) { 96 ctx.put("languageError", "Unable to switch language: no new language specified."); 98 return super.handleRequest(request, response, ctx); 100 } 101 102 Locale newLocale = LanguageUtil.createLocale(newLang); 103 104 if (!LanguageUtil.isSupported(newLocale, servletContext)) { 106 ctx.put("languageError", "Unable to switch language: new language '"+newLang+"' is not supported."); 108 return super.handleRequest(request, response, ctx); 110 } 111 112 HttpSession session = request.getSession(); 114 session.setAttribute(Globals.LOCALE_KEY, newLocale); 115 mLogger.debug("Changed language to: " + newLocale); 116 117 return super.handleRequest(request, response, ctx); 119 } 120 121 } 122 | Popular Tags |