1 9 10 package com.ibm.icu.impl; 11 12 import java.util.Locale ; 13 14 17 public class LocaleUtility { 18 19 23 public static Locale getLocaleFromName(String name) { 24 String language = ""; 25 String country = ""; 26 String variant = ""; 27 28 int i1 = name.indexOf('_'); 29 if (i1 < 0) { 30 language = name; 31 } else { 32 language = name.substring(0, i1); 33 ++i1; 34 int i2 = name.indexOf('_', i1); 35 if (i2 < 0) { 36 country = name.substring(i1); 37 } else { 38 country = name.substring(i1, i2); 39 variant = name.substring(i2+1); 40 } 41 } 42 43 return new Locale (language, country, variant); 44 } 45 46 51 public static boolean isFallbackOf(String parent, String child) { 52 if (!child.startsWith(parent)) { 53 return false; 54 } 55 int i = parent.length(); 56 return (i == child.length() || 57 child.charAt(i) == '_'); 58 } 59 60 65 public static boolean isFallbackOf(Locale parent, Locale child) { 66 return isFallbackOf(parent.toString(), child.toString()); 67 } 68 69 70 74 public static String canonicalLocaleString(Locale locale) { 75 return canonicalLocaleString(locale.toString()); 76 } 77 78 84 public static String canonicalLocaleString(String id) { 85 if (id != null) { 86 int x = id.indexOf("_"); 87 if (x == -1) { 88 id = id.toLowerCase(Locale.ENGLISH); 89 } else { 90 StringBuffer buf = new StringBuffer (); 91 buf.append(id.substring(0, x).toLowerCase(Locale.ENGLISH)); 92 buf.append(id.substring(x).toUpperCase(Locale.ENGLISH)); 93 94 int len = buf.length(); 95 int n = len; 96 while (--n >= 0 && buf.charAt(n) == '_') { 97 } 98 if (++n != len) { 99 buf.delete(n, len); 100 } 101 id = buf.toString(); 102 } 103 } 104 return id; 105 } 106 107 115 public static Locale fallback(Locale loc) { 116 117 String [] parts = new String [] 119 { loc.getLanguage(), loc.getCountry(), loc.getVariant() }; 120 int i; 121 for (i=2; i>=0; --i) { 122 if (parts[i].length() != 0) { 123 parts[i] = ""; 124 break; 125 } 126 } 127 if (i<0) { 128 return null; } 130 return new Locale (parts[0], parts[1], parts[2]); 131 } 132 } 133 | Popular Tags |