All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.fulcrum.localization.DefaultLocalizationService Maven / Gradle / Ivy

The newest version!
package org.apache.fulcrum.localization;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.Locale;
import java.util.ResourceBundle;

import jakarta.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

/**
 * 

* This class is the single point of access to all localization * resources. It caches different ResourceBundles for different * Locales. *

* *

Usage example:

* *
 * 
 * LocalizationService ls = (LocalizationService) TurbineServices
 *     .getInstance().getService(LocalizationService.SERVICE_NAME);
 * 
 * 
* *

* Then call {@link #getString(String, Locale, String)}, or one of * four methods to retrieve a ResourceBundle: *

* *
    *
  • getBundle("MyBundleName")
  • *
  • getBundle("MyBundleName", httpAcceptLanguageHeader)
  • *
  • etBundle("MyBundleName", HttpServletRequest)
  • *
  • getBundle("MyBundleName", Locale)
  • *
  • etc.
  • *
* * @author Jonas Maurus * @author Jon S. Stevens * @author Frank Y. Kim * @author Daniel Rall * @author Leonard Richardson * @author Stephen McConnell * @author Thomas Vandahl * @version $Id$ * avalon.component name="localization" lifestyle="singleton" * avalon.service type="org.apache.fulcrum.localization.LocalizationService" */ public class DefaultLocalizationService extends SimpleLocalizationServiceImpl implements LocalizationService { /** * Creates a new instance. */ public DefaultLocalizationService() { super(); } /** * This method returns a ResourceBundle given the bundle name and * the Locale information supplied in the HTTP "Accept-Language" * header. * * @see org.apache.fulcrum.localization.LocalizationService#getBundle(java.lang.String, java.lang.String) * * @param bundleName Name of bundle. * @param languageHeader A String with the language header. * @return A localized ResourceBundle. */ public ResourceBundle getBundle(String bundleName, String languageHeader) { return getBundle(bundleName, getLocale(languageHeader)); } /** * This method returns a ResourceBundle given the Locale * information supplied in the HTTP "Accept-Language" header which * is stored in HttpServletRequest. * * @param req HttpServletRequest. * @return A localized ResourceBundle. */ public ResourceBundle getBundle(HttpServletRequest req) { return getBundle(getDefaultBundleName(), getLocale(req)); } /** * @see org.apache.fulcrum.localization.LocalizationService#getBundle(java.lang.String, jakarta.servlet.http.HttpServletRequest) * * This method returns a ResourceBundle given the bundle name and * the Locale information supplied in the HTTP "Accept-Language" * header which is stored in HttpServletRequest. * * @param bundleName Name of the bundle to use if the request's * locale cannot be resolved. * @param req HttpServletRequest. * @return A localized ResourceBundle. */ public ResourceBundle getBundle(String bundleName, HttpServletRequest req) { return getBundle(bundleName, getLocale(req)); } /* (non-Javadoc) * @see org.apache.fulcrum.localization.LocalizationService#getLocale(jakarta.servlet.http.HttpServletRequest) */ public Locale getLocale(HttpServletRequest req) { return getLocale(req.getHeader(ACCEPT_LANGUAGE)); // (JSS) Backed out this change because Tomcat seems to be returning // the wrong result and things just are not working. // Locale l = req.getLocale(); // return (l != null ? l : getLocale(req.getHeader(ACCEPT_LANGUAGE))); } /* (non-Javadoc) * @see org.apache.fulcrum.localization.LocalizationService#getLocale(java.lang.String) */ public Locale getLocale(String header) { if (StringUtils.isNotEmpty(header)) { LocaleTokenizer tok = new LocaleTokenizer(header); if (tok.hasNext()) { return (Locale) tok.next(); } } // Couldn't parse locale. return getDefaultLocale(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy