View Javadoc

1   package org.apache.turbine.services.localization;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Locale;
23  import java.util.StringTokenizer;
24  
25  import org.apache.commons.configuration.Configuration;
26  
27  import org.apache.turbine.Turbine;
28  import org.apache.turbine.util.RunData;
29  
30  /***
31   * This class returns a Locale object based on the HTTP
32   * Accept-Language header.
33   *
34   * This class is based on examples from Jason Hunter's book <i>Java
35   * Servlet Programming</i>.
36   *
37   * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
38   * @author <a href="mailto:jon@collab.net">Jon S. Stevens</a>
39   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
40   * @version $Id: LocaleDetector.java 534527 2007-05-02 16:10:59Z tv $
41   * @deprecated Use LocaleTokenizer instead.
42   */
43  public class LocaleDetector
44  {
45      /***
46       * Attempts to pull the "Accept-Language" header out of the
47       * HttpServletRequest object and then parse it.  If the header is
48       * not present, it will return a null Locale.
49       *
50       * @param data Turbine information.
51       * @return A Locale.
52       */
53      public static Locale getLocale(RunData data)
54      {
55          String header = data.getRequest().getHeader("Accept-Language");
56          if (header == null || header.length() == 0)
57              return null;
58          return getLocale(header);
59      }
60  
61      /***
62       * This method parses the Accept-Language header and attempts to
63       * create a Locale out of it.
64       *
65       * @param languageHeader A String with the language header.
66       * @return A Locale.
67       */
68      public static Locale getLocale(String languageHeader)
69      {
70          Configuration conf = Turbine.getConfiguration();
71  
72  
73          // return a "default" locale
74          if (languageHeader == null ||
75                  languageHeader.trim().equals(""))
76          {
77              return new Locale(
78                      conf.getString("locale.default.language", "en"),
79                      conf.getString("locale.default.country", "US"));
80          }
81  
82          // The HTTP Accept-Header is something like
83          //
84          // "en, es;q=0.8, zh-TW;q=0.1"
85          StringTokenizer tokenizer = new StringTokenizer(languageHeader, ",");
86  
87          // while ( tokenizer.hasMoreTokens() )
88          // {
89          String language = tokenizer.nextToken();
90          // This should never be true but just in case
91          // if ( !language.trim().equals("") )
92          return getLocaleForLanguage(language.trim());
93          // }
94      }
95  
96      /***
97       * This method creates a Locale from the language.
98       *
99       * @param language A String with the language.
100      * @return A Locale.
101      */
102     private static Locale getLocaleForLanguage(String language)
103     {
104         Locale locale;
105         int semi, dash;
106 
107         // Cut off any q-value that comes after a semicolon.
108         if ((semi = language.indexOf(';')) != -1)
109         {
110             language = language.substring(0, semi);
111         }
112 
113         language = language.trim();
114 
115         // Create a Locale from the language.  A dash may separate the
116         // language from the country.
117         if ((dash = language.indexOf('-')) == -1)
118         {
119             // No dash means no country.
120             locale = new Locale(language, "");
121         }
122         else
123         {
124             locale = new Locale(language.substring(0, dash),
125                     language.substring(dash + 1));
126         }
127 
128         return locale;
129     }
130 }