LocaleUtil { public static Locale parseLocale(String localeString) { localeString = StringUtil.trimToNull(localeString); if (localeString == null) { return null; } String language = Emptys.EMPTY_STRING; String country = Emptys.EMPTY_STRING; String variant = Emptys.EMPTY_STRING; int start = 0; int index = localeString.indexOf("_"); if (index >= 0) { language = localeString.substring(start, index).trim(); start = index + 1; index = localeString.indexOf("_", start); if (index >= 0) { country = localeString.substring(start, index).trim(); variant = localeString.substring(index + 1).trim(); } else { country = localeString.substring(start).trim(); } } else { language = localeString.substring(start).trim(); } return new Locale(language, country, variant); }  static boolean isLocaleSupported(Locale locale); static boolean isCharsetSupported(String charset); static Locale parseLocale(String localeString); static String getCanonicalCharset(String charset); static List<String> calculateBundleNames(String baseName, Locale locale); static List<String> calculateBundleNames(String baseName, Locale locale, boolean noext); static LocaleInfo getSystem(); static LocaleInfo getDefault(); static LocaleInfo setDefault(Locale locale); static LocaleInfo setDefault(Locale locale, String charset); static LocaleInfo setDefault(LocaleInfo localeInfo); static void resetDefault(); static LocaleInfo getContext(); static LocaleInfo setContext(Locale locale); static LocaleInfo setContext(Locale locale, String charset); static LocaleInfo setContext(LocaleInfo localeInfo); static void resetContext();  }
@Test public void parseLocale() { assertNull(LocaleUtil.parseLocale(null)); assertNull(LocaleUtil.parseLocale("")); assertNull(LocaleUtil.parseLocale(" ")); assertEquals(new Locale("zh"), LocaleUtil.parseLocale(" zh ")); assertEquals(new Locale("zh", "CN"), LocaleUtil.parseLocale(" zh _CN")); assertEquals(new Locale("zh", "CN", "var"), LocaleUtil.parseLocale(" zh _CN_var")); assertEquals(new Locale("zh", "CN", "var_xxx"), LocaleUtil.parseLocale(" zh _CN_var_xxx")); assertEquals(new Locale("zh", "", "CN_var_xxx"), LocaleUtil.parseLocale(" zh__CN_var_xxx")); }