Expand my Community achievements bar.

Introducing Adobe LLM Optimizer: Own your brand’s presence in AI-Powered search and discovery
SOLVED

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

1 Accepted Solution

Avatar

Correct answer by
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

View solution in original post

2 Replies

Avatar

Correct answer by
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

Avatar

Community Advisor

Hi @Mohit3 

 

Please find below documentation for the same -

 

https://sling.apache.org/documentation/bundles/internationalization-support-i18n.html#sample-resourc...

 

Hope this helps ! 

 

Thanks