View Javadoc

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