1 2 3 18 21 22 package org.apache.xml.utils; 23 24 import java.util.Locale ; 25 26 29 public class LocaleUtility { 30 33 public final static char IETF_SEPARATOR = '-'; 34 public final static String EMPTY_STRING = ""; 35 36 37 public static Locale langToLocale(String lang) { 38 if((lang == null) || lang.equals(EMPTY_STRING)){ return Locale.getDefault(); 40 } 41 String language = EMPTY_STRING; 42 String country = EMPTY_STRING; 43 String 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 (language, country, variant ); 80 } 81 82 83 84 } 85 86 87 | Popular Tags |