Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

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

Sumit1191
Level 2
Level 2

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");

1 Accepted Solution
Anudeep_Garnepudi
Correct answer by
Community Advisor
Community Advisor

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

 

View solution in original post

11 Replies
SureshDhulipudi
Community Advisor
Community Advisor

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

}

Sumit1191
Level 2
Level 2

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.

SureshDhulipudi
Community Advisor
Community Advisor

@reference(target = "(component.name=org.apache.sling.i18n.impl.JcrResourceBundleProvider)")

private ResourceBundleProvider resourceBundleProvider;

 

ResourceBundle resourceBundle = getResourceBundleProvider().getResourceBundle(locale);
I18n i18n = new I18n(resourceBundle);

Kiran_Vedantam
Community Advisor
Community Advisor

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.

Sumit1191
Level 2
Level 2

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
Did the previous post by answered your question? your query seems to be in resolved state
Kiran_Vedantam
Community Advisor
Community Advisor

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.

 

Sumit1191
Level 2
Level 2

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

Anudeep_Garnepudi
Correct answer by
Community Advisor
Community Advisor

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