How to obtain the equivalent applicationResourcesProvider["mykey"] in Java code

In my portlet I reference labels captions with #{activePreferencesBean.XYZApp.applicationResourcesProvider[“mylabelkey”]} witch resolve to the files I have in the project.

But I need do the same (to generate localized messages) in Java using this same application provider (and files)

I test with ContextUtils.localizeMessage(ContextUtils.getFacesContext(), “AppResources”, “prueba”, null, false) but It returns an exception “Unable to load ResourceBundle, basename=AppResources, locale=en”

How can I do it?

If your code is inside of a managed bean, then the simplest way would be to use the same expression and resolve it from the provided java API.

For example:

String msg = (String)resolveExpression("#{activePreferencesBean.XYZApp.applicationResourcesProvider['mylabelkey']}");

Thank you for your solution.

Another way I have found:

public String getLocalString(String key) {
  FacesContext context = ContextUtils.getFacesContext();
  ResourceBundle bundle = ResourceBundle.getBundle(context.getApplication().getMessageBundle(), ContextUtils.getResponseLocale(context));
  return bundle.getString(key); 


public String getLocalString(String key) {
  ResourceBundle bundle = ResourceBundle.getBundle(getFacesContext().getApplication().getMessageBundle());
  return bundle.getString(key);