Expand my Community achievements bar.

SOLVED

How to change tag's language present on page based on language of page?

Avatar

Level 3

We are trying to change tag language depending on page language. We have jcr.title.language_locale property in each tag. but When select page with language other English, it is still showing in english.

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @sangrampatil111,

Localized tags requires proper implementation, setting different language versions on tag level is only first step.

On code level you should utilize one of below methods from Tag java api:

Both methods rely on Local object that allows to recognize which language version of tag should be returned.

Local can be retrieved using Page java api via:

Above methods will base on jcr:language property that can be set on page properties level under Advanced tab or on url structure.

language.jpg

Here is sample of Sling model implementation that returns localized list of tags base on url.

 

@Model(adaptables = {SlingHttpServletRequest.class})
public class TagComponent {

    @Self
    private SlingHttpServletRequest request;

    @SlingObject
    private ResourceResolver resourceResolver;

    private List<String> localizedTags;

    @PostConstruct
    protected void init() {
        localizedTags = new ArrayList<String>();
        PageManager pageManager = resourceResolver.adaptTo(PageManager.class);

        if (pageManager != null) {
            Page page = pageManager.getContainingPage(request.getResource());
            if (page != null) {
                Locale locale = page.getLanguage(true);
                for (Tag tag : page.getTags()) {
                    localizedTags.add(tag.getTitle(locale));
                }
            }
        }
    }

    public List<String> getLocalizedTags() {
        return localizedTags;
    }
}

 

View solution in original post

2 Replies

Avatar

Community Advisor

Hi @sangrampatil111 ,

 

Would it be possible for you to share the logs for this.

Maybe the jcr.title.language_locale property is not being correctly set or read when the page language is changed.

Another possibility is that there is a problem with the way that the tags are being displayed on the page, and that they are not properly picking up the language from the jcr.title.language_locale property.

 

Only way to confirm would be to look at the logs.

Avatar

Correct answer by
Community Advisor

Hi @sangrampatil111,

Localized tags requires proper implementation, setting different language versions on tag level is only first step.

On code level you should utilize one of below methods from Tag java api:

Both methods rely on Local object that allows to recognize which language version of tag should be returned.

Local can be retrieved using Page java api via:

Above methods will base on jcr:language property that can be set on page properties level under Advanced tab or on url structure.

language.jpg

Here is sample of Sling model implementation that returns localized list of tags base on url.

 

@Model(adaptables = {SlingHttpServletRequest.class})
public class TagComponent {

    @Self
    private SlingHttpServletRequest request;

    @SlingObject
    private ResourceResolver resourceResolver;

    private List<String> localizedTags;

    @PostConstruct
    protected void init() {
        localizedTags = new ArrayList<String>();
        PageManager pageManager = resourceResolver.adaptTo(PageManager.class);

        if (pageManager != null) {
            Page page = pageManager.getContainingPage(request.getResource());
            if (page != null) {
                Locale locale = page.getLanguage(true);
                for (Tag tag : page.getTags()) {
                    localizedTags.add(tag.getTitle(locale));
                }
            }
        }
    }

    public List<String> getLocalizedTags() {
        return localizedTags;
    }
}