KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > localization > Localization


1 package org.apache.fulcrum.localization;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.util.Locale JavaDoc;
58 import java.util.ResourceBundle JavaDoc;
59
60 import javax.servlet.http.HttpServletRequest JavaDoc;
61
62 import org.apache.fulcrum.TurbineServices;
63 import org.apache.turbine.services.yaaficomponent.YaafiComponentService;
64
65 /**
66  * <p>Wrapper around the TurbineLocalization Service that makes it easy
67  * to grab something from the service.</p>
68  *
69  * <p>Instead of typing:
70  *
71  * <blockquote><code><pre>
72  * ((LocalizationService)TurbineServices.getInstance()
73  * .getService(LocalizationService.SERVICE_NAME))
74  * .getBundle(data)
75  * .getString(key)
76  * </pre></code></blockquote>
77  *
78  * You need only type:
79  *
80  * <blockquote><code><pre>
81  * Localization.getString(key)
82  * </pre></code></blockquote>
83  * </p>
84  *
85  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
86  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
87  * @author <a HREF="mailto:leonardr@collab.net">Leonard Richardson</a>
88  * @version $Id: Localization.java 9284 2004-12-02 21:13:20Z dabbous $
89  */

90 public abstract class Localization
91 {
92     private static LocalizationService localizationService;
93     /**
94      * Pulls a string out of the LocalizationService with the default
95      * locale values of what is defined in the
96      * TurbineResources.properties file for the
97      * locale.default.language and locale.default.country property
98      * values. If those cannot be found, then the JVM defaults are
99      * used.
100      *
101      * @param key Name of string.
102      * @return A localized String.
103      */

104     public static String JavaDoc getString(String JavaDoc key)
105     {
106         return getService().getBundle().getString(key);
107     }
108
109     /**
110      * @see LocalizationService#getString(String, Locale, String)
111      */

112     public static String JavaDoc getString(Locale JavaDoc locale, String JavaDoc key)
113     {
114         return getService().getString(null, locale, key);
115     }
116
117     /**
118      * Fetches the localized text from the specified bundle, ignoring
119      * any default bundles.
120      *
121      * @see LocalizationService#getString(String, Locale, String)
122      */

123     public static String JavaDoc getString(String JavaDoc bundleName, Locale JavaDoc locale,
124                                    String JavaDoc key)
125     {
126         return getService().getString(bundleName, locale, key);
127     }
128
129     /**
130      * Convenience method that pulls a localized string off the
131      * LocalizationService using the ResourceBundle based on HTTP
132      * Accept-Language header in HttpServletRequest.
133      *
134      * @param req The HTTP request to parse the <code>Accept-Language</code> of.
135      * @param key Name of string.
136      * @return A localized string.
137      */

138     public static String JavaDoc getString(HttpServletRequest JavaDoc req, String JavaDoc key)
139     {
140         return getService().getBundle(req).getString(key);
141     }
142
143     /**
144      * Convenience method that pulls a localized string off the
145      * LocalizationService using the default ResourceBundle name
146      * defined in the TurbineResources.properties file and the
147      * specified language name in ISO format.
148      *
149      * @param key Name of string.
150      * @param lang Desired language for the localized string.
151      * @return A localized string.
152      */

153     public static String JavaDoc getString(String JavaDoc key, String JavaDoc lang)
154     {
155         return getBundle(getDefaultBundleName(), new Locale JavaDoc(lang, ""))
156             .getString(key);
157     }
158
159     /**
160      * Convenience method to get a ResourceBundle based on name.
161      *
162      * @param bundleName Name of bundle.
163      * @return A localized ResourceBundle.
164      */

165     public static ResourceBundle JavaDoc getBundle(String JavaDoc bundleName)
166     {
167         return getService().getBundle(bundleName);
168     }
169
170     /**
171      * Convenience method to get a ResourceBundle based on name and
172      * HTTP Accept-Language header.
173      *
174      * @param bundleName Name of bundle.
175      * @param languageHeader A String with the language header.
176      * @return A localized ResourceBundle.
177      */

178     public static ResourceBundle JavaDoc getBundle(String JavaDoc bundleName,
179                                            String JavaDoc languageHeader)
180     {
181         return getService().getBundle(bundleName, languageHeader);
182     }
183
184     /**
185      * Convenience method to get a ResourceBundle based on name and
186      * HTTP Accept-Language header in HttpServletRequest.
187      *
188      * @param req HttpServletRequest.
189      * @return A localized ResourceBundle.
190      */

191     public static ResourceBundle JavaDoc getBundle(HttpServletRequest JavaDoc req)
192     {
193         return getService().getBundle(req);
194     }
195
196     /**
197      * Convenience method to get a ResourceBundle based on name and
198      * HTTP Accept-Language header in HttpServletRequest.
199      *
200      * @param bundleName Name of bundle.
201      * @param req HttpServletRequest.
202      * @return A localized ResourceBundle.
203      */

204     public static ResourceBundle JavaDoc getBundle(String JavaDoc bundleName,
205                                            HttpServletRequest JavaDoc req)
206     {
207         return getService().getBundle(bundleName, req);
208     }
209
210     /**
211      * Convenience method to get a ResourceBundle based on name and
212      * Locale.
213      *
214      * @param bundleName Name of bundle.
215      * @param locale A Locale.
216      * @return A localized ResourceBundle.
217      */

218     public static ResourceBundle JavaDoc getBundle(String JavaDoc bundleName, Locale JavaDoc locale)
219     {
220         return getService().getBundle(bundleName, locale);
221     }
222
223     /**
224      * This method sets the name of the default bundle.
225      *
226      * @param defaultBundle Name of default bundle.
227      * @see LocalizationService#setBundle(String)
228      */

229     public static void setBundle(String JavaDoc defaultBundle)
230     {
231         getService().setBundle(defaultBundle);
232     }
233
234     /**
235      * @see LocalizationService#getLocale(HttpServletRequest)
236      */

237     public static Locale JavaDoc getLocale(HttpServletRequest JavaDoc req)
238     {
239         return getService().getLocale(req);
240     }
241
242     /**
243      * This method parses the <code>Accept-Language</code> header and
244      * attempts to create a Locale out of it.
245      *
246      * @param languageHeader A String with the language header.
247      * @return A Locale.
248      */

249     public static Locale JavaDoc getLocale(String JavaDoc languageHeader)
250     {
251         return getService().getLocale(languageHeader);
252     }
253
254     /**
255      * @see LocalizationService#getDefaultBundleName()
256      */

257     public static String JavaDoc getDefaultBundleName()
258     {
259         return getService().getDefaultBundleName();
260     }
261
262     /**
263      * @see LocalizationService#getDefaultCountry()
264      */

265     public static String JavaDoc getDefaultCountry()
266     {
267         return getService().getDefaultCountry();
268     }
269
270     /**
271      * @see LocalizationService#getDefaultLanguage()
272      */

273     public static String JavaDoc getDefaultLanguage()
274     {
275         return getService().getDefaultLanguage();
276     }
277
278     /**
279      * Gets the <code>LocalizationService</code> implementation.
280      *
281      * @return the LocalizationService implementation.
282      */

283     protected static final LocalizationService getService()
284     {
285         if (localizationService==null)
286         {
287             try{
288                 YaafiComponentService yaafi = (YaafiComponentService) TurbineServices.getInstance().getService(
289                     YaafiComponentService.SERVICE_NAME);
290                 localizationService = (LocalizationService) yaafi.lookup(LocalizationService.class.getName());
291             }
292             catch (Exception JavaDoc e)
293             {
294                 throw new RuntimeException JavaDoc("Problem looking up localization service: " + e.getMessage());
295             }
296         }
297         return localizationService;
298     }
299     
300     public static void setLocalizationService(LocalizationService service){
301         localizationService = service;
302     }
303
304     /**
305      * @see LocalizationService#format(String, Locale, String, Object)
306      */

307     public static String JavaDoc format(String JavaDoc bundleName, Locale JavaDoc locale,
308                                 String JavaDoc key, Object JavaDoc arg1)
309     {
310         return getService().format(bundleName, locale, key, arg1);
311     }
312
313     /**
314      * @see LocalizationService#format(String, Locale, String, Object, Object)
315      */

316     public static String JavaDoc format(String JavaDoc bundleName, Locale JavaDoc locale,
317                                 String JavaDoc key, Object JavaDoc arg1, Object JavaDoc arg2)
318     {
319         return getService().format(bundleName, locale, key, arg1, arg2);
320     }
321
322     /**
323      * @see LocalizationService#format(String, Locale, String, Object[])
324      */

325     public static String JavaDoc format(String JavaDoc bundleName, Locale JavaDoc locale,
326                                 String JavaDoc key, Object JavaDoc[] args)
327     {
328         return getService().format(bundleName, locale, key, args);
329     }
330
331
332     // ---- Deprecated method(s) -------------------------------------------
333

334     /**
335      * @deprecated Use getString(Locale, String) instead.
336      */

337     public static String JavaDoc getString(String JavaDoc key, Locale JavaDoc locale)
338     {
339         return getString(locale, key);
340     }
341
342
343     /**
344      * @deprecated Use getString(HttpServletRequest, String) instead.
345      */

346     public static String JavaDoc getString(String JavaDoc key, HttpServletRequest JavaDoc req)
347     {
348         return getString(req, key);
349     }
350 }
351
Popular Tags