KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > localization > I18nStrutsAdapter


1 /**
2  * Licensed under the Artistic License; you may not use this file
3  * except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://displaytag.sourceforge.net/license.html
7  *
8  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */

12 package org.displaytag.localization;
13
14 import java.util.Locale JavaDoc;
15
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.HttpSession JavaDoc;
18 import javax.servlet.jsp.PageContext JavaDoc;
19 import javax.servlet.jsp.tagext.Tag JavaDoc;
20
21 import org.apache.struts.Globals;
22 import org.apache.struts.config.ModuleConfig;
23 import org.apache.struts.util.MessageResources;
24
25
26 /**
27  * Struts implementation of a resource provider and locale resolver. Uses Struts
28  * <code>RequestUtils.getUserLocale()</code> and <code>TagUtils.message()</code> for the lookup.
29  * @author Fabrizio Giustina
30  * @version $Revision: 752 $ ($Author: fgiust $)
31  */

32 public class I18nStrutsAdapter implements I18nResourceProvider, LocaleResolver
33 {
34
35     /**
36      * prefix/suffix for missing entries.
37      */

38     public static final String JavaDoc UNDEFINED_KEY = "???"; //$NON-NLS-1$
39

40     /**
41      * @see LocaleResolver#resolveLocale(HttpServletRequest)
42      */

43     public Locale JavaDoc resolveLocale(HttpServletRequest JavaDoc request)
44     {
45         Locale JavaDoc userLocale = null;
46         HttpSession JavaDoc session = request.getSession(false);
47
48         // Only check session if sessions are enabled
49
if (session != null)
50         {
51             userLocale = (Locale JavaDoc) session.getAttribute(Globals.LOCALE_KEY);
52         }
53
54         if (userLocale == null)
55         {
56             // Returns Locale based on Accept-Language header or the server default
57
userLocale = request.getLocale();
58         }
59
60         return userLocale;
61     }
62
63     /**
64      * @see I18nResourceProvider#getResource(String, String, Tag, PageContext)
65      */

66     public String JavaDoc getResource(String JavaDoc resourceKey, String JavaDoc defaultValue, Tag JavaDoc tag, PageContext JavaDoc pageContext)
67     {
68
69         // if titleKey isn't defined either, use property
70
String JavaDoc key = (resourceKey != null) ? resourceKey : defaultValue;
71
72         // retrieve MessageResources. Don't use TagUtils to mantain Struts 1.1 compatibility
73
MessageResources resources = (MessageResources) pageContext.getAttribute(
74             Globals.MESSAGES_KEY,
75             PageContext.REQUEST_SCOPE);
76
77         if (resources == null)
78         {
79             ModuleConfig moduleConfig = (ModuleConfig) pageContext.getRequest().getAttribute(Globals.MODULE_KEY);
80
81             if (moduleConfig == null)
82             {
83                 moduleConfig = (ModuleConfig) pageContext.getServletContext().getAttribute(Globals.MODULE_KEY);
84                 pageContext.getRequest().setAttribute(Globals.MODULE_KEY, moduleConfig);
85             }
86
87             resources = (MessageResources) pageContext.getAttribute(
88                 Globals.MESSAGES_KEY + moduleConfig.getPrefix(),
89                 PageContext.APPLICATION_SCOPE);
90         }
91
92         if (resources == null)
93         {
94             resources = (MessageResources) pageContext
95                 .getAttribute(Globals.MESSAGES_KEY, PageContext.APPLICATION_SCOPE);
96         }
97
98         String JavaDoc title = null;
99         if (resources != null)
100         {
101             Locale JavaDoc userLocale = resolveLocale((HttpServletRequest JavaDoc) pageContext.getRequest());
102             title = resources.getMessage(userLocale, key);
103         }
104
105         // if user explicitely added a titleKey we guess this is an error
106
if (title == null && resourceKey != null)
107         {
108             title = UNDEFINED_KEY + resourceKey + UNDEFINED_KEY;
109         }
110
111         return title;
112     }
113
114 }
115
Popular Tags