Expand my Community achievements bar.

SOLVED

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

Avatar

Level 4

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)
 
@inject
@reference(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.
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Prashardan,

You can check the following links: 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/i18n-access-from-model-ada...  

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
    }
 
    // ...
  }
}

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

Hi @Prashardan,

You can check the following links: 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/i18n-access-from-model-ada...  

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
    }
 
    // ...
  }
}

Avatar

Community Advisor

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/