1 16 package org.outerj.daisy.repository; 17 18 import java.util.Locale ; 19 import java.util.StringTokenizer ; 20 21 24 public class LocaleHelper { 25 public static Locale parseLocale(String localeString) { 26 StringTokenizer localeParser = new StringTokenizer (localeString, "-_"); 27 28 String lang = null, country = null, variant = null; 29 30 if (localeParser.hasMoreTokens()) 31 lang = localeParser.nextToken(); 32 if (localeParser.hasMoreTokens()) 33 country = localeParser.nextToken(); 34 if (localeParser.hasMoreTokens()) 35 variant = localeParser.nextToken(); 36 37 if (lang != null && country != null && variant != null) 38 return new Locale (lang, country, variant); 39 else if (lang != null && country != null) 40 return new Locale (lang, country); 41 else if (lang != null) 42 return new Locale (lang); 43 else 44 return new Locale (""); 45 } 46 47 public static String getString(Locale locale) { 48 boolean hasLanguage = !locale.getLanguage().equals(""); 49 boolean hasCountry = !locale.getCountry().equals(""); 50 boolean hasVariant = !locale.getVariant().equals(""); 51 52 if (hasLanguage && hasCountry && hasVariant) 53 return locale.getLanguage() + '-' + locale.getCountry() + '-' + locale.getVariant(); 54 else if (hasLanguage && hasCountry) 55 return locale.getLanguage() + '-' + locale.getCountry(); 56 else if (hasLanguage) 57 return locale.getLanguage(); 58 else 59 return ""; 60 } 61 } 62 | Popular Tags |