Reputation: 4985
That is, if I have "English (United States)" I'd like to get "en-US", or an appropriate java.util.Locale
. It looks like the API is a one-way street, but perhaps I'm not looking in the right place. Thanks in advance.
Upvotes: 6
Views: 3934
Reputation: 12
You could use Apache's commons-lang:
org.apache.commons.lang.LocaleUtils.toLocale(localeStr);
Upvotes: -2
Reputation: 108879
Display name in which language?
Locale[] locales = Locale.getAvailableLocales();
for (Locale current : locales) {
for (Locale test : locales) {
System.out
.print(test.getDisplayName(current) + " ");
}
System.out.println();
}
I assume that if you are dealing with locales, you need to handle multiple languages.
Upvotes: 3
Reputation: 191895
No, it does not appear that there is such a method in the API. However, you could create a cache using the Locales
returned by Locale.getAvailableLocales()
; then you can simply look up the display name in this cache.
private static Map<String, Locale> displayNames = new HashMap<String, Locale>();
static {
for (Locale l : Locale.getAvailableLocales()) {
displayNames.put(l.getDisplayName(), l);
}
}
public static Locale getLocale(String displayName) {
return displayNames.get(displayName);
}
Upvotes: 8
Reputation: 19320
I did this once and I don't think it's guaranteed to work all the time because the locales could be named differently in each JVM implementation. Loop through the locales until you find the one you want.
Upvotes: 1