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:
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @Prashardan,
You can check the following links:
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
}
// ...
}
}
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/inde...
Regards,
Shailesh
Hi @Prashardan,
You can check the following links:
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
}
// ...
}
}
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/