001: package org.apache.turbine.services.localization;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.Locale;
023: import java.util.ResourceBundle;
024:
025: import javax.servlet.http.HttpServletRequest;
026:
027: import org.apache.turbine.services.TurbineServices;
028: import org.apache.turbine.util.RunData;
029:
030: /**
031: * Wrapper around the TurbineLocalization Service that makes it easy
032: * to grab something from the service and make the code cleaner.
033: *
034: * <p>
035: *
036: * Instead of typing:
037: *
038: * <br>
039: *
040: * ((LocalizationService)TurbineServices.getInstance()<br>
041: * .getService(LocalizationService.SERVICE_NAME))<br>
042: * .getBundle(data)<br>
043: * .getString(str)<br>
044: *
045: * Now you only need to type:
046: *
047: * <br>
048: *
049: * Localization.getString(str)
050: *
051: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
052: * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
053: * @version $Id: Localization.java 534527 2007-05-02 16:10:59Z tv $
054: */
055: public abstract class Localization {
056: /**
057: * Fetches the localized text from the specified bundle, ignoring
058: * any default bundles.
059: *
060: * @see LocalizationService#getString(String, Locale, String)
061: */
062: public static String getString(String bundleName, Locale locale,
063: String key) {
064: return getService().getString(bundleName, locale, key);
065: }
066:
067: /**
068: * Pulls a string out of the LocalizationService with the default
069: * locale values of what is defined in the
070: * TurbineResources.properties file for the
071: * locale.default.language and locale.default.country property
072: * values. If those cannot be found, then the JVM defaults are
073: * used.
074: *
075: * @param key Name of string.
076: * @return A localized String.
077: */
078: public static String getString(String key) {
079: return getService().getString(null, null, key);
080: }
081:
082: /**
083: * @param key Name of the text to retrieve.
084: * @param locale Locale to get text for.
085: * @return Localized text.
086: */
087: public static String getString(String key, Locale locale) {
088: return getService().getString(null, locale, key);
089: }
090:
091: /**
092: * Pulls a string out of the LocalizationService and attempts to
093: * determine the Locale by the Accept-Language header. If that
094: * header is not present, it will fall back to using the locale
095: * values of what is defined in the TurbineResources.properties
096: * file for the locale.default.language and locale.default.country
097: * property values. If those cannot be found, then the JVM
098: * defaults are used.
099: *
100: * @param req HttpServletRequest information.
101: * @param key Name of string.
102: * @return A localized String.
103: */
104: public static String getString(String key, HttpServletRequest req) {
105: return getService().getString(null, getLocale(req), key);
106: }
107:
108: /**
109: * Convenience method that pulls a localized string off the
110: * LocalizationService using the default ResourceBundle name
111: * defined in the TurbineResources.properties file and the
112: * specified language name in ISO format.
113: *
114: * @param key Name of string.
115: * @param lang Desired language for the localized string.
116: * @return A localized string.
117: */
118: public static String getString(String key, String lang) {
119: return getString(getDefaultBundleName(), new Locale(lang, ""),
120: key);
121: }
122:
123: /**
124: * Convenience method to get a ResourceBundle based on name.
125: *
126: * @param bundleName Name of bundle.
127: * @return A localized ResourceBundle.
128: */
129: public static ResourceBundle getBundle(String bundleName) {
130: return getService().getBundle(bundleName);
131: }
132:
133: /**
134: * Convenience method to get a ResourceBundle based on name and
135: * HTTP Accept-Language header.
136: *
137: * @param bundleName Name of bundle.
138: * @param languageHeader A String with the language header.
139: * @return A localized ResourceBundle.
140: */
141: public static ResourceBundle getBundle(String bundleName,
142: String languageHeader) {
143: return getService().getBundle(bundleName, languageHeader);
144: }
145:
146: /**
147: * Convenience method to get a ResourceBundle based on name and
148: * HTTP Accept-Language header in HttpServletRequest.
149: *
150: * @param req HttpServletRequest.
151: * @return A localized ResourceBundle.
152: */
153: public static ResourceBundle getBundle(HttpServletRequest req) {
154: return getService().getBundle(req);
155: }
156:
157: /**
158: * Convenience method to get a ResourceBundle based on name and
159: * HTTP Accept-Language header in HttpServletRequest.
160: *
161: * @param bundleName Name of bundle.
162: * @param req HttpServletRequest.
163: * @return A localized ResourceBundle.
164: */
165: public static ResourceBundle getBundle(String bundleName,
166: HttpServletRequest req) {
167: return getService().getBundle(bundleName, req);
168: }
169:
170: /**
171: * Convenience method to get a ResourceBundle based on name and
172: * Locale.
173: *
174: * @param bundleName Name of bundle.
175: * @param locale A Locale.
176: * @return A localized ResourceBundle.
177: */
178: public static ResourceBundle getBundle(String bundleName,
179: Locale locale) {
180: return getService().getBundle(bundleName, locale);
181: }
182:
183: /**
184: * This method sets the name of the default bundle.
185: *
186: * @param defaultBundle Name of default bundle.
187: */
188: public static void setBundle(String defaultBundle) {
189: getService().setBundle(defaultBundle);
190: }
191:
192: /**
193: * Attempts to pull the <code>Accept-Language</code> header out of
194: * the HttpServletRequest object and then parse it. If the header
195: * is not present, it will return a null Locale.
196: *
197: * @param req HttpServletRequest.
198: * @return A Locale.
199: */
200: public static Locale getLocale(HttpServletRequest req) {
201: return getService().getLocale(req);
202: }
203:
204: /**
205: * This method parses the <code>Accept-Language</code> header and
206: * attempts to create a Locale out of it.
207: *
208: * @param languageHeader A String with the language header.
209: * @return A Locale.
210: */
211: public static Locale getLocale(String languageHeader) {
212: return getService().getLocale(languageHeader);
213: }
214:
215: /**
216: * @see org.apache.turbine.services.localization.LocalizationService#getDefaultBundle()
217: */
218: public static String getDefaultBundleName() {
219: return getService().getDefaultBundleName();
220: }
221:
222: /**
223: * Formats a localized value using the provided object.
224: *
225: * @param bundleName The bundle in which to look for the localizable text.
226: * @param locale The locale for which to format the text.
227: * @param key The identifier for the localized text to retrieve,
228: * @param arg1 The object to use as {0} when formatting the localized text.
229: * @return Formatted localized text.
230: * @see #format(String, Locale, String, Object[])
231: */
232: public static String format(String bundleName, Locale locale,
233: String key, Object arg1) {
234: return getService().format(bundleName, locale, key, arg1);
235: }
236:
237: /**
238: * Formats a localized value using the provided objects.
239: *
240: * @param bundleName The bundle in which to look for the localizable text.
241: * @param locale The locale for which to format the text.
242: * @param key The identifier for the localized text to retrieve,
243: * @param arg1 The object to use as {0} when formatting the localized text.
244: * @param arg2 The object to use as {1} when formatting the localized text.
245: * @return Formatted localized text.
246: * @see #format(String, Locale, String, Object[])
247: */
248: public static String format(String bundleName, Locale locale,
249: String key, Object arg1, Object arg2) {
250: return getService().format(bundleName, locale, key, arg1, arg2);
251: }
252:
253: /**
254: * Formats a localized value using the provided objects.
255: *
256: * @param bundleName The bundle in which to look for the localizable text.
257: * @param locale The locale for which to format the text.
258: * @param key The identifier for the localized text to retrieve,
259: * @param args The objects to use as {0}, {1}, etc. when
260: * formatting the localized text.
261: * @return Formatted localized text.
262: */
263: public static String format(String bundleName, Locale locale,
264: String key, Object[] args) {
265: return getService().format(bundleName, locale, key, args);
266: }
267:
268: /**
269: * Gets the <code>LocalizationService</code> implementation.
270: *
271: * @return the LocalizationService implementation.
272: */
273: protected static final LocalizationService getService() {
274: return (LocalizationService) TurbineServices.getInstance()
275: .getService(LocalizationService.SERVICE_NAME);
276: }
277:
278: /**
279: * @deprecated Call getString(key, data.getRequest()) instead.
280: */
281: public static String getString(RunData data, String key) {
282: return getString(key, data.getRequest());
283: }
284:
285: /**
286: * @deprecated Call getBundle(bundleName, data.getRequest()) instead.
287: */
288: public static ResourceBundle getBundle(String bundleName,
289: RunData data) {
290: return getBundle(bundleName, data.getRequest());
291: }
292: }
|