I am writing an API (using Java) that takes locale as a parameter. We want the clients to be able to specify "en-US" or "en_US" as they both seem to be widely used across all languages. 

I did go through these links 

1. [How to get locale from string representation in Java][1]
2. [en_US or en-US, which one should you use][2]
3. [Java Locale documentation][3]

Java documentation (source 3 from above) states "Well-formed variant values have the form SUBTAG (('_'|'-') SUBTAG)* where SUBTAG = [0-9][0-9a-zA-Z]{3} | [0-9a-zA-Z]{5,8}. (Note: BCP 47 only uses hyphen ('-') as a delimiter, this is more lenient)." 

 The way I understood this is both "_" and "-" are supported, but their code supports only "-". see my sample unit test below which fails, but passes if i use "en-US".

    public void testLocale() {
        Locale locale = new Locale.Builder().setVariant("en_US").build();
        assertThat(locale.getLanguage(), equalTo("en"));

Are there ways to parse a locale from both forms of string "en_US" and "en-US"? What is the recommended approach here ?

  [1]: http://stackoverflow.com/questions/2522248/how-to-get-locale-from-its-string-representation-in-java
  [2]: http://stackoverflow.com/questions/4904803/en-us-or-en-us-which-one-should-you-use
  [3]: http://docs.oracle.com/javase/7/docs/api/java/util/Locale.html