Question : How do I correctly use java.util.locale for checking a user’s locale?
Summary : The legacy code I have uses a predefined static in Locale to check if a user is, for example, in France …
if(Locale.FRANCE.equals(locale) || Locale.FRENCH.equals(locale)) {
// do stuff
}
I wish to add some code to check if a user is in Australia. However, Locale only has a limited set of predefined statics, and AUSTRALIA is not one of them. I appear to be able to do the following …
if(new Locale("AU").equals(locale)) {
// do stuff
}
However, this is inconsistent with the existing code. What is the correct way of doing it? If the first example I have given is correct, why is the predefined list of statics so limited?
No.
new Locale( "AU" )would be the language “AU” (whatever that is). You need the two argument constructor!The
Locale.equals()methods compares bothlanguage,countryandvariant. You should probably check like this:As for why the list of predefined
Locale‘s are so limited: Pass. We should probably be honored that there is anything buten_USat all 🙂Cheers,