Expand my Community achievements bar.

SOLVED

Using I18N inside a Transformer class

Avatar

Level 3

I'm trying to translate text inside a Transformer class.  The page's locale is coming out at de but I'm never getting the German string.  My i18n libraries are correct because I can get the translated string out via the groovy console.  Any idea why the resourceBundle isn't loading the translations?

 

... class TranslationTransformer extends AbstractSAXPipe implements Transformer { ... @Override public void init(final ProcessingContext context, final ProcessingComponentConfiguration config) throws IOException { this.request = context.request Page page = this.request.resource.parent.adaptTo(Page.class) if (page) { Locale locale = page.getLanguage(false) ResourceBundle resourceBundle = this.request.getResourceBundle(locale) this.i18n = new I18n(resourceBundle) LOG.debug("... Locale is $locale") String temp = "Contact Us" String output = i18n.getVar(temp) LOG.debug("... Contact Us is $output") } } ... }
1 Accepted Solution

Avatar

Correct answer by
Level 3

The solution was to inject a ResourceBundle Provider then use that to find the resourceBundle.  The request wouldn't find it for some reason.

 

@Reference(target="(component.name=org.apache.sling.i18n.impl.JcrResourceBundleProvider)")private ResourceBundleProvider resourceBundleProvide
ResourceBundle resourceBundle = resourceBundleProvider.getResourceBundle(locale)

View solution in original post

3 Replies

Avatar

Level 10

are you using the same name for the page locale and i18N locale

ex: If you have create a language node called 'de' in page, i18N should also have a same node name 'de' and not de_DE

Avatar

Level 3

I've been banging my head on this for a while.  Our i18n node is just 'de' not de_DE.  I believe the issue is with the request coming from the ProcessingContext.  I've moved the code above to a servlet and it works just fine.

Avatar

Correct answer by
Level 3

The solution was to inject a ResourceBundle Provider then use that to find the resourceBundle.  The request wouldn't find it for some reason.

 

@Reference(target="(component.name=org.apache.sling.i18n.impl.JcrResourceBundleProvider)")private ResourceBundleProvider resourceBundleProvide
ResourceBundle resourceBundle = resourceBundleProvider.getResourceBundle(locale)