001: package org.apache.fulcrum.localization;
002:
003: /* ====================================================================
004: * The Apache Software License, Version 1.1
005: *
006: * Copyright (c) 2001 The Apache Software Foundation. All rights
007: * reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution,
022: * if any, must include the following acknowledgment:
023: * "This product includes software developed by the
024: * Apache Software Foundation (http://www.apache.org/)."
025: * Alternately, this acknowledgment may appear in the software itself,
026: * if and wherever such third-party acknowledgments normally appear.
027: *
028: * 4. The names "Apache" and "Apache Software Foundation" and
029: * "Apache Turbine" must not be used to endorse or promote products
030: * derived from this software without prior written permission. For
031: * written permission, please contact apache@apache.org.
032: *
033: * 5. Products derived from this software may not be called "Apache",
034: * "Apache Turbine", nor may "Apache" appear in their name, without
035: * prior written permission of the Apache Software Foundation.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
041: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
042: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
043: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
044: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
045: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
046: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
047: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
048: * SUCH DAMAGE.
049: * ====================================================================
050: *
051: * This software consists of voluntary contributions made by many
052: * individuals on behalf of the Apache Software Foundation. For more
053: * information on the Apache Software Foundation, please see
054: * <http://www.apache.org/>.
055: */
056:
057: import java.util.Locale;
058: import java.util.ResourceBundle;
059:
060: import javax.servlet.http.HttpServletRequest;
061: import org.tigris.scarab.services.ServiceManager;
062:
063: /**
064: * <p>Wrapper around the TurbineLocalization Service that makes it easy
065: * to grab something from the service.</p>
066: *
067: * <p>Instead of typing:
068: *
069: * <blockquote><code><pre>
070: * ((LocalizationService)TurbineServices.getInstance()
071: * .getService(LocalizationService.SERVICE_NAME))
072: * .getBundle(data)
073: * .getString(key)
074: * </pre></code></blockquote>
075: *
076: * You need only type:
077: *
078: * <blockquote><code><pre>
079: * Localization.getString(key)
080: * </pre></code></blockquote>
081: * </p>
082: *
083: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
084: * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
085: * @author <a href="mailto:leonardr@collab.net">Leonard Richardson</a>
086: * @version $Id: Localization.java 10193 2006-06-30 12:49:42Z dabbous $
087: */
088: public abstract class Localization {
089: private static LocalizationService localizationService;
090:
091: /**
092: * Pulls a string out of the LocalizationService with the default
093: * locale values of what is defined in the
094: * TurbineResources.properties file for the
095: * locale.default.language and locale.default.country property
096: * values. If those cannot be found, then the JVM defaults are
097: * used.
098: *
099: * @param key Name of string.
100: * @return A localized String.
101: */
102: public static String getString(String key) {
103: return getService().getBundle().getString(key);
104: }
105:
106: /**
107: * @see LocalizationService#getString(String, Locale, String)
108: */
109: public static String getString(Locale locale, String key) {
110: return getService().getString(null, locale, key);
111: }
112:
113: /**
114: * Fetches the localized text from the specified bundle, ignoring
115: * any default bundles.
116: *
117: * @see LocalizationService#getString(String, Locale, String)
118: */
119: public static String getString(String bundleName, Locale locale,
120: String key) {
121: return getService().getString(bundleName, locale, key);
122: }
123:
124: /**
125: * Convenience method that pulls a localized string off the
126: * LocalizationService using the ResourceBundle based on HTTP
127: * Accept-Language header in HttpServletRequest.
128: *
129: * @param req The HTTP request to parse the <code>Accept-Language</code> of.
130: * @param key Name of string.
131: * @return A localized string.
132: */
133: public static String getString(HttpServletRequest req, String key) {
134: return getService().getBundle(req).getString(key);
135: }
136:
137: /**
138: * Convenience method that pulls a localized string off the
139: * LocalizationService using the default ResourceBundle name
140: * defined in the TurbineResources.properties file and the
141: * specified language name in ISO format.
142: *
143: * @param key Name of string.
144: * @param lang Desired language for the localized string.
145: * @return A localized string.
146: */
147: public static String getString(String key, String lang) {
148: return getBundle(getDefaultBundleName(), new Locale(lang, ""))
149: .getString(key);
150: }
151:
152: /**
153: * Convenience method to get a ResourceBundle based on name.
154: *
155: * @param bundleName Name of bundle.
156: * @return A localized ResourceBundle.
157: */
158: public static ResourceBundle getBundle(String bundleName) {
159: return getService().getBundle(bundleName);
160: }
161:
162: /**
163: * Convenience method to get a ResourceBundle based on name and
164: * HTTP Accept-Language header.
165: *
166: * @param bundleName Name of bundle.
167: * @param languageHeader A String with the language header.
168: * @return A localized ResourceBundle.
169: */
170: public static ResourceBundle getBundle(String bundleName,
171: String languageHeader) {
172: return getService().getBundle(bundleName, languageHeader);
173: }
174:
175: /**
176: * Convenience method to get a ResourceBundle based on name and
177: * HTTP Accept-Language header in HttpServletRequest.
178: *
179: * @param req HttpServletRequest.
180: * @return A localized ResourceBundle.
181: */
182: public static ResourceBundle getBundle(HttpServletRequest req) {
183: return getService().getBundle(req);
184: }
185:
186: /**
187: * Convenience method to get a ResourceBundle based on name and
188: * HTTP Accept-Language header in HttpServletRequest.
189: *
190: * @param bundleName Name of bundle.
191: * @param req HttpServletRequest.
192: * @return A localized ResourceBundle.
193: */
194: public static ResourceBundle getBundle(String bundleName,
195: HttpServletRequest req) {
196: return getService().getBundle(bundleName, req);
197: }
198:
199: /**
200: * Convenience method to get a ResourceBundle based on name and
201: * Locale.
202: *
203: * @param bundleName Name of bundle.
204: * @param locale A Locale.
205: * @return A localized ResourceBundle.
206: */
207: public static ResourceBundle getBundle(String bundleName,
208: Locale locale) {
209: return getService().getBundle(bundleName, locale);
210: }
211:
212: /**
213: * This method sets the name of the default bundle.
214: *
215: * @param defaultBundle Name of default bundle.
216: * @see LocalizationService#setBundle(String)
217: */
218: public static void setBundle(String defaultBundle) {
219: getService().setBundle(defaultBundle);
220: }
221:
222: /**
223: * @see LocalizationService#getLocale(HttpServletRequest)
224: */
225: public static Locale getLocale(HttpServletRequest req) {
226: return getService().getLocale(req);
227: }
228:
229: /**
230: * This method parses the <code>Accept-Language</code> header and
231: * attempts to create a Locale out of it.
232: *
233: * @param languageHeader A String with the language header.
234: * @return A Locale.
235: */
236: public static Locale getLocale(String languageHeader) {
237: return getService().getLocale(languageHeader);
238: }
239:
240: /**
241: * @see LocalizationService#getDefaultBundleName()
242: */
243: public static String getDefaultBundleName() {
244: return getService().getDefaultBundleName();
245: }
246:
247: /**
248: * @see LocalizationService#getDefaultCountry()
249: */
250: public static String getDefaultCountry() {
251: return getService().getDefaultCountry();
252: }
253:
254: /**
255: * @see LocalizationService#getDefaultLanguage()
256: */
257: public static String getDefaultLanguage() {
258: return getService().getDefaultLanguage();
259: }
260:
261: /**
262: * Gets the <code>LocalizationService</code> implementation.
263: *
264: * @return the LocalizationService implementation.
265: */
266: protected static final LocalizationService getService() {
267: if (localizationService == null) {
268: ServiceManager sm = ServiceManager.getInstance();
269: localizationService = (LocalizationService) sm
270: .lookup(LocalizationService.class);
271: }
272: return localizationService;
273: }
274:
275: public static void setLocalizationService(
276: LocalizationService service) {
277: localizationService = service;
278: }
279:
280: /**
281: * @see LocalizationService#format(String, Locale, String, Object)
282: */
283: public static String format(String bundleName, Locale locale,
284: String key, Object arg1) {
285: return getService().format(bundleName, locale, key, arg1);
286: }
287:
288: /**
289: * @see LocalizationService#format(String, Locale, String, Object, Object)
290: */
291: public static String format(String bundleName, Locale locale,
292: String key, Object arg1, Object arg2) {
293: return getService().format(bundleName, locale, key, arg1, arg2);
294: }
295:
296: /**
297: * @see LocalizationService#format(String, Locale, String, Object[])
298: */
299: public static String format(String bundleName, Locale locale,
300: String key, Object[] args) {
301: return getService().format(bundleName, locale, key, args);
302: }
303:
304: // ---- Deprecated method(s) -------------------------------------------
305:
306: /**
307: * @deprecated Use getString(Locale, String) instead.
308: */
309: public static String getString(String key, Locale locale) {
310: return getString(locale, key);
311: }
312:
313: /**
314: * @deprecated Use getString(HttpServletRequest, String) instead.
315: */
316: public static String getString(String key, HttpServletRequest req) {
317: return getString(req, key);
318: }
319: }
|