ResourceBundle not working in AEM 6.2

829 views Asked by At

Following the documentation here, I am trying to get i18n working on my AEM 6.2 instance:

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

The problem is, I always get a org.apache.sling.engine.impl.helper.NullResourceBundle which seems to be just an empty fallback bundle if nothing was found. I tried to preload the bundles in the OSGi configuration, but to no avail.

Even if my own translations wouldn't work, at least it should contain the predefined translations provided with the product. Though my own show up in the tool: http://localhost:4502/libs/cq/i18n/translator.html

Any ideas why I don't get a valid resource bundle?

EDIT: I did a simple JSP test and there it works, so it is related to Sling Models where the code resides within a @PostConstruct method where the model was adapted from the SlingHttpServletRequest

1

There are 1 answers

0
Thomas On

With the help of the wcm.io mailing list and a snippet from it's code I was able to get this working:

Locale pageLang = currentPage.getLanguage(false);
SlingHttpServletRequest request = slingRequest;
SlingBindings bindings = (SlingBindings)slingRequest.getAttribute(SlingBindings.class.getName());
if (bindings != null) {
    request = bindings.getRequest();
}
ResourceBundle resourceBundle = request.getResourceBundle(pageLang );

It seems that the injected request in SlingModels isn't the real request, but just a wrapper that doesn't contain the ResourceBundle. I then tried the @AemObject from wcm.io which also didn't work for me and then I asked in the mailing list what could be the problem.