KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > localization > Localization


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

19
20 import java.util.Locale JavaDoc;
21 import java.util.ResourceBundle JavaDoc;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24
25 import org.apache.turbine.services.TurbineServices;
26 import org.apache.turbine.util.RunData;
27
28 /**
29  * Wrapper around the TurbineLocalization Service that makes it easy
30  * to grab something from the service and make the code cleaner.
31  *
32  * <p>
33  *
34  * Instead of typing:
35  *
36  * <br>
37  *
38  * ((LocalizationService)TurbineServices.getInstance()<br>
39  * .getService(LocalizationService.SERVICE_NAME))<br>
40  * .getBundle(data)<br>
41  * .getString(str)<br>
42  *
43  * Now you only need to type:
44  *
45  * <br>
46  *
47  * Localization.getString(str)
48  *
49  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
50  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
51  * @version $Id: Localization.java,v 1.10.2.2 2004/05/20 03:06:51 seade Exp $
52  */

53 public abstract class Localization
54 {
55     /**
56      * Fetches the localized text from the specified bundle, ignoring
57      * any default bundles.
58      *
59      * @see LocalizationService#getString(String, Locale, String)
60      */

61     public static String JavaDoc getString(String JavaDoc bundleName, Locale JavaDoc locale,
62                                    String JavaDoc key)
63     {
64         return getService().getString(bundleName, locale, key);
65     }
66
67     /**
68      * Pulls a string out of the LocalizationService with the default
69      * locale values of what is defined in the
70      * TurbineResources.properties file for the
71      * locale.default.language and locale.default.country property
72      * values. If those cannot be found, then the JVM defaults are
73      * used.
74      *
75      * @param key Name of string.
76      * @return A localized String.
77      */

78     public static String JavaDoc getString(String JavaDoc key)
79     {
80         return getService().getString(null, null, key);
81     }
82
83     /**
84      * @param key Name of the text to retrieve.
85      * @param locale Locale to get text for.
86      * @return Localized text.
87      */

88     public static String JavaDoc getString(String JavaDoc key, Locale JavaDoc locale)
89     {
90         return getService().getString(null, locale, key);
91     }
92
93     /**
94      * Pulls a string out of the LocalizationService and attempts to
95      * determine the Locale by the Accept-Language header. If that
96      * header is not present, it will fall back to using the locale
97      * values of what is defined in the TurbineResources.properties
98      * file for the locale.default.language and locale.default.country
99      * property values. If those cannot be found, then the JVM
100      * defaults are used.
101      *
102      * @param req HttpServletRequest information.
103      * @param key Name of string.
104      * @return A localized String.
105      */

106     public static String JavaDoc getString(String JavaDoc key, HttpServletRequest JavaDoc req)
107     {
108         return getService().getString(null, getLocale(req), key);
109     }
110
111     /**
112      * Convenience method that pulls a localized string off the
113      * LocalizationService using the default ResourceBundle name
114      * defined in the TurbineResources.properties file and the
115      * specified language name in ISO format.
116      *
117      * @param key Name of string.
118      * @param lang Desired language for the localized string.
119      * @return A localized string.
120      */

121     public static String JavaDoc getString(String JavaDoc key, String JavaDoc lang)
122     {
123         return getString(getDefaultBundleName(), new Locale JavaDoc(lang, ""), key);
124     }
125
126     /**
127      * Convenience method to get a ResourceBundle based on name.
128      *
129      * @param bundleName Name of bundle.
130      * @return A localized ResourceBundle.
131      */

132     public static ResourceBundle JavaDoc getBundle(String JavaDoc bundleName)
133     {
134         return getService().getBundle(bundleName);
135     }
136
137     /**
138      * Convenience method to get a ResourceBundle based on name and
139      * HTTP Accept-Language header.
140      *
141      * @param bundleName Name of bundle.
142      * @param languageHeader A String with the language header.
143      * @return A localized ResourceBundle.
144      */

145     public static ResourceBundle JavaDoc getBundle(String JavaDoc bundleName,
146                                            String JavaDoc languageHeader)
147     {
148         return getService().getBundle(bundleName, languageHeader);
149     }
150
151     /**
152      * Convenience method to get a ResourceBundle based on name and
153      * HTTP Accept-Language header in HttpServletRequest.
154      *
155      * @param req HttpServletRequest.
156      * @return A localized ResourceBundle.
157      */

158     public static ResourceBundle JavaDoc getBundle(HttpServletRequest JavaDoc req)
159     {
160         return getService().getBundle(req);
161     }
162
163     /**
164      * Convenience method to get a ResourceBundle based on name and
165      * HTTP Accept-Language header in HttpServletRequest.
166      *
167      * @param bundleName Name of bundle.
168      * @param req HttpServletRequest.
169      * @return A localized ResourceBundle.
170      */

171     public static ResourceBundle JavaDoc getBundle(String JavaDoc bundleName,
172                                            HttpServletRequest JavaDoc req)
173     {
174         return getService().getBundle(bundleName, req);
175     }
176
177     /**
178      * Convenience method to get a ResourceBundle based on name and
179      * Locale.
180      *
181      * @param bundleName Name of bundle.
182      * @param locale A Locale.
183      * @return A localized ResourceBundle.
184      */

185     public static ResourceBundle JavaDoc getBundle(String JavaDoc bundleName, Locale JavaDoc locale)
186     {
187         return getService().getBundle(bundleName, locale);
188     }
189
190     /**
191      * This method sets the name of the default bundle.
192      *
193      * @param defaultBundle Name of default bundle.
194      */

195     public static void setBundle(String JavaDoc defaultBundle)
196     {
197         getService().setBundle(defaultBundle);
198     }
199
200     /**
201      * Attempts to pull the <code>Accept-Language</code> header out of
202      * the HttpServletRequest object and then parse it. If the header
203      * is not present, it will return a null Locale.
204      *
205      * @param req HttpServletRequest.
206      * @return A Locale.
207      */

208     public static Locale JavaDoc getLocale(HttpServletRequest JavaDoc req)
209     {
210         return getService().getLocale(req);
211     }
212
213     /**
214      * This method parses the <code>Accept-Language</code> header and
215      * attempts to create a Locale out of it.
216      *
217      * @param languageHeader A String with the language header.
218      * @return A Locale.
219      */

220     public static Locale JavaDoc getLocale(String JavaDoc languageHeader)
221     {
222         return getService().getLocale(languageHeader);
223     }
224
225     /**
226      * @see org.apache.turbine.services.localization.LocalizationService#getDefaultBundle()
227      */

228     public static String JavaDoc getDefaultBundleName()
229     {
230         return getService().getDefaultBundleName();
231     }
232
233     /**
234      * Gets the <code>LocalizationService</code> implementation.
235      *
236      * @return the LocalizationService implementation.
237      */

238     protected static final LocalizationService getService()
239     {
240         return (LocalizationService) TurbineServices.getInstance()
241                 .getService(LocalizationService.SERVICE_NAME);
242     }
243
244     /**
245      * @deprecated Call getString(data.getRequest()) instead.
246      */

247     public static String JavaDoc getString(RunData data, String JavaDoc key)
248     {
249         return getBundle(data.getRequest()).getString(key);
250     }
251
252     /**
253      * @deprecated Call getBundle(bundleName, data.getRequest()) instead.
254      */

255     public static ResourceBundle JavaDoc getBundle(String JavaDoc bundleName, RunData data)
256     {
257         return getBundle(bundleName, data.getRequest());
258     }
259 }
260
Popular Tags