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

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

  • December 18, 2020
  • 4 replies
  • 4326 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

SureshDhulipudi
Community Advisor
Community Advisor
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
Level 2
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.

Kiran_Vedantam
Community Advisor
Community Advisor
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
Level 2
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.

Kiran_Vedantam
Community Advisor
Community Advisor
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
Level 2
December 21, 2020

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

Anudeep_Garnepudi
Community Advisor
Anudeep_GarnepudiCommunity AdvisorAccepted solution
Community Advisor
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); }

 

AG
Sumit1191Author
Level 2
December 22, 2020
@anudeep_garnepudi thanks for the solution.