KickJava   Java API By Example, From Geeks To Geeks.

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


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.jsp.PageContext JavaDoc;
18 import javax.servlet.jsp.tagext.Tag JavaDoc;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.displaytag.Messages;
23 import org.springframework.context.MessageSource;
24 import org.springframework.web.servlet.support.RequestContextUtils;
25
26
27 /**
28  * Spring implementation of a resource provider and locale resolver. Since Displaytag locale resolution is modelled on
29  * the Spring one, it simply forward <code>resolveLocale</code> calls to the Spring-configured LocaleResolver.
30  * @author Fabrizio Giustina
31  * @version $Revision: 737 $ ($Author: fgiust $)
32  */

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

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

41     /**
42      * logger.
43      */

44     private static Log log = LogFactory.getLog(I18nSpringAdapter.class);
45
46     /**
47      * @see LocaleResolver#resolveLocale(HttpServletRequest)
48      */

49     public Locale JavaDoc resolveLocale(HttpServletRequest JavaDoc request)
50     {
51         return RequestContextUtils.getLocale(request);
52     }
53
54     /**
55      * @see I18nResourceProvider#getResource(String, String, Tag, PageContext)
56      */

57     public String JavaDoc getResource(String JavaDoc resourceKey, String JavaDoc defaultValue, Tag JavaDoc tag, PageContext JavaDoc pageContext)
58     {
59         MessageSource messageSource = RequestContextUtils.getWebApplicationContext(pageContext.getRequest());
60         if (messageSource == null)
61         {
62             log.warn("messageSource not found");
63             return null;
64         }
65
66         // if resourceKey isn't defined either, use defaultValue
67
String JavaDoc key = (resourceKey != null) ? resourceKey : defaultValue;
68
69         String JavaDoc message = null;
70
71         message = messageSource.getMessage(key, null, null, RequestContextUtils
72             .getLocale((HttpServletRequest JavaDoc) pageContext.getRequest()));
73
74         // if user explicitely added a titleKey we guess this is an error
75
if (message == null && resourceKey != null)
76         {
77             log.debug(Messages.getString("Localization.missingkey", resourceKey)); //$NON-NLS-1$
78
message = UNDEFINED_KEY + resourceKey + UNDEFINED_KEY;
79         }
80
81         return message;
82
83     }
84 }
85
Popular Tags