Enabling CLDR Locale Data in Java and Groovy

By | 2016-06-30

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!

7 thoughts on “Enabling CLDR Locale Data in Java and Groovy

  1. Lele

    I don’t even know what to say, this made things so much eaeirs!

  2. http://www./

    That is wonderful!!!! Ms. Stritch has given me inspiration to wear a TIE w/ blazer!!!! Glad you had the pleasure of meeting up with her and a chance to take a snap-shot!

  3. Ai Apps

    Howdy I am so delighted I found your blog, I really found you by accident, while
    I was searching on Yahoo for something else, Nonetheless I am here now and would just
    like to say cheers for a tremendous post and a all round entertaining
    blog (I also love the theme/design), I don’t have time to look over it all at the minute but I have book-marked it and
    also added your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the superb job.

  4. New Year 2017 Wishes

    I have been exploring for a little for any high quality articles or blog
    posts on this sort of house . Exploring in Yahoo I ultimately
    stumbled upon this website. Studying this info So i’m happy to
    convey that I have an incredibly good uncanny feeling I came upon just what I needed.
    I so much undoubtedly will make certain to do not omit this web site and provides it a look
    on a continuing basis.

  5. vérification tva

    Please let me know if you’re looking for a article author for your site.
    You have some really good articles and I feel I would be a good asset.

    If you ever want to take some of the load off, I’d absolutely love to write some articles for your blog
    in exchange for a link back to mine. Please blast me an email if interested.
    Many thanks!

  6. http://www./

    CATHIRIA:Tienes toda la razón, solo quien sea arrebatado de este planeta, vivo y con cuerpo físico, y llevado a ese lugar donde se celebrarán las bodas del cordero podrá comprobar que la salvación le alcanzó.Los demás se enfrentarán con impotencia y angustia a esa realidad, la gran tribulación, evento que no les acolitará sus justificaciones. FGuttmann

Leave a Reply

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