@Test public void testParseLocale() { assertEquals( new Locale(Locale.ENGLISH.getLanguage(), Locale.US.getCountry()), LocaleUtils.parseLocale("en-us") ); assertEquals( new Locale(Locale.ENGLISH.getLanguage(), Locale.US.getCountry()), LocaleUtils.parseLocale("EN-us") ); assertEquals( new Locale(Locale.ENGLISH.getLanguage(), Locale.US.getCountry()), LocaleUtils.parseLocale("en-US") ); assertEquals( new Locale(Locale.ENGLISH.getLanguage(), Locale.US.getCountry()), LocaleUtils.parseLocale("EN-US") ); assertEquals( new Locale(Locale.ENGLISH.getLanguage(), Locale.US.getCountry()), LocaleUtils.parseLocale("en_us") ); assertEquals( new Locale(Locale.ENGLISH.getLanguage(), Locale.US.getCountry()), LocaleUtils.parseLocale("en - US_") ); assertEquals( new Locale(Locale.FRENCH.getLanguage(), Locale.CANADA.getCountry(), "Quebec"), LocaleUtils.parseLocale("fr_ca_Quebec") ); assertFalse( new Locale(Locale.ENGLISH.getLanguage()).equals( LocaleUtils.parseLocale("en_us") ) ); assertFalse( new Locale(Locale.GERMAN.getLanguage(), Locale.US.getCountry()).equals( LocaleUtils.parseLocale("en_us") ) ); }
public static Locale parseLocale(String string) { String[] segments = string.split("[-_]"); if (segments.length == 1) { if ("".equals(segments[0].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim()); } } else if (segments.length == 2) { if ("".equals(segments[0].trim()) || "".equals(segments[1].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim(), segments[1].trim()); } } else if (segments.length == 3) { if ("".equals(segments[0].trim()) || "".equals(segments[1].trim()) || "".equals(segments[2].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim(), segments[1].trim(), segments[2].trim()); } } else { throw new IllegalArgumentException("Invalid locale string: " + string); } }
LocaleUtils { public static Locale parseLocale(String string) { String[] segments = string.split("[-_]"); if (segments.length == 1) { if ("".equals(segments[0].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim()); } } else if (segments.length == 2) { if ("".equals(segments[0].trim()) || "".equals(segments[1].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim(), segments[1].trim()); } } else if (segments.length == 3) { if ("".equals(segments[0].trim()) || "".equals(segments[1].trim()) || "".equals(segments[2].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim(), segments[1].trim(), segments[2].trim()); } } else { throw new IllegalArgumentException("Invalid locale string: " + string); } } }
LocaleUtils { public static Locale parseLocale(String string) { String[] segments = string.split("[-_]"); if (segments.length == 1) { if ("".equals(segments[0].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim()); } } else if (segments.length == 2) { if ("".equals(segments[0].trim()) || "".equals(segments[1].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim(), segments[1].trim()); } } else if (segments.length == 3) { if ("".equals(segments[0].trim()) || "".equals(segments[1].trim()) || "".equals(segments[2].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim(), segments[1].trim(), segments[2].trim()); } } else { throw new IllegalArgumentException("Invalid locale string: " + string); } }  }
LocaleUtils { public static Locale parseLocale(String string) { String[] segments = string.split("[-_]"); if (segments.length == 1) { if ("".equals(segments[0].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim()); } } else if (segments.length == 2) { if ("".equals(segments[0].trim()) || "".equals(segments[1].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim(), segments[1].trim()); } } else if (segments.length == 3) { if ("".equals(segments[0].trim()) || "".equals(segments[1].trim()) || "".equals(segments[2].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim(), segments[1].trim(), segments[2].trim()); } } else { throw new IllegalArgumentException("Invalid locale string: " + string); } }  static Locale parseLocale(String string); static Locale bestLocale(Locale[] preferredLocales, Locale[] availableLocales); }
LocaleUtils { public static Locale parseLocale(String string) { String[] segments = string.split("[-_]"); if (segments.length == 1) { if ("".equals(segments[0].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim()); } } else if (segments.length == 2) { if ("".equals(segments[0].trim()) || "".equals(segments[1].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim(), segments[1].trim()); } } else if (segments.length == 3) { if ("".equals(segments[0].trim()) || "".equals(segments[1].trim()) || "".equals(segments[2].trim())) { throw new IllegalArgumentException("Invalid locale string: " + string); } else { return new Locale(segments[0].trim(), segments[1].trim(), segments[2].trim()); } } else { throw new IllegalArgumentException("Invalid locale string: " + string); } }  static Locale parseLocale(String string); static Locale bestLocale(Locale[] preferredLocales, Locale[] availableLocales); static final Locale DEFAULT_LOCALE; }