Expand my Community achievements bar.

Translation of Pages, Experience Fragment and Content Fragment

Avatar

Level 7

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.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Reply

Avatar

Level 3

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.