Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Get Country of currentPage

Avatar

Level 6

In my Java model class I have the currentPage from which I'm trying to get the country. But it always gives empty/null value. I tried the following:

currentPage.getLanguage() --> gives me the locale

locale.getLanguage-->gives me the language of the locale

currentPage.getLanguage().getCountry()-->empty

currentPage.getLanguage().getDisplayCountry()-->empty

I'm getting the correct language codes for both cases but not the country. What should be the correct approach?

 

1 Accepted Solution

Avatar

Correct answer by
Level 4

Hi @Shaheena_Sheikh,

 

I had a similar requirement and I was able to achieve it via authoring & currentPage object. My home page which is also my language root had the name as its language code ("en" or "de" or "es" etc). Also for the same page I had to author the "Language" field in Advanced tab of page properties. Having these combined I was able to get the country and language for a specific page and its child pages. I think the authoring part will be missing in your case.

Below is for reference:

  1. My page - /content/sample/generic-root/switzerland/de
  2. Page properties:Screenshot 2022-05-27 at 02.20.24.png
  3. Sightly code
name --> ${currentPage.name}
country --> ${currentPage.language.displayCountry}
lang --> ${currentPage.language.displayLanguage}
Output:
name --> de
country --> Switzerland
lang --> German

Hope this helps! 

 

Thanks!!

View solution in original post

7 Replies

Avatar

Community Advisor

Hi @Shaheena_Sheikh,

 

As per the documentation: https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html

 

getCountry()

Returns the country/region code for this locale, which should either be the empty string, an uppercase ISO 3166 2-letter code, or a UN M.49 3-digit code.
 
Please check if the country/region code is set for the page while creating it. Refer here.
 
Hope this helps!
 
Thanks,
Kiran Vedantam.

Avatar

Community Advisor

Hi @Shaheena_Sheikh ,

 

If you have constant pattern in your URLs then you can use below appraoch.

e.g. In our Project we had below contant pattern for URLs 

/content/webistename/us/en_us

/content/webistename/ie/en_gb

.

.

etc.

so we created below custom method to get country. you can try this if it fits for your requirement.

 

public static String getCountry(Resource resource) {
        String country = StringUtils.EMPTY;
        if (null != resource && StringUtils.isNotBlank(resource.getPath())) {
            String relPath = resource.getPath();
            if (relPath.contains("/")) {
                String[] path = relPath.split("/");
                country = path[3];
            }
        }
        return country;

Thank you

 

Avatar

Level 4

Hi @Shaheena_Sheikh 

 

Your code should work when your structure would be somewhat like: /content/we-retail/ca/en_CA.html

Here value after "_" will act as a country in currentPage.getLanguage().getCountry().

 

Thanks!

Avatar

Community Advisor

Hi @Shaheena_Sheikh ,

 

I already mentioned it. But if you want to use above method you need to modify its index value accordingly.

Avatar

Level 3

I believe that the content hierarchy doesn't have country code. it's just language code.

i.e. /content/product/en/demo

in this case, you would want to create a field in page Properties ,common for all pages, as country and process the same.

or

you can explicitly try passing the locale name containing the language code and country code.

i.e. request.getLocale().getDisplayCoutnry(Locale.Canada)

Avatar

Level 4

Yes of course,if you want it configurable you have many options... few of them would be osgi config, csv ,json etc. As well.

Avatar

Correct answer by
Level 4

Hi @Shaheena_Sheikh,

 

I had a similar requirement and I was able to achieve it via authoring & currentPage object. My home page which is also my language root had the name as its language code ("en" or "de" or "es" etc). Also for the same page I had to author the "Language" field in Advanced tab of page properties. Having these combined I was able to get the country and language for a specific page and its child pages. I think the authoring part will be missing in your case.

Below is for reference:

  1. My page - /content/sample/generic-root/switzerland/de
  2. Page properties:Screenshot 2022-05-27 at 02.20.24.png
  3. Sightly code
name --> ${currentPage.name}
country --> ${currentPage.language.displayCountry}
lang --> ${currentPage.language.displayLanguage}
Output:
name --> de
country --> Switzerland
lang --> German

Hope this helps! 

 

Thanks!!