tl;dr
- Wrong country code.
- Do not use
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.
CLDR
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.
Standard country codes
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
%%
Validator
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.
Avoid Locale
constants
You 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
, Locale.of( "en" , "GB" )
See Locale.of
in action.
Locale locale = Locale.of( "en" , "GB" ) ;
new java.util.Locale("en", "UK").getISO3Country()
throws an exception. – Formidablenew Locale("en", "GB")
(i.e.Locale.UK
is perfectly fine, and there's no such thing asLocale.GB
). – FormidableLocale ca = Locale.CANADA;
, where it returns english as language. So If one needs french the second way is the correct one – IridicLocale.of
.Locale.of( "en" , "GB" )
for English in UK,Locale.of( "en" , "CA" )
for English in Canada,Locale.of( "fr" , "CA" )
for French in Canada. – GlassesUK
is not a valid region (country) subtag. – KristieLocale.UK
is basically the same asnew Locale("en", "GB")
(explaining the difference tonew Locale("en", "UK")
) – Orsola