1 16 package org.apache.myfaces.util; 17 18 import java.util.Locale ; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 24 35 public class LocaleUtils 36 { 37 private static final Log log = LogFactory.getLog(LocaleUtils.class); 38 39 40 private LocaleUtils() 41 { 42 } 44 45 52 public static Locale toLocale(String localeString) 53 { 54 if ((localeString == null) || (localeString.length() == 0)) 55 { 56 log.error("Locale name null or empty, ignoring"); 57 return Locale.getDefault(); 58 } 59 60 int separatorCountry = localeString.indexOf('_'); 61 char separator; 62 if (separatorCountry >= 0) { 63 separator = '_'; 64 } 65 else 66 { 67 separatorCountry = localeString.indexOf('-'); 68 separator = '-'; 69 } 70 71 String language, country, variant; 72 if (separatorCountry < 0) 73 { 74 language = localeString; 75 country = variant = ""; 76 } 77 else 78 { 79 language = localeString.substring(0, separatorCountry); 80 81 int separatorVariant = localeString.indexOf(separator, separatorCountry + 1); 82 if (separatorVariant < 0) 83 { 84 country = localeString.substring(separatorCountry + 1); 85 variant = ""; 86 } 87 else 88 { 89 country = localeString.substring(separatorCountry + 1, separatorVariant); 90 variant = localeString.substring(separatorVariant + 1); 91 } 92 } 93 94 return new Locale (language, country, variant); 95 } 96 } 97 | Popular Tags |