Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Get Country of currentPage

Shaheena_Sheikh
Level 4
Level 4

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
mayur_satav
Correct answer by
Level 3
Level 3

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

 

View solution in original post

0 Replies
Kiran_Vedantam
Community Advisor
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.
mayur_satav
Correct answer by
Level 3
Level 3

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

 

View solution in original post

Rohit_Negi550
Level 4
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!

mayur_satav
Level 3
Level 3

Hi @Shaheena_Sheikh ,

 

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

ravijais04
Level 1
Level 1

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)

Rohit_Negi550
Level 4
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.