Is there any difference between this two ways of creating a Locale
:
Locale uk = Locale.UK;
Locale en_uk = new Locale("en", "UK");
The only difference I have noticed so far is that getDisplayCountry
returns different values. The first United Kingdom
and the second UK
, which does not bother me very much. Is there besides that any difference? Especially because I would like to adopt the first style for other regions like Germany, France, Korea etc
Locale
constants. Use Locale.of
.Locale.of( "en" , "GB" ) // For English language, and cultural norms of United Kingdom.
Other examples:
Locale.of( "en" , "CA" )
for English in Canada.Locale.of( "fr" , "CA" )
for French in Canada.Locale.of( "en" , "US" )
for English in United States.GB
, not UK
For your purpose, the correct country code for the United Kingdom of Great Britain and Northern Ireland is GB
, not UK
, as commented by Mark Rotteveel.
Modern Java (9+) uses by default the Common Locale Data Repository (CLDR) as the primary source of localization data. Entry # 827 in the CLDR shows a country code of GB
for United Kingdom.
The Javadoc for Locale
states that the country/region code must be either:
If curious, dump to console:
Arrays.stream( Locale.getISOLanguages() ).forEach( System.out::println ); // All 2-letter country codes defined in ISO 3166.
Arrays.stream( Locale.getISOCountries() ).forEach( System.out::println ); // All 2-letter language codes defined in ISO 639.
The Javadoc also notes that a complete list can be found in the IANA Language Subtag Registry with a tip to search for Type: region
. There we find this entry telling us to use GB
for the United Kingdom.
%%
Type: region
Subtag: GB
Description: United Kingdom
Added: 2005-10-16
Comments: as of 2006-03-29 GB no longer includes the Channel Islands and
Isle of Man; see GG, JE, IM
%%
The Comment by andrewJames points us to this online validator by Christoph Schneegans for IETF BCP 47 language tag with language and country code combined.
There we see en-UK
fails, while en-GB
passes.
Locale
constantsYou should ignore those constants defined on Locale
class such as Locale.UK
and Locale.CANADA_FRENCH
. Their creation was a hasty poor decision in design. For more info, see my longer Answer.
Instead, in modern Java (19+), use the convenient static factory methods Locale.of
.
Locale.UK
See Locale.of
in action.
Locale locale = Locale.of( "en" , "GB" ) ;