KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > servlet > jsp > jstl > fmt > LocaleSupport


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package javax.servlet.jsp.jstl.fmt;
18
19 import java.text.MessageFormat JavaDoc;
20 import java.util.MissingResourceException JavaDoc;
21 import java.util.ResourceBundle JavaDoc;
22
23 import javax.servlet.jsp.PageContext JavaDoc;
24
25 import org.apache.taglibs.standard.tag.common.fmt.BundleSupport;
26 import org.apache.taglibs.standard.tag.common.fmt.MessageSupport;
27
28 /**
29  * Class which exposes the locale-determination logic for resource bundles
30  * through convenience methods.
31  *
32  * <p> This class may be useful to any tag handler implementation that needs
33  * to produce localized messages. For example, this might be useful for
34  * exception messages that are intended directly for user consumption on an
35  * error page.
36  *
37  * @author Jan Luehe
38  */

39
40 public class LocaleSupport {
41
42     /**
43      * Retrieves the localized message corresponding to the given key.
44      *
45      * <p> The given key is looked up in the resource bundle of the default
46      * I18N localization context, which is retrieved from the
47      * <tt>javax.servlet.jsp.jstl.fmt.localizationContext</tt> configuration
48      * setting.
49      *
50      * <p> If the configuration setting is empty, or the default I18N
51      * localization context does not contain any resource bundle, or the given
52      * key is undefined in its resource bundle, the string "???&lt;key&gt;???" is
53      * returned, where "&lt;key&gt;" is replaced with the given key.
54      *
55      * @param pageContext the page in which to get the localized message
56      * corresponding to the given key
57      * @param key the message key
58      *
59      * @return the localized message corresponding to the given key
60      */

61     public static String JavaDoc getLocalizedMessage(PageContext JavaDoc pageContext,
62                                              String JavaDoc key) {
63     return getLocalizedMessage(pageContext, key, null, null);
64     }
65
66     /**
67      * Retrieves the localized message corresponding to the given key.
68      *
69      * <p> The given key is looked up in the resource bundle with the given
70      * base name.
71      *
72      * <p> If no resource bundle with the given base name exists, or the given
73      * key is undefined in the resource bundle, the string "???&lt;key&gt;???" is
74      * returned, where "&lt;key&gt;" is replaced with the given key.
75      *
76      * @param pageContext the page in which to get the localized message
77      * corresponding to the given key
78      * @param key the message key
79      * @param basename the resource bundle base name
80      *
81      * @return the localized message corresponding to the given key
82      */

83     public static String JavaDoc getLocalizedMessage(PageContext JavaDoc pageContext,
84                                              String JavaDoc key,
85                                              String JavaDoc basename) {
86     return getLocalizedMessage(pageContext, key, null, basename);
87     }
88
89     /**
90      * Retrieves the localized message corresponding to the given key, and
91      * performs parametric replacement using the arguments specified via
92      * <tt>args</tt>.
93      *
94      * <p> See the specification of the &lt;fmt:message&gt; action for a description
95      * of how parametric replacement is implemented.
96      *
97      * <p> The localized message is retrieved as in
98      * {@link #getLocalizedMessage(javax.servlet.jsp.PageContext,java.lang.String) getLocalizedMessage(pageContext, key)}.
99      *
100      * @param pageContext the page in which to get the localized message
101      * corresponding to the given key
102      * @param key the message key
103      * @param args the arguments for parametric replacement
104      *
105      * @return the localized message corresponding to the given key
106      */

107     public static String JavaDoc getLocalizedMessage(PageContext JavaDoc pageContext,
108                                              String JavaDoc key,
109                                              Object JavaDoc[] args) {
110     return getLocalizedMessage(pageContext, key, args, null);
111     }
112
113     /**
114      * Retrieves the localized message corresponding to the given key, and
115      * performs parametric replacement using the arguments specified via
116      * <tt>args</tt>.
117      *
118      * <p> See the specification of the &lt;fmt:message&gt; action for a description
119      * of how parametric replacement is implemented.
120      *
121      * <p> The localized message is retrieved as in
122      * {@link #getLocalizedMessage(javax.servlet.jsp.PageContext,java.lang.String, java.lang.String) getLocalizedMessage(pageContext, key, basename)}.
123      *
124      * @param pageContext the page in which to get the localized message
125      * corresponding to the given key
126      * @param key the message key
127      * @param args the arguments for parametric replacement
128      * @param basename the resource bundle base name
129      *
130      * @return the localized message corresponding to the given key
131      */

132     public static String JavaDoc getLocalizedMessage(PageContext JavaDoc pageContext,
133                                              String JavaDoc key,
134                                              Object JavaDoc[] args,
135                                              String JavaDoc basename) {
136     LocalizationContext locCtxt = null;
137     String JavaDoc message = MessageSupport.UNDEFINED_KEY + key
138         + MessageSupport.UNDEFINED_KEY;
139
140     if (basename != null) {
141         locCtxt = BundleSupport.getLocalizationContext(pageContext, basename);
142     } else {
143         locCtxt = BundleSupport.getLocalizationContext(pageContext);
144     }
145
146     if (locCtxt != null) {
147         ResourceBundle JavaDoc bundle = locCtxt.getResourceBundle();
148         if (bundle != null) {
149         try {
150             message = bundle.getString(key);
151             if (args != null) {
152             MessageFormat JavaDoc formatter = new MessageFormat JavaDoc("");
153             if (locCtxt.getLocale() != null) {
154                 formatter.setLocale(locCtxt.getLocale());
155             }
156             formatter.applyPattern(message);
157             message = formatter.format(args);
158             }
159         } catch (MissingResourceException JavaDoc mre) {
160         }
161         }
162     }
163
164     return message;
165     }
166 }
167
168
Popular Tags