01:
02: /*
03: * Copyright 1999-2004 The Apache Software Foundation.
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /*
18: * $Id: LocaleUtility.java,v 1.2 2004/02/17 04:21:14 minchau Exp $
19: */
20:
21: package org.apache.xml.utils;
22:
23: import java.util.Locale;
24:
25: /**
26: * @author Igor Hersht, igorh@ca.ibm.com
27: */
28: public class LocaleUtility {
29: /**
30: * IETF RFC 1766 tag separator
31: */
32: public final static char IETF_SEPARATOR = '-';
33: public final static String EMPTY_STRING = "";
34:
35: public static Locale langToLocale(String lang) {
36: if ((lang == null) || lang.equals(EMPTY_STRING)) { // not specified => getDefault
37: return Locale.getDefault();
38: }
39: String language = EMPTY_STRING;
40: String country = EMPTY_STRING;
41: String variant = EMPTY_STRING;
42:
43: int i1 = lang.indexOf(IETF_SEPARATOR);
44: if (i1 < 0) {
45: language = lang;
46: } else {
47: language = lang.substring(0, i1);
48: ++i1;
49: int i2 = lang.indexOf(IETF_SEPARATOR, i1);
50: if (i2 < 0) {
51: country = lang.substring(i1);
52: } else {
53: country = lang.substring(i1, i2);
54: variant = lang.substring(i2 + 1);
55: }
56: }
57:
58: if (language.length() == 2) {
59: language = language.toLowerCase();
60: } else {
61: language = EMPTY_STRING;
62: }
63:
64: if (country.length() == 2) {
65: country = country.toUpperCase();
66: } else {
67: country = EMPTY_STRING;
68: }
69:
70: if ((variant.length() > 0)
71: && ((language.length() == 2) || (country.length() == 2))) {
72: variant = variant.toUpperCase();
73: } else {
74: variant = EMPTY_STRING;
75: }
76:
77: return new Locale(language, country, variant);
78: }
79:
80: }
|