ResourceBundle resourceBundle = resourceBundleProvider.getResourceBundle(locale); is giving null | Adobe Higher Education
Skip to main content
Prashardan
Level 4
July 21, 2023
Besvarat

ResourceBundle resourceBundle = resourceBundleProvider.getResourceBundle(locale); is giving null

  • July 21, 2023
  • 3 svar
  • 1041 visningar

Hi Team

 

I am using a sling model where I would like to get i18n name of a product but stuck as resourceBundle is giving me null.

 

The below is code snip, Kindly correct me if anything is wrong.

 

imports:

import java.util.Locale;
import java.util.ResourceBundle;
import org.apache.sling.i18n.ResourceBundleProvider;
import javax.inject.Inject;
 
model declaration:
@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
 
@586265
@3214626(target = "(component.name=org.apache.sling.i18n.impl.JcrResourceBundleProvider)")
private ResourceBundleProvider resourceBundleProvider;
 
Locale locale = LocaleUtil.getLocale(resource);
ResourceBundle resourceBundle = resourceBundleProvider.getResourceBundle(locale);
I18n i18n = new I18n(resourceBundle);
 
here resourceBundle is always giving me null and stucked to proceed further. Kindly help me on this.
Det här ämnet har stängts för svar.
Bästa svar av Mahedi_Sabuj

Hi @prashardan,

You can check the following links: 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/i18n-access-from-model-adaptables-resource-class/m-p/254608  

https://www.flexibledesigns.rs/getting-localization-messages-in-sling-models

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class SomeModel { @Self private Resource resource; @Inject private ResourceResolver resourceResolver; @Inject @Filter("(component.name=org.apache.sling.i18n.impl.JcrResourceBundleProvider)") private ResourceBundleProvider i18nProvider; // ... @PostConstruct protected void init() { // ... PageManager pm = resourceResolver.adaptTo(PageManager.class); Page currentPage = pm.getContainingPage(resource); Locale currentLocale = currentPage.getLanguage(true); if(i18nProvider != null) { ResourceBundle bundle = i18nProvider.getResourceBundle(currentLocale); String localizationMessage = bundle.getString("some.key"); // do something with localizationMessage } // ... } }

3 svar

Level 2
July 21, 2023

Hi @prashardan !

 

If you are using ACS Commons in project, you can use something like this.

https://adobe-consulting-services.github.io/acs-aem-commons/features/sling-model-injectors/i18n/index.html

 

Regards,

Shailesh

Mahedi_Sabuj
Community Advisor
Community Advisor
July 21, 2023

Hi @prashardan,

You can check the following links: 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/i18n-access-from-model-adaptables-resource-class/m-p/254608  

https://www.flexibledesigns.rs/getting-localization-messages-in-sling-models

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class SomeModel { @Self private Resource resource; @Inject private ResourceResolver resourceResolver; @Inject @Filter("(component.name=org.apache.sling.i18n.impl.JcrResourceBundleProvider)") private ResourceBundleProvider i18nProvider; // ... @PostConstruct protected void init() { // ... PageManager pm = resourceResolver.adaptTo(PageManager.class); Page currentPage = pm.getContainingPage(resource); Locale currentLocale = currentPage.getLanguage(true); if(i18nProvider != null) { ResourceBundle bundle = i18nProvider.getResourceBundle(currentLocale); String localizationMessage = bundle.getString("some.key"); // do something with localizationMessage } // ... } }
Mahedi Sabuj
DPrakashRaj
Community Advisor
Community Advisor
July 21, 2023

Check this one. You need to first inject it and then use it in @postconstruct as explains on https://www.flexibledesigns.rs/getting-localization-messages-in-sling-models/