Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

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

Avatar

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

Avatar

Correct answer by
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

Avatar

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

}

Avatar

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.

Avatar

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

Avatar

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.

Avatar

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.

Avatar

Community Advisor
Did the previous post by answered your question? your query seems to be in resolved state

Avatar

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.

 

Avatar

Level 2

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

Avatar

Correct answer by
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);
    }