Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between creating a locale for en-US and en_US?

I have all my ResourceBundle values in a table and formatted as per requirement. I have to change the languages on the website based on user selection in a dropdown at the top of the page.

If I use a language code such as en_US, then it works fine. If I use en-US as a language code, then it doesn't work. What might be the problem? Which is the correct way to do this?

like image 210
Jothi Avatar asked Jan 08 '11 07:01

Jothi


People also ask

What is en_US locale?

For example, “en_US” is a locale that is based on the English language in the United States.

What is en GB locale?

the correct country code in ISO 3166 for the United Kingdom of Great Britain and Northern Ireland is for some reason "GB", so the locale code should be en_GB, and not en_UK as noted here.

What is locale us in Java?

A Locale object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user.


1 Answers

"en-US" is an IETF language tag. While Java'a Locale class was clearly based on IETF language tags, it uses underscores in place of hyphens when separating language codes from country codes (and also variants), so calling toString() on the equivalent Locale will give you en_US.

As of Java 7 you can use Locale.forLanguageTag(String) and toLanguageTag() to convert between language tags and Locale objects.

When converting strings to Locale objects it's a good idea to normalize by splitting components on hyphens and underscores, lowercasing the first component (the language code) and upper-casing the second component (the country code).

like image 51
Laurence Gonsalves Avatar answered Sep 26 '22 21:09

Laurence Gonsalves