[
https://wcm-io.atlassian.net/browse/WCMIO-21?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=10131#comment-10131 ]
Alexander Muthmann commented on WCMIO-21:
-----------------------------------------
Currently it does not seem to be possible to use the i18n class with a request as source.
SlingHttpServletRequestImpl always returns a NullResourceBundle, no matter which languages/translations are available.
A solution is to use the ResourceBundleProvider-Service and get a ResourceBundle from it.
By default an implementation "PseudoTranslation" has the service-ranking 1000, the Sling implementation "org.apache.sling.i18n.impl.JcrResourceBundleProvider" has no service-ranking defined. Therefor it is required to either iterate over all Services or load the Sling implementation. If I understand the documentation correctly, the interface should not be implemented by another service:
{quote}This service interface is not intended to be implemented outside of the sling.i18n bundle{quote}
To get an instance of i18n for the language of the currentPage, this code works fine:
{code}
SlingBindings bindings = (SlingBindings)request.getAttribute(SlingBindings.class.getName());
SlingScriptHelper scriptHelper = bindings.getSling();
ResourceBundleProvider bundleProvider = scriptHelper.getServices(ResourceBundleProvider.class, "(
component.name=org.apache.sling.i18n.impl.JcrResourceBundleProvider)")[0];
ResourceBundle resourceBundle = bundleProvider.getResourceBundle(currentPage.getLanguage(true));
I18n i18n = new I18n(resourceBundle);
{code}