Skip to content

Instantly share code, notes, and snippets.

@mattiz
Forked from trygvis/LocaleUtils.java
Created April 13, 2011 11:44
Show Gist options
  • Save mattiz/917399 to your computer and use it in GitHub Desktop.
Save mattiz/917399 to your computer and use it in GitHub Desktop.
public class LocaleUtil {
private static Pattern pattern = Pattern.compile("^([a-z][a-z])(_([A-Z][A-Z]))?$");
/**
* Returns null if the string couldn't be parsed
*/
public static Locale parseStringToLocale(String s) {
Matcher matcher = pattern.matcher(s);
if(!matcher.matches()) {
throw new IllegalArgumentException("Could not parse locale")
}
String language = matcher.group(1);
String country = matcher.group(3);
if(country == null) {
return new Locale(language);
}
return new Locale(language, country);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment