KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > velocity > LanguageServlet


1 /*
2  * Filename: LanguageServlet.java
3  *
4  * Created on 02-May-04
5  */

6 package org.roller.presentation.velocity;
7
8 import java.util.Locale JavaDoc;
9
10 import javax.servlet.ServletConfig JavaDoc;
11 import javax.servlet.ServletContext JavaDoc;
12 import javax.servlet.ServletException JavaDoc;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15 import javax.servlet.http.HttpSession JavaDoc;
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 /**
26  * Changes the language of the current Locale to the language specified
27  * by the user. The new language must be supported by Roller.
28  *
29  * And new supported languages to the web.servlet-init-param value. Make sure you add language-only
30  * values at the end of a chain. So "en_US,en" instead of "en,en_US". And no spaces.
31  *
32  * @web.servlet name="LanguageServlet" load-on-startup="10"
33  * @web.servlet-init-param name="org.roller.presentation.supported.languages" value="en,nl,zh_cn,zh_tw,vi"
34  *
35  * @web.servlet-mapping url-pattern="/language/*"
36  *
37  * @author <a HREF="mailto:molen@mail.com">Jaap van der Molen</a>
38  * @version $Revision: 1.8 $
39  */

40 public class LanguageServlet extends BasePageServlet
41 {
42     static final long serialVersionUID = -6548723098429557183L;
43     
44     /**
45      * Logger
46      */

47     private static Log mLogger =
48         LogFactory.getFactory().getInstance(LanguageServlet.class);
49
50     /**
51      * @see org.roller.presentation.velocity.BasePageServlet#init(javax.servlet.ServletConfig)
52      */

53     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc
54     {
55         super.init(config);
56
57         // load supported languages
58
ServletContext JavaDoc ctx = config.getServletContext();
59         String JavaDoc supportedLanguages =
60             config.getInitParameter(LanguageUtil.SUPPORTED_LANGUAGES);
61         if (supportedLanguages != null
62             && supportedLanguages.trim().length() > 0)
63         {
64             // extract langauges
65
ctx.setAttribute(
66                 LanguageUtil.SUPPORTED_LANGUAGES,
67                 LanguageUtil.extractLanguages(supportedLanguages));
68         }
69     }
70
71     /**
72      * @see org.roller.presentation.velocity.BasePageServlet#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.apache.velocity.context.Context)
73      */

74     public Template handleRequest(
75         HttpServletRequest JavaDoc request,
76         HttpServletResponse JavaDoc response,
77         Context JavaDoc ctx) throws Exception JavaDoc
78     {
79         mLogger.debug("Processing language change...");
80         ServletContext JavaDoc servletContext = RollerContext.getServletContext();
81         
82         Locale JavaDoc[] supportedLanguages =
83             LanguageUtil.getSupportedLanguages(servletContext);
84
85         if (supportedLanguages == null || supportedLanguages.length == 0)
86         {
87             // add error message
88
ctx.put("languageError", "Unable to switch language: no supported languages defined.");
89             // proceed with request serving
90
return super.handleRequest(request, response, ctx);
91         }
92
93         String JavaDoc newLang = request.getParameter("language");
94         mLogger.debug("New language in Request: " + newLang);
95         if (newLang == null || newLang.length() == 0) {
96             // add error message
97
ctx.put("languageError", "Unable to switch language: no new language specified.");
98             // proceed with request serving
99
return super.handleRequest(request, response, ctx);
100         }
101         
102         Locale JavaDoc newLocale = LanguageUtil.createLocale(newLang);
103
104         // verify if new language is supported
105
if (!LanguageUtil.isSupported(newLocale, servletContext)) {
106             // add error message
107
ctx.put("languageError", "Unable to switch language: new language '"+newLang+"' is not supported.");
108             // proceed with request serving
109
return super.handleRequest(request, response, ctx);
110         }
111
112         // by now, all should be fine: change Locale
113
HttpSession JavaDoc session = request.getSession();
114         session.setAttribute(Globals.LOCALE_KEY, newLocale);
115         mLogger.debug("Changed language to: " + newLocale);
116
117         // proceed with request serving
118
return super.handleRequest(request, response, ctx);
119     }
120
121 }
122
Popular Tags