How to Convert ISO3 Country Codes to ISO2 and Vice Versa with a Grails Service

Situation

Often I am in the need to convert ISO Country Codes, either from ISO Alpha-3 to ISO Alpha-2 (e.g. “DEU” -> “DE”) or to find the according country name of an ISO Country Code (e.g. “DEU” -> “Germany”).

Working with ISO Alpha-2 codes is easy, because their conversions are directly implemented in java.util.Locale.

But there are no equivalent methods contained in java.util.Locale for ISO Alpha-3 codes.

Method

The generally accepted solution is to create a map, which contains the alpha-3 and alpha-2 codes. Then map from alpha-3 to alpha-2, so you can use the alpha-2 based methods of java.util.Locale.

Here is an example, how to do it with Java and you get the overall idea.

Of course you might use this coding directly in Grails, but I wanted to have a groovy Grails service: IsoCodeService.

Solution

IsoCodeService, here we go …

At first we create the isoCodesMap for the mapping between alpha-3 and alpha-2 codes.

def initIsoCodes () {
   Locale.getISOCountries().each { country ->
       def locale = new Locale("", country)
       // e.g. isoCodesMap = [AND:AD, ARE:AE, AFG:AF, ATG:AG, AIA:AI, ALB:AL, ARM:AM, ANT:AN,....
       isoCodesMap.put(locale.getISO3Country().toUpperCase(), country)
   }
}

To convert the alpha-2 to an alpha-3 code (e.g. “DE” -> “DEU”), we just use the getISO3Country() method of java.util.Locale.

def iso2CountryCodeToIso3CountryCode(String iso2CountryCode){
   def locale = new Locale("", iso2CountryCode);
   return locale.getISO3Country();
}

And vice versa, to convert an alpha-3 to an alpha-2 code (e.g. “DEU” -> “DE”), we use the created mappings in isoCodesMap:

def iso3CountryCodeToIso2CountryCode(String iso3CountryCode) {
   // e.g. DEU -> DE, AUT -> AT, AFG -> AF, ....
   return isoCodesMap.(iso3CountryCode.toUpperCase())
}

To get the country’s name from the iso code (“DE” -> “Germany”, “DEU” -> “Germany”), we can use the following coding:

def iso2CountryCodeToCountryName(String iso2CountryCode) {
   return isoCountryCodeToCountryName(iso2CountryCode)
}

def iso3CountryCodeToCountryName(String iso3CountryCode) {
   def iso2CountryCode = isoCodesMap.(iso3CountryCode.toUpperCase())
   return isoCountryCodeToCountryName(iso2CountryCode)
}

private def isoCountryCodeToCountryName(String isoCountryCode) {
   def locale = new Locale("", isoCountryCode);
   return locale.getDisplayCountry()
}

The initIsoCodes() method should be called once, when the Grails service class IsoCodeService is initialized.
Conveniently, I use the implementation of InitializingBean and its afterPropertiesSet() method.

This is complete service class IsoCodeService:

package services

import grails.transaction.Transactional
import org.springframework.beans.factory.InitializingBean

class IsoCodeService implements InitializingBean {
   static transactional = true
   def isoCodesMap = [:]

   public void afterPropertiesSet() throws Exception {
      //initialization logic goes here
      initIsoCodes()
   }

   def iso3CountryCodeToIso2CountryCode(String iso3CountryCode) {
      // e.g. DEU -> DE, AUT -> AT, AFG -> AF, ....
      return isoCodesMap.(iso3CountryCode.toUpperCase())
   }

   def iso2CountryCodeToIso3CountryCode(String iso2CountryCode){
      def locale = new Locale("", iso2CountryCode);
      return locale.getISO3Country();
   }

   def iso2CountryCodeToCountryName(String iso2CountryCode) {
      return isoCountryCodeToCountryName(iso2CountryCode)
   }

   def iso3CountryCodeToCountryName(String iso3CountryCode) {
      def iso2CountryCode = isoCodesMap.(iso3CountryCode.toUpperCase())
      return isoCountryCodeToCountryName(iso2CountryCode)
   }

   private def isoCountryCodeToCountryName(String isoCountryCode) {
      def locale = new Locale("", isoCountryCode);
      return locale.getDisplayCountry()
   }

   private def initIsoCodes () {
      Locale.getISOCountries().each { country ->
         def locale = new Locale("", country)
	 // e.g. isoCodesMap = [DEU:DE, AUT:AT, AFG:AF, ATG:AG, AIA:AI, ALB:AL, ARM:AM, ANT:AN,....
	 isoCodesMap.put(locale.getISO3Country().toUpperCase(), country)
      }
      log.info "isoCodesMap: ${isoCodesMap}"
   }
}

Summary

The IsoCodeService provides a convenient Grails service to convert ISO alpha-3 country codes to ISO alpha-2 country codes and vice versa. It is also possible to derive the country’s name of the appropriate ISO code.

This entry was posted in Development, Grails and tagged , , , . Bookmark the permalink.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.