View Javadoc

1   package org.apache.turbine.services.localization;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Locale;
23  import java.util.ResourceBundle;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  import org.apache.turbine.services.TurbineServices;
28  import org.apache.turbine.util.RunData;
29  
30  /***
31   * Wrapper around the TurbineLocalization Service that makes it easy
32   * to grab something from the service and make the code cleaner.
33   *
34   * <p>
35   *
36   * Instead of typing:
37   *
38   * <br>
39   *
40   * ((LocalizationService)TurbineServices.getInstance()<br>
41   *           .getService(LocalizationService.SERVICE_NAME))<br>
42   *     .getBundle(data)<br>
43   *     .getString(str)<br>
44   *
45   * Now you only need to type:
46   *
47   * <br>
48   *
49   * Localization.getString(str)
50   *
51   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
52   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
53   * @version $Id: Localization.java 534527 2007-05-02 16:10:59Z tv $
54   */
55  public abstract class Localization
56  {
57      /***
58       * Fetches the localized text from the specified bundle, ignoring
59       * any default bundles.
60       *
61       * @see LocalizationService#getString(String, Locale, String)
62       */
63      public static String getString(String bundleName, Locale locale,
64                                     String key)
65      {
66          return getService().getString(bundleName, locale, key);
67      }
68  
69      /***
70       * Pulls a string out of the LocalizationService with the default
71       * locale values of what is defined in the
72       * TurbineResources.properties file for the
73       * locale.default.language and locale.default.country property
74       * values.  If those cannot be found, then the JVM defaults are
75       * used.
76       *
77       * @param key Name of string.
78       * @return A localized String.
79       */
80      public static String getString(String key)
81      {
82          return getService().getString(null, null, key);
83      }
84  
85      /***
86       * @param key Name of the text to retrieve.
87       * @param locale Locale to get text for.
88       * @return Localized text.
89       */
90      public static String getString(String key, Locale locale)
91      {
92          return getService().getString(null, locale, key);
93      }
94  
95      /***
96       * Pulls a string out of the LocalizationService and attempts to
97       * determine the Locale by the Accept-Language header.  If that
98       * header is not present, it will fall back to using the locale
99       * values of what is defined in the TurbineResources.properties
100      * file for the locale.default.language and locale.default.country
101      * property values.  If those cannot be found, then the JVM
102      * defaults are used.
103      *
104      * @param req HttpServletRequest information.
105      * @param key Name of string.
106      * @return A localized String.
107      */
108     public static String getString(String key, HttpServletRequest req)
109     {
110         return getService().getString(null, getLocale(req), key);
111     }
112 
113     /***
114      * Convenience method that pulls a localized string off the
115      * LocalizationService using the default ResourceBundle name
116      * defined in the TurbineResources.properties file and the
117      * specified language name in ISO format.
118      *
119      * @param key Name of string.
120      * @param lang Desired language for the localized string.
121      * @return A localized string.
122      */
123     public static String getString(String key, String lang)
124     {
125         return getString(getDefaultBundleName(), new Locale(lang, ""), key);
126     }
127 
128     /***
129      * Convenience method to get a ResourceBundle based on name.
130      *
131      * @param bundleName Name of bundle.
132      * @return A localized ResourceBundle.
133      */
134     public static ResourceBundle getBundle(String bundleName)
135     {
136         return getService().getBundle(bundleName);
137     }
138 
139     /***
140      * Convenience method to get a ResourceBundle based on name and
141      * HTTP Accept-Language header.
142      *
143      * @param bundleName Name of bundle.
144      * @param languageHeader A String with the language header.
145      * @return A localized ResourceBundle.
146      */
147     public static ResourceBundle getBundle(String bundleName,
148                                            String languageHeader)
149     {
150         return getService().getBundle(bundleName, languageHeader);
151     }
152 
153     /***
154      * Convenience method to get a ResourceBundle based on name and
155      * HTTP Accept-Language header in HttpServletRequest.
156      *
157      * @param req HttpServletRequest.
158      * @return A localized ResourceBundle.
159      */
160     public static ResourceBundle getBundle(HttpServletRequest req)
161     {
162         return getService().getBundle(req);
163     }
164 
165     /***
166      * Convenience method to get a ResourceBundle based on name and
167      * HTTP Accept-Language header in HttpServletRequest.
168      *
169      * @param bundleName Name of bundle.
170      * @param req HttpServletRequest.
171      * @return A localized ResourceBundle.
172      */
173     public static ResourceBundle getBundle(String bundleName,
174                                            HttpServletRequest req)
175     {
176         return getService().getBundle(bundleName, req);
177     }
178 
179     /***
180      * Convenience method to get a ResourceBundle based on name and
181      * Locale.
182      *
183      * @param bundleName Name of bundle.
184      * @param locale A Locale.
185      * @return A localized ResourceBundle.
186      */
187     public static ResourceBundle getBundle(String bundleName, Locale locale)
188     {
189         return getService().getBundle(bundleName, locale);
190     }
191 
192     /***
193      * This method sets the name of the default bundle.
194      *
195      * @param defaultBundle Name of default bundle.
196      */
197     public static void setBundle(String defaultBundle)
198     {
199         getService().setBundle(defaultBundle);
200     }
201 
202     /***
203      * Attempts to pull the <code>Accept-Language</code> header out of
204      * the HttpServletRequest object and then parse it.  If the header
205      * is not present, it will return a null Locale.
206      *
207      * @param req HttpServletRequest.
208      * @return A Locale.
209      */
210     public static Locale getLocale(HttpServletRequest req)
211     {
212         return getService().getLocale(req);
213     }
214 
215     /***
216      * This method parses the <code>Accept-Language</code> header and
217      * attempts to create a Locale out of it.
218      *
219      * @param languageHeader A String with the language header.
220      * @return A Locale.
221      */
222     public static Locale getLocale(String languageHeader)
223     {
224         return getService().getLocale(languageHeader);
225     }
226 
227     /***
228      * @see org.apache.turbine.services.localization.LocalizationService#getDefaultBundle()
229      */
230     public static String getDefaultBundleName()
231     {
232         return getService().getDefaultBundleName();
233     }
234 
235     /***
236      * Formats a localized value using the provided object.
237      *
238      * @param bundleName The bundle in which to look for the localizable text.
239      * @param locale The locale for which to format the text.
240      * @param key The identifier for the localized text to retrieve,
241      * @param arg1 The object to use as {0} when formatting the localized text.
242      * @return Formatted localized text.
243      * @see #format(String, Locale, String, Object[])
244      */
245     public static String format(String bundleName, Locale locale,
246                                 String key, Object arg1)
247     {
248         return getService().format(bundleName, locale, key, arg1);
249     }
250 
251     /***
252      * Formats a localized value using the provided objects.
253      *
254      * @param bundleName The bundle in which to look for the localizable text.
255      * @param locale The locale for which to format the text.
256      * @param key The identifier for the localized text to retrieve,
257      * @param arg1 The object to use as {0} when formatting the localized text.
258      * @param arg2 The object to use as {1} when formatting the localized text.
259      * @return Formatted localized text.
260      * @see #format(String, Locale, String, Object[])
261      */
262     public static String format(String bundleName, Locale locale,
263                                 String key, Object arg1, Object arg2)
264     {
265         return getService().format(bundleName, locale, key, arg1, arg2);
266     }
267 
268     /***
269      * Formats a localized value using the provided objects.
270      *
271      * @param bundleName The bundle in which to look for the localizable text.
272      * @param locale The locale for which to format the text.
273      * @param key The identifier for the localized text to retrieve,
274      * @param args The objects to use as {0}, {1}, etc. when
275      *             formatting the localized text.
276      * @return Formatted localized text.
277      */
278     public static String format(String bundleName, Locale locale,
279                                 String key, Object[] args)
280     {
281         return getService().format(bundleName, locale, key, args);
282     }
283 
284     /***
285      * Gets the <code>LocalizationService</code> implementation.
286      *
287      * @return the LocalizationService implementation.
288      */
289     protected static final LocalizationService getService()
290     {
291         return (LocalizationService) TurbineServices.getInstance()
292                 .getService(LocalizationService.SERVICE_NAME);
293     }
294 
295     /***
296      * @deprecated Call getString(key, data.getRequest()) instead.
297      */
298     public static String getString(RunData data, String key)
299     {
300         return getString(key, data.getRequest());
301     }
302 
303     /***
304      * @deprecated Call getBundle(bundleName, data.getRequest()) instead.
305      */
306     public static ResourceBundle getBundle(String bundleName, RunData data)
307     {
308         return getBundle(bundleName, data.getRequest());
309     }
310 }