-
-
Save mattiz/917399 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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