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?
The java.util.Locale class object represents a specific geographical, political, or cultural region. . Following are the important points about Locale − An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to form information for the user.
To get equivalent information in Java, use Locale. getDefault() to get the Locale that Java is using, and use methods on the Locale object such as getCountry() , getLanguage() to get details. The information is available using ISO codes and as human readable/displayable names.
No. new Locale( "AU" )
would be the language "AU" (whatever that is). You need the two argument constructor!
The Locale.equals()
methods compares both language
, country
and variant
. You should probably check like this:
if ( "AU".equals( locale.getCountry() ) ) { /* do stuff */ }
As for why the list of predefined Locale
's are so limited: Pass. We should probably be honored that there is anything but en_US
at all :-)
Cheers,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With