1
2 package org.apache.turbine.services.localization;
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import java.util.Locale;
21 import java.util.ResourceBundle;
22
23 import javax.servlet.http.HttpServletRequest;
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 getString(String bundleName, Locale locale,
62 String 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 getString(String 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 getString(String key, Locale 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 getString(String key, HttpServletRequest 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 getString(String key, String lang)
122 {
123 return getString(getDefaultBundleName(), new Locale(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 getBundle(String 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 getBundle(String bundleName,
146 String 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 getBundle(HttpServletRequest 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 getBundle(String bundleName,
172 HttpServletRequest 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 getBundle(String bundleName, Locale 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 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 getLocale(HttpServletRequest 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 getLocale(String languageHeader)
221 {
222 return getService().getLocale(languageHeader);
223 }
224
225 /***
226 * @see org.apache.turbine.services.localization.LocalizationService#getDefaultBundle()
227 */
228 public static String 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 getString(RunData data, String 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 getBundle(String bundleName, RunData data)
256 {
257 return getBundle(bundleName, data.getRequest());
258 }
259 }