001 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020 /* 021 * $Id: LocaleUtility.java 468655 2006-10-28 07:12:06Z minchau $ 022 */ 023 024 package org.apache.xml.utils; 025 026 import java.util.Locale; 027 028 /** 029 * @author Igor Hersht, igorh@ca.ibm.com 030 */ 031 public class LocaleUtility { 032 /** 033 * IETF RFC 1766 tag separator 034 */ 035 public final static char IETF_SEPARATOR = '-'; 036 public final static String EMPTY_STRING = ""; 037 038 039 public static Locale langToLocale(String lang) { 040 if((lang == null) || lang.equals(EMPTY_STRING)){ // not specified => getDefault 041 return Locale.getDefault(); 042 } 043 String language = EMPTY_STRING; 044 String country = EMPTY_STRING; 045 String variant = EMPTY_STRING; 046 047 int i1 = lang.indexOf(IETF_SEPARATOR); 048 if (i1 < 0) { 049 language = lang; 050 } else { 051 language = lang.substring(0, i1); 052 ++i1; 053 int i2 = lang.indexOf(IETF_SEPARATOR, i1); 054 if (i2 < 0) { 055 country = lang.substring(i1); 056 } else { 057 country = lang.substring(i1, i2); 058 variant = lang.substring(i2+1); 059 } 060 } 061 062 if(language.length() == 2){ 063 language = language.toLowerCase(); 064 }else { 065 language = EMPTY_STRING; 066 } 067 068 if(country.length() == 2){ 069 country = country.toUpperCase(); 070 }else { 071 country = EMPTY_STRING; 072 } 073 074 if((variant.length() > 0) && 075 ((language.length() == 2) ||(country.length() == 2))){ 076 variant = variant.toUpperCase(); 077 }else{ 078 variant = EMPTY_STRING; 079 } 080 081 return new Locale(language, country, variant ); 082 } 083 084 085 086 } 087 088