Enabling CLDR Locale Data in Java and Groovy

Groovy Internationalization

Enabling CLDR Locale Data in Java and Groovy

Duke jdk8 1 Today a colleague asked whether I could provide a list of localized country names for Portuguese. I know that the CLDR database contains all that great localization goodness, but I didn’t have the time to dig through the raw data. Still, I wanted to help, and I know that the Java platform has included CLDR data since 1.8. I decided to create a short Groovy language script to retrieve and display the information. Because Groovy runs in the JVM, it also has access to that CLDR information.

The Groovy Script

Here’s the script I came up with. I named it “countries”.

#!/usr/bin/env groovy 
 * Created by joconner on 6/29/16.

supportedLangs = Locale.getISOLanguages();

if (!(this.args.length > 0 && supportedLangs.contains(this.args[0].toLowerCase()))) {
    printf("Usage: countries <lang_code>\n")
    printf("\t<lang_code> must be an ISO lang code. Examples: en, pt, fr, es, ja.\n")
currentLocale = new Locale.Builder().setLanguageTag(this.args[0].toLowerCase()).build()
locales = Locale.getISOCountries()
for(c in locales) {
    targetLocale = new Locale.Builder().setRegion(c).build()
    printf("%s, %s\n", targetLocale.getCountry(), targetLocale.getDisplayCountry(currentLocale))

I won’t go into the script in detail. If you are familiar with Java, you’ve probably seen these classes and methods before. The interesting thing is that the CLDR data is not enabled by default.

Enabling CLDR

For backward compatibility, CLDR locale data is not enabled by default. However, you can enable it by setting a system property on the Java command line. The system property is java.locale.providers. If you set this to the value CLDR, the JRE will use CLDR as the locale data source.

Run the above script like this to get Portuguese (pt) names for all supported ISO countries in the JRE: groovy -Djava.locale.providers=CLDR countries pt

Although this example is written in Groovy, the same system property is available with the Java command line as well. Just run your Java application like this: java -Djava.locale.providers=CLDR

Analyzing Differences

I ran the above script twice, once using CLDR data and once using the default JRE data. For the pt language, I discovered a couple differences in JDK 1.8.0_92:

BQ, Bonaire, Sint Eustatius, and Saba BQ, Bonaire, Sint Eustatius and Saba
SX, Sint Maarten SX, Sint Maarten (Dutch part)

I suspect you’ll find other and perhaps more significant differences for other languages.

Downloading Code

You can git the code for this article from my github account. Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *