KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > localization > LocaleDetector


1 package org.apache.turbine.services.localization;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.Locale JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21
22 import org.apache.commons.configuration.Configuration;
23
24 import org.apache.turbine.Turbine;
25 import org.apache.turbine.util.RunData;
26
27 /**
28  * This class returns a Locale object based on the HTTP
29  * Accept-Language header.
30  *
31  * This class is based on examples from Jason Hunter's book <i>Java
32  * Servlet Programming</i>.
33  *
34  * @author <a HREF="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
35  * @author <a HREF="mailto:jon@collab.net">Jon S. Stevens</a>
36  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37  * @version $Id: LocaleDetector.java,v 1.7.2.2 2004/05/20 03:06:51 seade Exp $
38  * @deprecated Use LocaleTokenizer instead.
39  */

40 public class LocaleDetector
41 {
42     /**
43      * Attempts to pull the "Accept-Language" header out of the
44      * HttpServletRequest object and then parse it. If the header is
45      * not present, it will return a null Locale.
46      *
47      * @param data Turbine information.
48      * @return A Locale.
49      */

50     public static Locale JavaDoc getLocale(RunData data)
51     {
52         String JavaDoc header = data.getRequest().getHeader("Accept-Language");
53         if (header == null || header.length() == 0)
54             return null;
55         return getLocale(header);
56     }
57
58     /**
59      * This method parses the Accept-Language header and attempts to
60      * create a Locale out of it.
61      *
62      * @param languageHeader A String with the language header.
63      * @return A Locale.
64      */

65     public static Locale JavaDoc getLocale(String JavaDoc languageHeader)
66     {
67         Configuration conf = Turbine.getConfiguration();
68
69
70         // return a "default" locale
71
if (languageHeader == null ||
72                 languageHeader.trim().equals(""))
73         {
74             return new Locale JavaDoc(
75                     conf.getString("locale.default.language", "en"),
76                     conf.getString("locale.default.country", "US"));
77         }
78
79         // The HTTP Accept-Header is something like
80
//
81
// "en, es;q=0.8, zh-TW;q=0.1"
82
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(languageHeader, ",");
83
84         // while ( tokenizer.hasMoreTokens() )
85
// {
86
String JavaDoc language = tokenizer.nextToken();
87         // This should never be true but just in case
88
// if ( !language.trim().equals("") )
89
return getLocaleForLanguage(language.trim());
90         // }
91
}
92
93     /**
94      * This method creates a Locale from the language.
95      *
96      * @param language A String with the language.
97      * @return A Locale.
98      */

99     private static Locale JavaDoc getLocaleForLanguage(String JavaDoc language)
100     {
101         Locale JavaDoc locale;
102         int semi, dash;
103
104         // Cut off any q-value that comes after a semicolon.
105
if ((semi = language.indexOf(';')) != -1)
106         {
107             language = language.substring(0, semi);
108         }
109
110         language = language.trim();
111
112         // Create a Locale from the language. A dash may separate the
113
// language from the country.
114
if ((dash = language.indexOf('-')) == -1)
115         {
116             // No dash means no country.
117
locale = new Locale JavaDoc(language, "");
118         }
119         else
120         {
121             locale = new Locale JavaDoc(language.substring(0, dash),
122                     language.substring(dash + 1));
123         }
124
125         return locale;
126     }
127 }
128
Popular Tags