en_US or en-US, which one should you use?

JavaLocalizationLocale

Java Problem Overview


Assume you want to store the locale of user preference in database, which value you will use?

en_US or en-US

They are two standards, but which one you prefer to use as part of your own application?

Updated: Is seems many web sites use dash instead of underscore, e.g.

http://zh.wikipedia.org/zh-tw http://www.google.com.hk/search?hl=zh-TW

Java Solutions


Solution 1 - Java

I'm pretty sure "-" is the standard. If you see "_" somewhere it's probably something some people came up with to make it a valid identifier.

Personally I'd go with "-", just to be correct.

http://en.wikipedia.org/wiki/IETF_language_tag

https://datatracker.ietf.org/doc/html/rfc5646

Solution 2 - Java

If you're working with Java, you might as well use the Java locale format (en_US).

The BCP 47 documents actually do specify the en-US format, and it's just as common if not more common than Java-style locale names. But in practice you'll see the form with the underbar quite a bit. For example, both Java and most POSIX-type platforms use the underbar for their language/region separator.

So you can't go far wrong with either choice. But given that you're writing in Java and probably targeting a Unix platform, en_US is probably the way to go.

Solution 3 - Java

In Java 7, there is a new method Locale.forLanguageTag(String), which assumes the hyphen as a separator. I'd consider that as normative.

Check the documentation of Locale for more information.

Solution 4 - Java

en_US. This is a very useful read.

Solution 5 - Java

I don't think en-US is a standard at all for Java. (If you see it somewhere could you add a link).

So just use en_US.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHowardView Question on Stackoverflow
Solution 1 - JavaMatti VirkkunenView Answer on Stackoverflow
Solution 2 - JavadkarpView Answer on Stackoverflow
Solution 3 - Javauser1774051View Answer on Stackoverflow
Solution 4 - JavaCoolBeansView Answer on Stackoverflow
Solution 5 - JavajzdView Answer on Stackoverflow