I had the same questions, and this is what I found.
If you create a Locale with a constructor like:
Locale locale = new Locale("en_US");
and then you call getLanguage :
String language = locale.getLanguage();
The language value will be "en_us";
If you create a Locale using the builder:
Locale locale = new Locale.Builder().setLanguage("en").setRegion("US").build()
Then the value of locale.getLanguage() will return, it will be "en".
This is strange for me, but it was implemented that way.
So, this was a long answer to explain that if you want the language code to return the two-letter ISO language, you need to use the Java Locale constructor or do some string manipulation.
Your substring method works, but I would like to use something like the one below to cover cases where the delimiter can be "-" or "_".
public String getLang(Locale language) String[] localeStrings = (language.split("[-_]+")); return localeStrings[0]; }
Brod
source share