001    package org.apache.fulcrum.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.commons.lang.StringUtils;
028    
029    /**
030     * <p>This class is the single point of access to all localization
031     * resources.  It caches different ResourceBundles for different
032     * Locales.</p>
033     *
034     * <p>Usage example:</p>
035     *
036     * <blockquote><code><pre>
037     * LocalizationService ls = (LocalizationService) TurbineServices
038     *     .getInstance().getService(LocalizationService.SERVICE_NAME);
039     * </pre></code></blockquote>
040     *
041     * <p>Then call {@link #getString(String, Locale, String)}, or one of
042     * four methods to retrieve a ResourceBundle:
043     *
044     * <ul>
045     * <li>getBundle("MyBundleName")</li>
046     * <li>getBundle("MyBundleName", httpAcceptLanguageHeader)</li>
047     * <li>etBundle("MyBundleName", HttpServletRequest)</li>
048     * <li>getBundle("MyBundleName", Locale)</li>
049     * <li>etc.</li>
050     * </ul></p>
051     *
052     * @author <a href="mailto:jm@mediaphil.de">Jonas Maurus</a>
053     * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
054     * @author <a href="mailto:novalidemail@foo.com">Frank Y. Kim</a>
055     * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
056     * @author <a href="mailto:leonardr@collab.net">Leonard Richardson</a>
057     * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
058     * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
059     * @version $Id: DefaultLocalizationService.java 645885 2008-04-08 12:50:57Z tv $
060     * @avalon.component name="localization" lifestyle="singleton"
061     * @avalon.service type="org.apache.fulcrum.localization.LocalizationService"
062     */
063    public class DefaultLocalizationService
064        extends SimpleLocalizationServiceImpl
065        implements LocalizationService
066    {
067        /**
068         * Creates a new instance.
069         */
070        public DefaultLocalizationService()
071        {
072            super();
073        }
074    
075        /**
076         * This method returns a ResourceBundle given the bundle name and
077         * the Locale information supplied in the HTTP "Accept-Language"
078         * header.
079         *
080         * @param bundleName Name of bundle.
081         * @param languageHeader A String with the language header.
082         * @return A localized ResourceBundle.
083         */
084        public ResourceBundle getBundle(String bundleName, String languageHeader)
085        {
086            return getBundle(bundleName, getLocale(languageHeader));
087        }
088    
089        /**
090         * This method returns a ResourceBundle given the Locale
091         * information supplied in the HTTP "Accept-Language" header which
092         * is stored in HttpServletRequest.
093         *
094         * @param req HttpServletRequest.
095         * @return A localized ResourceBundle.
096         */
097        public ResourceBundle getBundle(HttpServletRequest req)
098        {
099            return getBundle(getDefaultBundleName(), getLocale(req));
100        }
101    
102        /**
103         * This method returns a ResourceBundle given the bundle name and
104         * the Locale information supplied in the HTTP "Accept-Language"
105         * header which is stored in HttpServletRequest.
106         *
107         * @param bundleName Name of the bundle to use if the request's
108         * locale cannot be resolved.
109         * @param req HttpServletRequest.
110         * @return A localized ResourceBundle.
111         */
112        public ResourceBundle getBundle(String bundleName, HttpServletRequest req)
113        {
114            return getBundle(bundleName, getLocale(req));
115        }
116    
117        /**
118         * @see org.apache.fulcrum.localization.LocalizationService#getLocale(HttpServletRequest)
119         */
120        public Locale getLocale(HttpServletRequest req)
121        {
122            return getLocale(req.getHeader(ACCEPT_LANGUAGE));
123            // (JSS) Backed out this change because Tomcat seems to be returning
124            //       the wrong result and things just are not working.
125            //        Locale l = req.getLocale();
126            //        return (l != null ? l : getLocale(req.getHeader(ACCEPT_LANGUAGE)));
127        }
128    
129        /**
130         * @see org.apache.fulcrum.localization.LocalizationService#getLocale(String)
131         */
132        public Locale getLocale(String header)
133        {
134            if (!StringUtils.isEmpty(header))
135            {
136                LocaleTokenizer tok = new LocaleTokenizer(header);
137                if (tok.hasNext())
138                {
139                    return (Locale) tok.next();
140                }
141            }
142            
143            // Couldn't parse locale.
144            return getDefaultLocale();
145        }
146    }