Esta conversación ha sido bloqueada debido a la inactividad. Cree una nueva publicación.
Nivel 1
Nivel 2
Iniciar sesión en la comunidad
Iniciar sesión para ver todas las insignias
Esta conversación ha sido bloqueada debido a la inactividad. Cree una nueva publicación.
I need to access the translations library from my Model - Resource.class
I manage to do it within a Model - SlingHttpServletRequest.class
To instantiate i18n I need either the slingHttpServletRequest, or the resourceBundle but, from within my humble resource class,
I am not being able to get any of them.
I tried with @Model(adaptables= {Resource.class, SlingHttpServletRequest.class}), but when I try to inject the request the my page does not render.
Does any of you manages to access i18n from a @model Resource.class?
Any help would be greatly appreciated!
Vistas
Respuestas
Total de me gusta
following snippet might help
@Model(adaptables = {SlingHttpServletRequest.class, Resource.class})
public class SimpleModel {
@Self(injectionStrategy = InjectionStrategy.OPTIONAL)
private SlingHttpServletRequest request;
private I18n i18n;
if (request != null) {
i18n=new I18n(request);
String val =i18n.getVar(“key”);
}
}
Vistas
Respuestas
Total de me gusta
I actually tried that, but the request never gets filled.
I will try to see if I can make it work, and come back if so.
Thanks for the help!
Vistas
Respuestas
Total de me gusta
Hey
you can give a try in another way like
final Locale pageLocale = getCurrentPage().getLanguage(false);
final ResourceBundle resourceBundle = getRequest().getResourceBundle(pageLocale);
final I18n i18n = new I18n(resourceBundle);
Vistas
Respuestas
Total de me gusta
HI guys either way it should work
Vistas
Respuestas
Total de me gusta
Well, in the, and since adapting to both didn't work for me,
we made the model
@Model(adaptables = SlingHttpServletRequest.class)
and injected everything else
@Inject
private ResourceResolver resolver;
@Inject
private Resource resource;
@Inject
@Optional
private I18n i18n;
Thanks for the suport!
Vistas
Respuestas
Total de me gusta
Did the above snippet work ? is request getting injected ?
Vistas
Respuestas
Total de me gusta
Yes it got Injected.
But I actually, today, got a better way of getting it, without the need to adapt from slinghttpservletrequest
@Model(adaptables = Resource.class)
public class MyClassMode {
@Inject
@Filter("(component.name=org.apache.sling.i18n.impl.JcrResourceBundleProvider)")
private ResourceBundleProvider i18nProvider;
}
Vistas
Respuestas
Total de me gusta
Vistas
me gusta
Respuestas
Vistas
me gusta
Respuestas
Vistas
me gusta
Respuestas