How to change tag's language present on page based on language of page? | Community
Skip to main content
Level 3
January 12, 2023
Solved

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

  • January 12, 2023
  • 2 replies
  • 1029 views

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.

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by lukasz-m

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.

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; } }

 

2 replies

Anmol_Bhardwaj
Community Advisor
Community Advisor
January 12, 2023

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.

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
January 12, 2023

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.

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; } }