How to use java.util.Locale correctly for checking the Australian language

Question: How to use java.util.locale to validate a user locale?

Summary: The legacy code that I use in the locale has predefined statics to check if the user has, for example, in France ...

if(Locale.FRANCE.equals(locale) || Locale.FRENCH.equals(locale)) { // do stuff } 

I want to add a code to check if the user is in Australia. However, Locale has only a limited set of predefined statics, and AUSTRALIA is not one of them. It seems I can do the following ...

 if(new Locale("AU").equals(locale)) { // do stuff } 

However, this does not match the existing code. What is the right way to do this? If the first example I gave is correct, why is the predefined list of statics so limited?

+4
source share
1 answer

No. new Locale( "AU" ) will be the language of "AU" (whatever that is). You need two argument constructors!

The Locale.equals() methods compare both language , country , and variant . You should probably check the following:

 if ( "AU".equals( locale.getCountry() ) ) { /* do stuff */ } 

As for why the list of predefined Locale so limited: Pass. We should probably read that there is something other than en_US

Greetings

+4
source

Source: https://habr.com/ru/post/1444796/


All Articles