Expand my Community achievements bar.

reading 18n values from servlet.

Avatar

Level 2

Hey , 

I need to read i18n values from my java code , I am able to do so via this piece of code 

if (request != null) {
i18n = new I18n(request);
textLoadMore = i18n.getVar(LOAD_MORE);
}

but this works when it is called from sling models but from servlet my request do not contain locale and language info hence its fetching en for all the locales.

can you help me to find a way to do so , also i have locale info in my servlet.

Regards,


Mohit

2 Replies

Avatar

Community Advisor

Hi @Mohit3 ,

You may either pass language as request parameter to the servlet or if possible retrieve current page to get i18n object like below

Page currentPage = resourceResolver.getResource(currentPagePath).adaptTo(Page.class);//optional, you may directly pass lang value
ResourceBundle bundle = request.getResourceBundle(currentPage.getLanguage());
I18n i18n = new I18n(bundle);

Or something like below

PageManager pageManager = request.getResourceResolver().adaptTo(PageManager.class);
Resource resource = request.getResource();
Locale currentLocale = null;
if(pageManager != null) {
Page page = pageManager.getContainingPage(resource);
currentLocale = page.getLanguage();
}
ResourceBundle bundle = request.getResourceBundle(currentLocale);
I18n i18n = new I18n(bundle);

Thanks