KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xml > utils > LocaleUtility


1
2
3 /*
4  * Copyright 1999-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  * $Id: LocaleUtility.java,v 1.2 2004/02/17 04:21:14 minchau Exp $
20  */

21
22 package org.apache.xml.utils;
23
24 import java.util.Locale JavaDoc;
25
26 /**
27  * @author Igor Hersht, igorh@ca.ibm.com
28  */

29 public class LocaleUtility {
30     /**
31      * IETF RFC 1766 tag separator
32      */

33     public final static char IETF_SEPARATOR = '-';
34     public final static String JavaDoc EMPTY_STRING = "";
35     
36    
37  public static Locale JavaDoc langToLocale(String JavaDoc lang) {
38        if((lang == null) || lang.equals(EMPTY_STRING)){ // not specified => getDefault
39
return Locale.getDefault();
40        }
41         String JavaDoc language = EMPTY_STRING;
42         String JavaDoc country = EMPTY_STRING;
43         String JavaDoc variant = EMPTY_STRING;
44
45         int i1 = lang.indexOf(IETF_SEPARATOR);
46         if (i1 < 0) {
47             language = lang;
48         } else {
49             language = lang.substring(0, i1);
50             ++i1;
51             int i2 = lang.indexOf(IETF_SEPARATOR, i1);
52             if (i2 < 0) {
53                 country = lang.substring(i1);
54             } else {
55                 country = lang.substring(i1, i2);
56                 variant = lang.substring(i2+1);
57             }
58         }
59         
60         if(language.length() == 2){
61            language = language.toLowerCase();
62         }else {
63           language = EMPTY_STRING;
64         }
65         
66         if(country.length() == 2){
67            country = country.toUpperCase();
68         }else {
69           country = EMPTY_STRING;
70         }
71         
72         if((variant.length() > 0) &&
73         ((language.length() == 2) ||(country.length() == 2))){
74            variant = variant.toUpperCase();
75         }else{
76             variant = EMPTY_STRING;
77         }
78              
79         return new Locale JavaDoc(language, country, variant );
80     }
81     
82   
83    
84  }
85   
86
87
Popular Tags