1 package org.jahia.utils; 2 3 import java.util.Locale ; 4 import java.util.StringTokenizer ; 5 import java.text.Collator ; 6 import java.util.Comparator ; 7 import java.util.ArrayList ; 8 import java.util.Collections ; 9 10 21 22 public class LanguageCodeConverters { 23 24 35 public static Locale languageCodeToLocale(String languageCode) { 36 if ( languageCode == null ){ 37 return null; 38 } 39 StringTokenizer codeTokens = new StringTokenizer (languageCode,"_"); 40 String language = ""; 41 String country = ""; 42 String variant = ""; 43 44 if (codeTokens.hasMoreTokens()) { 45 language = codeTokens.nextToken(); 46 } 47 if (codeTokens.hasMoreTokens()) { 48 country = codeTokens.nextToken(); 49 } 50 if (codeTokens.hasMoreTokens()) { 51 variant = codeTokens.nextToken(); 52 } 53 54 return newLocale(language, country, variant); 55 } 56 57 67 public static Locale languageTagsToLocale(String languageCode) { 68 StringTokenizer codeTokens = new StringTokenizer (languageCode,"-"); 69 String language = ""; 70 String country = ""; 71 String variant = ""; 72 73 String primaryTag = ""; 74 String secondSubTag = ""; 75 String thirdSubTag = ""; 76 77 if (codeTokens.hasMoreTokens()) { 78 primaryTag = codeTokens.nextToken(); 79 } 80 if (codeTokens.hasMoreTokens()) { 81 secondSubTag = codeTokens.nextToken(); 82 } 83 if (codeTokens.hasMoreTokens()) { 84 thirdSubTag = codeTokens.nextToken(); 85 } 86 87 90 if (primaryTag.length() != 2) { 93 96 100 101 return null; 102 } 103 language = primaryTag; 104 105 if (secondSubTag.length() == 2) { 108 country = secondSubTag; 109 variant = thirdSubTag; 110 } else { 111 variant = secondSubTag; 112 } 113 114 return newLocale(language, country, variant); 115 } 116 117 129 public static String localeToLanguageTag(Locale locale) { 130 StringBuffer result = new StringBuffer (); 131 if (!("".equals(locale.getLanguage()))) { 132 result.append(locale.getLanguage()); 133 } else { 134 return null; 137 } 138 if (!("".equals(locale.getCountry()))) { 139 result.append("-"); 140 result.append(locale.getCountry()); 141 } 142 if (!("".equals(locale.getVariant()))) { 143 result.append("-"); 144 result.append(locale.getVariant()); 145 } 146 147 return result.toString(); 148 } 149 150 164 private static Locale newLocale(String language, String country, String variant) { 165 if ("".equals(variant)) { 166 if ("".equals(country)) { 167 if ("".equals(language)) { 168 return Locale.getDefault(); 169 } else { 170 return new Locale (language, ""); 171 } 172 } else { 173 return new Locale (language, country); 174 } 175 } else { 176 return new Locale (language, country, variant); 177 } 178 } 179 180 public static ArrayList getSortedLocaleList(Locale currentLocale) { 181 Locale [] availableLocales = Locale.getAvailableLocales(); 182 ArrayList sortedLocaleList = new ArrayList (); 183 for (int i=0; i < availableLocales.length; i++) { 184 Locale curLocale = availableLocales[i]; 185 sortedLocaleList.add(curLocale); 186 } 187 Collections.sort(sortedLocaleList, LanguageCodeConverters.getLocaleDisplayNameComparator(currentLocale)); 188 return sortedLocaleList; 189 } 190 191 195 public static class LocaleDisplayNameComparator implements Comparator { 196 197 private Collator collator = Collator.getInstance(); 198 private Locale currentLocale; 199 200 public LocaleDisplayNameComparator(Locale locale) { 201 if (locale != null) { 202 this.currentLocale = locale; 203 collator = Collator.getInstance(locale); 204 } 205 } 206 207 public int compare(Object o1, 208 Object o2) { 209 Locale locale1 = (Locale ) o1; 210 Locale locale2 = (Locale ) o2; 211 return collator.compare(locale1.getDisplayName(currentLocale), locale2.getDisplayName(currentLocale)); 212 } 213 214 public boolean equals(Object obj) { 215 if (obj instanceof LocaleDisplayNameComparator) { 216 return true; 217 } else { 218 return false; 219 } 220 } 221 } 222 223 public static LocaleDisplayNameComparator getLocaleDisplayNameComparator(Locale locale) { 224 return new LocaleDisplayNameComparator(locale); 225 } 226 227 } | Popular Tags |