View Javadoc
1 package org.apache.turbine.services.localization; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import javax.servlet.http.HttpServletRequest; 58 import java.util.Locale; 59 import java.util.ResourceBundle; 60 import org.apache.turbine.services.Service; 61 import org.apache.turbine.services.ServiceBroker; 62 import org.apache.turbine.services.TurbineServices; 63 import org.apache.turbine.services.resources.TurbineResources; 64 import org.apache.turbine.util.RunData; 65 66 /*** 67 * Wrapper around the TurbineLocalization Service that makes it easy 68 * to grab something from the service and make the code cleaner. 69 * 70 * <p> 71 * 72 * Instead of typing: 73 * 74 * <br> 75 * 76 * ((LocalizationService)TurbineServices.getInstance()<br> 77 * .getService(LocalizationService.SERVICE_NAME))<br> 78 * .getBundle(data)<br> 79 * .getString(str)<br> 80 * 81 * Now you only need to type: 82 * 83 * <br> 84 * 85 * Localization.getString(str) 86 * 87 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> 88 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a> 89 * @version $Id: Localization.java,v 1.6 2002/03/08 18:00:24 dlr Exp $ 90 */ 91 public abstract class Localization 92 { 93 /*** 94 * Fetches the localized text from the specified bundle, ignoring 95 * any default bundles. 96 * 97 * @see LocalizationService#getString(String, Locale, String) 98 */ 99 public static String getString(String bundleName, Locale locale, 100 String key) 101 { 102 return getService().getString(bundleName, locale, key); 103 } 104 105 /*** 106 * Pulls a string out of the LocalizationService with the default 107 * locale values of what is defined in the 108 * TurbineResources.properties file for the 109 * locale.default.language and locale.default.country property 110 * values. If those cannot be found, then the JVM defaults are 111 * used. 112 * 113 * @param key Name of string. 114 * @return A localized String. 115 */ 116 public static String getString(String key) 117 { 118 return getService().getString(null, null, key); 119 } 120 121 /*** 122 * @param key Name of the text to retrieve. 123 * @param locale Locale to get text for. 124 * @return Localized text. 125 */ 126 public static String getString(String key, Locale locale) 127 { 128 return getService().getString(null, locale, key); 129 } 130 131 /*** 132 * Pulls a string out of the LocalizationService and attempts to 133 * determine the Locale by the Accept-Language header. If that 134 * header is not present, it will fall back to using the locale 135 * values of what is defined in the TurbineResources.properties 136 * file for the locale.default.language and locale.default.country 137 * property values. If those cannot be found, then the JVM 138 * defaults are used. 139 * 140 * @param req HttpServletRequest information. 141 * @param key Name of string. 142 * @return A localized String. 143 */ 144 public static String getString(String key, HttpServletRequest req) 145 { 146 return getService().getString(null, getLocale(req), key); 147 } 148 149 /*** 150 * Convenience method that pulls a localized string off the 151 * LocalizationService using the default ResourceBundle name 152 * defined in the TurbineResources.properties file and the 153 * specified language name in ISO format. 154 * 155 * @param key Name of string. 156 * @param lang Desired language for the localized string. 157 * @return A localized string. 158 */ 159 public static String getString(String key, String lang) 160 { 161 return getString(getDefaultBundleName(), new Locale(lang, ""),key); 162 } 163 164 /*** 165 * Convenience method to get a ResourceBundle based on name. 166 * 167 * @param bundleName Name of bundle. 168 * @return A localized ResourceBundle. 169 */ 170 public static ResourceBundle getBundle(String bundleName) 171 { 172 return getService().getBundle(bundleName); 173 } 174 175 /*** 176 * Convenience method to get a ResourceBundle based on name and 177 * HTTP Accept-Language header. 178 * 179 * @param bundleName Name of bundle. 180 * @param languageHeader A String with the language header. 181 * @return A localized ResourceBundle. 182 */ 183 public static ResourceBundle getBundle(String bundleName, 184 String languageHeader) 185 { 186 return getService().getBundle(bundleName, languageHeader); 187 } 188 189 /*** 190 * Convenience method to get a ResourceBundle based on name and 191 * HTTP Accept-Language header in HttpServletRequest. 192 * 193 * @param req HttpServletRequest. 194 * @return A localized ResourceBundle. 195 */ 196 public static ResourceBundle getBundle(HttpServletRequest req) 197 { 198 return getService().getBundle(req); 199 } 200 201 /*** 202 * Convenience method to get a ResourceBundle based on name and 203 * HTTP Accept-Language header in HttpServletRequest. 204 * 205 * @param bundleName Name of bundle. 206 * @param req HttpServletRequest. 207 * @return A localized ResourceBundle. 208 */ 209 public static ResourceBundle getBundle(String bundleName, 210 HttpServletRequest req) 211 { 212 return getService().getBundle(bundleName, req); 213 } 214 215 /*** 216 * Convenience method to get a ResourceBundle based on name and 217 * Locale. 218 * 219 * @param bundleName Name of bundle. 220 * @param locale A Locale. 221 * @return A localized ResourceBundle. 222 */ 223 public static ResourceBundle getBundle(String bundleName, Locale locale) 224 { 225 return getService().getBundle(bundleName, locale); 226 } 227 228 /*** 229 * This method sets the name of the default bundle. 230 * 231 * @param defaultBundle Name of default bundle. 232 */ 233 public static void setBundle(String defaultBundle) 234 { 235 getService().setBundle(defaultBundle); 236 } 237 238 /*** 239 * Attempts to pull the <code>Accept-Language</code> header out of 240 * the HttpServletRequest object and then parse it. If the header 241 * is not present, it will return a null Locale. 242 * 243 * @param req HttpServletRequest. 244 * @return A Locale. 245 */ 246 public static Locale getLocale(HttpServletRequest req) 247 { 248 return getService().getLocale(req); 249 } 250 251 /*** 252 * This method parses the <code>Accept-Language</code> header and 253 * attempts to create a Locale out of it. 254 * 255 * @param languageHeader A String with the language header. 256 * @return A Locale. 257 */ 258 public static Locale getLocale(String languageHeader) 259 { 260 return getService().getLocale(languageHeader); 261 } 262 263 /*** 264 * @see org.apache.turbine.services.localization.LocalizationService#getDefaultBundle() 265 */ 266 public static String getDefaultBundleName() 267 { 268 return getService().getDefaultBundleName(); 269 } 270 271 /*** 272 * Gets the <code>LocalizationService</code> implementation. 273 * 274 * @return the LocalizationService implementation. 275 */ 276 protected static final LocalizationService getService() 277 { 278 return (LocalizationService) TurbineServices.getInstance() 279 .getService(LocalizationService.SERVICE_NAME); 280 } 281 282 /*** 283 * @deprecated Call getString(data.getRequest()) instead. 284 */ 285 public static String getString(RunData data, String key) 286 { 287 return getBundle(data.getRequest()).getString(key); 288 } 289 290 /*** 291 * @deprecated Call getBundle(bundleName, data.getRequest()) instead. 292 */ 293 public static ResourceBundle getBundle(String bundleName, RunData data) 294 { 295 return getBundle(bundleName, data.getRequest()); 296 } 297 }

This page was automatically generated by Maven