How to get resource bundle (for localization) in custom workflow process step | Community
Skip to main content
New Participant
December 18, 2020
Solved

How to get resource bundle (for localization) in custom workflow process step

  • December 18, 2020
  • 4 replies
  • 4315 views

How to get i18Dictionary object in custom workflow process step?

Following points tried :
1. As we dont have slingHttpRequest object exists so can not get resource bundle using request.
2. ResourceBundle rb = ResourceBundle.getBundle("projectName");
3. ResourceBundle rb = ResourceBundle.getBundle("project full qualified name");

Best answer by Anudeep_Garnepudi

@sumit1191 

Try below snippet

@Reference(target="(component.name=org.apache.sling.i18n.impl.JcrResourceBundleProvider)") private ResourceBundleProvider resourceBundleProvider; @Override public void execute(WorkItem item, WorkflowSession session, MetaDataMap args) throws WorkflowException { Locale locale = new Locale("de"); ResourceBundle resourceBundle = resourceBundleProvider.getResourceBundle(locale); I18n i18n = new I18n(resourceBundle); }

 

4 replies

Anudeep_Garnepudi
Anudeep_GarnepudiAccepted solution
New Participant
December 21, 2020

@sumit1191 

Try below snippet

@Reference(target="(component.name=org.apache.sling.i18n.impl.JcrResourceBundleProvider)") private ResourceBundleProvider resourceBundleProvider; @Override public void execute(WorkItem item, WorkflowSession session, MetaDataMap args) throws WorkflowException { Locale locale = new Locale("de"); ResourceBundle resourceBundle = resourceBundleProvider.getResourceBundle(locale); I18n i18n = new I18n(resourceBundle); }

 

Sumit1191Author
New Participant
December 22, 2020
@anudeep_garnepudi thanks for the solution.
Kiran_Vedantam
New Participant
December 18, 2020

Hi @sumit1191 

 

You can use ResourceBundleProvider to fetch the ResourceBundle when you do not have the request object.

 

Try this:

@Reference (target= "(component.name=org.apache.sling.i18n.impl.JcrBundleProvider)")
ResourceBundleProvider rbp;

 

ResourceBundle bundle = rbp.getResourceBundle(locale);

 

Reference: https://sling.apache.org/documentation/bundles/internationalization-support-i18n.html

 

Thanks,

Kiran Vedantam.

 

Sumit1191Author
New Participant
December 21, 2020

I tried above solution, its not working. State of the process step is showing Unsatisfied.

Kiran_Vedantam
New Participant
December 18, 2020

The I18n class has 2 constructors:

1. To present the string in the language that is specified in the user account

I18n i18n = new I18n(slingRequest);

2. For using the page locale:

Locale pageLang = currentPage.getLanguage(false);
ResourceBundle resourceBundle = slingRequest.getResourceBundle(pageLang);
I18n i18n = new I18n(resourceBundle);

 

Can you let me know how are you planning to trigger the workflow? Are you not using the servlet?

 

Thanks,

Kiran Vedantam.

Sumit1191Author
New Participant
December 18, 2020

We are planning to trigger workflow manually.

 

As you mentioned 2 ways to get i18n object,  we need slingRequest object, but we don't have the object in process step.

SureshDhulipudi
New Participant
December 18, 2020

Did you try getting Resource from workflow session?

 

// Get ResourceResolver from workflow session
final Map<String, Object> authInfo = new HashMap<>();
authInfo.put(JcrResourceConstants.AUTHENTICATION_INFO_SESSION, workflowSession.getSession());
ResourceResolver resourceResolver = null;
Resource resource = null;
try{
resourceResolver = resourceResolverFactory.getResourceResolver(authInfo);
// Get the resource of payload
resource = resourceResolver.getResource(path);

}

Sumit1191Author
New Participant
December 18, 2020

Actually the payload is Content Fragment.

Also using the path I got resource as well.

 

But how we can get Resource Bundle (Dictionary object) using it.