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 "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
21
* $Id: LocaleUtility.java,v 1.2 2009/12/10 03:18:23 matthewoliver Exp $
24
package org.apache.xml.utils;
26
import java.util.Locale;
29
* @author Igor Hersht, igorh@ca.ibm.com
31
public class LocaleUtility {
33
* IETF RFC 1766 tag separator
35
public final static char IETF_SEPARATOR = '-';
36
public final static String EMPTY_STRING = "";
39
public static Locale langToLocale(String lang) {
40
if((lang == null) || lang.equals(EMPTY_STRING)){ // not specified => getDefault
41
return Locale.getDefault();
43
String language = EMPTY_STRING;
44
String country = EMPTY_STRING;
45
String variant = EMPTY_STRING;
47
int i1 = lang.indexOf(IETF_SEPARATOR);
51
language = lang.substring(0, i1);
53
int i2 = lang.indexOf(IETF_SEPARATOR, i1);
55
country = lang.substring(i1);
57
country = lang.substring(i1, i2);
58
variant = lang.substring(i2+1);
62
if(language.length() == 2){
63
language = language.toLowerCase();
65
language = EMPTY_STRING;
68
if(country.length() == 2){
69
country = country.toUpperCase();
71
country = EMPTY_STRING;
74
if((variant.length() > 0) &&
75
((language.length() == 2) ||(country.length() == 2))){
76
variant = variant.toUpperCase();
78
variant = EMPTY_STRING;
81
return new Locale(language, country, variant );