KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > util > LocaleUtils


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

16 package org.apache.myfaces.util;
17
18 import java.util.Locale JavaDoc;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23
24 /**
25  * @author Anton Koinov (latest modification by $Author$)
26  * @version $Revision$ $Date$
27  * $Log$
28  * Revision 1.2 2004/10/13 11:51:01 matze
29  * renamed packages to org.apache
30  *
31  * Revision 1.1 2004/08/23 05:13:39 dave0000
32  * Externalize String-to-Locale conversion
33  *
34  */

35 public class LocaleUtils
36 {
37     private static final Log log = LogFactory.getLog(LocaleUtils.class);
38    
39     /** Utility class, do not instatiate */
40     private LocaleUtils()
41     {
42         // utility class, do not instantiate
43
}
44     
45     /**
46      * Converts a locale string to <code>Locale</code> class. Accepts both
47      * '_' and '-' as separators for locale components.
48      *
49      * @param localeString string representation of a locale
50      * @return Locale instance, compatible with the string representation
51      */

52     public static Locale JavaDoc toLocale(String JavaDoc localeString)
53     {
54         if ((localeString == null) || (localeString.length() == 0))
55         {
56             log.error("Locale name null or empty, ignoring");
57             return Locale.getDefault();
58         }
59
60         int separatorCountry = localeString.indexOf('_');
61         char separator;
62         if (separatorCountry >= 0) {
63             separator = '_';
64         }
65         else
66         {
67             separatorCountry = localeString.indexOf('-');
68             separator = '-';
69         }
70         
71         String JavaDoc language, country, variant;
72         if (separatorCountry < 0)
73         {
74             language = localeString;
75             country = variant = "";
76         }
77         else
78         {
79             language = localeString.substring(0, separatorCountry);
80             
81             int separatorVariant = localeString.indexOf(separator, separatorCountry + 1);
82             if (separatorVariant < 0)
83             {
84                 country = localeString.substring(separatorCountry + 1);
85                 variant = "";
86             }
87             else
88             {
89                 country = localeString.substring(separatorCountry + 1, separatorVariant);
90                 variant = localeString.substring(separatorVariant + 1);
91             }
92         }
93         
94         return new Locale JavaDoc(language, country, variant);
95     }
96 }
97
Popular Tags