Translation of Pages, Experience Fragment and Content Fragment | Community
Skip to main content
Level 6
March 6, 2024

Translation of Pages, Experience Fragment and Content Fragment

  • March 6, 2024
  • 2 replies
  • 595 views

I am trying to translate experience fragment and content fragment via code and 3rd Party API.

 

For Page it is working: we can pick page.getLanguage() return the jcr:language of the page

For experience Fragment: How to pick language similar to page

For content Fragment: Same how to get the language.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

PGURUKRISHNA
Level 4
March 7, 2024

Hi @ronnie09 

To get the language for Experience Fragments and Content Fragments, you can follow a similar approach as you did for pages. You can access the language information through the JCR properties of the fragment's node.

For Experience Fragment:

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;

// ...

Resource experienceFragmentResource = ... // Get the experience fragment resource
ResourceResolver resourceResolver = experienceFragmentResource.getResourceResolver();
String language = resourceResolver.getLanguage(experienceFragmentResource.getPath());

For Content Fragment:

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;

// ...

Resource contentFragmentResource = ... // Get the content fragment resource
ResourceResolver resourceResolver = contentFragmentResource.getResourceResolver();
String language = resourceResolver.getLanguage(contentFragmentResource.getPath());

 

These code snippets demonstrate how to get the language for Experience Fragments and Content Fragments by using the ResourceResolver to get the language based on the resource's path.




kautuk_sahni
Community Manager
Community Manager
May 20, 2024

@ronnie09 Did you find the suggestion helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

 

Kautuk Sahni