Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Accessing I18n strings on backend

Avatar

Level 2

Hello folks. Hope you are doing great.

I'm having a weird problem with I18n object I created to get the translated strings before rendering my htl template. For example, I'm doing something like this:

var i18n = new Packages.com.day.cq.i18n.I18n(request);

var translated = i18n.get('Some string in my dictionary')

I have configured the language dictionaries properly and created a Spanish copy of the page I'm working on, but I always get the English text on the `translated` variable

I know that the translations are working because if in the HTL I write something like this

${'Some string in my dictionary' @ i18n}

then I can see the Spanish version of the text in the html, so the dictionary is well configured.

Do I need to do anything else to get the translated string in the backend??

Thanks

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

try with ResourceBundle e.g.

Locale pageLang = currentPage.getLanguage(false);

ResourceBundle resourceBundle = slingRequest.getResourceBundle(pageLang);

I18n i18n = new I18n(resourceBundle);



Arun Patidar

View solution in original post

4 Replies

Avatar

Community Advisor

could be problem with request object.

I18n i18n = new I18n(slingRequest);

The constructor uses the SlingHTTPRequest to retrieve the user's language setting.

more info - Internationalizing UI Strings



Arun Patidar

Avatar

Level 2

Hi, thanks for answering. The problem is that according to this: HTL Global Objects  "request" is an instance of org.apache.sling.api.SlingHttpServletRequest So I think it is the correct object

Avatar

Correct answer by
Community Advisor

Hi,

try with ResourceBundle e.g.

Locale pageLang = currentPage.getLanguage(false);

ResourceBundle resourceBundle = slingRequest.getResourceBundle(pageLang);

I18n i18n = new I18n(resourceBundle);



Arun Patidar

Avatar

Level 2

Yes. That worked. Thanks for the help