Most Recent

Blog Post

Displaying Country Names by Code in Pega

Displaying Country Names by Code in Pega

As mentioned in my previous article on Country Picker Controls you might prefer the legacy Pick-Country control of the newer Pick-Country-i18n having internationalization for the listed options.

The minor downside is that such legacy controls are might NOT be “auto-generated”. With means the HTML source for this rule instance is custom, but at the same (design) time hardcoded. For example, the Pick-Country won’t show the Control rule form with Control Modes (Editable/Read-only, Read-only or Action) like with the standard pxTextInput or pxDropdown. This implies that this picker is only of the Editable mode and cannot be used directly for Read-only display — have a try by dragging it as read-only onto a Section — as it won’t behave properly.

This brings us to a simple approach of getting the display name for the country code stored. The output of such supporting utility Function rule can be stored in a paired pyCountryName property beforehand and even be kept in synch through a Declare Expression if needed.

For the utility function implementation the Java.util.Locale class is of particular interest, because the class features:

  • getDisplayCountry()
  • getDisplayLanguage()

As well as basic conversion through:

  • getCountry() for 2-letter code
  • getISO3Country() for 3-letter obviously
  • getISOCountries() lists all 2-letter codes according to ISO 3166

I´ve written the following few lines of code in Java for getting the country display name given the 3-letter code:

public static String getISO3DisplayCountry(String code) {
    String[] countries = Locale.getISOCountries();
    String displayname = "";
    for (int i = 0; i < countries.length; i++) {
        Locale locale = new Locale("", countries[i]);
        if (locale.getISO3Country().equalsIgnoreCase(code)) {
displayname = locale.getDisplayCountry();
}
    }
    return displayname;
}

For Pega, just copy-n-paste this (all but the first a last line) in a suitable new function rule. This should look something like: Source code for getISO3DisplayCountry(String)

Also, make sure you use matching parameter signature for input and output:

Input parameters
NameDescriptionJava typePega type
code3-letter country code conform ISO 3166StringText

Output
Java data typePega typePega class
StringText 

Exemplary calls

The following expression examples be used inline in the usual places like Data Transforms and Activities or even Decision Tables and Trees:

  • @getISO3DisplayCountry(.Landcode)
  • @getISO3DisplayCountry(.pyCountryCode)
  • @getISO3DisplayCountry(Param.CountryCode)

Similarly, the function that would take a 2-letter (ALPHA2) code could be implemented as:

public static String getISO2DisplayCountry(String code) {
    String[] countries = Locale.getISOCountries();
    String displayname = "";
    for (int i = 0; i < countries.length; i++) {
        Locale locale = new Locale("", countries[i]);
        if (locale.getISOCountry().equalsIgnoreCase(code)) {
	    displayname = locale.getDisplayCountry();
}
    }
    return displayname;
}

Thank you for visiting! #StaySafe [Edgar] (4/5/2021 11:51:10 AM)

 

Twitter Feed

@edgarverburg

Bio

About Edgar

Edgar is a software engineer with experience in TIBCO Middleware and Pega Case Managemement. He holds a master's degree in Computer Science with a specialization in Data Visualization & Computer Graphics.

In his spare time Edgar reads SOS and Empire, mixes house music, blogs and writes film reviews or goes running.


Currently employed by SynTouch he is specifically looking for a PRPC project. Feel free to contact him for challenging assignments through LinkedIn.