Foren

Ability to Switch/Change language/locale in JSF 2.0 Backing Bean portlet

thumbnail
Johann Kneringer, geändert vor 11 Jahren.

Ability to Switch/Change language/locale in JSF 2.0 Backing Bean portlet

Junior Member Beiträge: 42 Beitrittsdatum: 10.11.11 Neueste Beiträge
Hey,

I'm looking for the ability to change the locale in a jsf 2.0 portlet in a method in a backing bean...

I am able to get the ThemeDisplay and I can read the already used locale.

I also tried this, but nothing happens:

themeDisplay.setLocale(locale);

Is there a possibility to change the language/locale in this way?

Regards from Austria.
thumbnail
Neil Griffin, geändert vor 11 Jahren.

moved thread to Liferay Faces forum

Liferay Legend Beiträge: 2655 Beitrittsdatum: 27.07.05 Neueste Beiträge
moved thread to Liferay Faces forum
thumbnail
Neil Griffin, geändert vor 11 Jahren.

RE: Ability to Switch/Change language/locale in JSF 2.0 Backing Bean portle

Liferay Legend Beiträge: 2655 Beitrittsdatum: 27.07.05 Neueste Beiträge
In order for the locale change to stick with subsequent requests, it has to be done in a persisted way. I recommend that you try something like this:

User user = themeDisplay.getUser();
user.setLocale(locale);
UserLocalServiceUtil.updateUser(user);
thumbnail
Johann Kneringer, geändert vor 11 Jahren.

RE: Ability to Switch/Change language/locale in JSF 2.0 Backing Bean portle

Junior Member Beiträge: 42 Beitrittsdatum: 10.11.11 Neueste Beiträge
Neil Griffin:
In order for the locale change to stick with subsequent requests, it has to be done in a persisted way. I recommend that you try something like this:

User user = themeDisplay.getUser();
user.setLocale(locale);
UserLocalServiceUtil.updateUser(user);


thanks for your reply Neil!

Unfortunatly there is no implemented Method setLocale(local) for the User (com.liferay.portal.model.User)

Such a method exists only in the ThemeDisplay...Is there a way to make an update on the ThemeDisplay?

So first:

PortletRequest portletRequest = (PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
themeDisplay = (ThemeDisplay)portletRequest.getAttribute(WebKeys.THEME_DISPLAY);
themeDisplay.setLocale(locale);
Now we might need to perform an update of the themeDisplay...
thumbnail
Neil Griffin, geändert vor 11 Jahren.

moved thread to Liferay Faces forum

Liferay Legend Beiträge: 2655 Beitrittsdatum: 27.07.05 Neueste Beiträge
moved thread to Liferay Faces forum
thumbnail
Neil Griffin, geändert vor 11 Jahren.

RE: Ability to Switch/Change language/locale in JSF 2.0 Backing Bean portle

Liferay Legend Beiträge: 2655 Beitrittsdatum: 27.07.05 Neueste Beiträge
Oops, sorry about that. emoticon Try something like this instead:

User user = themeDisplay.getUser();
String languageId = LocaleUtil.toLanguageId(locale);
user.setLanguageId(languageId);
UserLocalServiceUtil.updateUser(user);
thumbnail
Johann Kneringer, geändert vor 11 Jahren.

RE: Ability to Switch/Change language/locale in JSF 2.0 Backing Bean portle

Junior Member Beiträge: 42 Beitrittsdatum: 10.11.11 Neueste Beiträge
Neil Griffin:
Oops, sorry about that. emoticon Try something like this instead:

User user = themeDisplay.getUser();
String languageId = LocaleUtil.toLanguageId(locale);
user.setLanguageId(languageId);
UserLocalServiceUtil.updateUser(user);


I tried this, but there is no affect...

If I get the user with User user = themeDisplay.getUser();, the property languageID on the user is already "de_DE", but it should be "en_US", because I want to change it to "de_DE".

I have to say that I have this portlet on a page with the language portlet on top, and I am able to change the language of the portlet.

In my backing bean I want to change the language of this page, where the protlet is embeded. I guess that the languageID of the user is the default language that is set by the user itself....But I want to change the language of the Portal-Page, or the Language Portlet itself...

Any other ideas?

Thanks anyways Neil....
thumbnail
Neil Griffin, geändert vor 11 Jahren.

RE: Ability to Switch/Change language/locale in JSF 2.0 Backing Bean portle

Liferay Legend Beiträge: 2655 Beitrittsdatum: 27.07.05 Neueste Beiträge
Let me restate your requirement, and let me know if I understand it correctly: You want to be able to set the language/locale of individual portal pages, meaning all portlets on the page would be rendered according to a particular language/locale.
thumbnail
Johann Kneringer, geändert vor 11 Jahren.

RE: Ability to Switch/Change language/locale in JSF 2.0 Backing Bean portle

Junior Member Beiträge: 42 Beitrittsdatum: 10.11.11 Neueste Beiträge
Neil Griffin:
Let me restate your requirement, and let me know if I understand it correctly: You want to be able to set the language/locale of individual portal pages, meaning all portlets on the page would be rendered according to a particular language/locale.


Yes. This is correct,

In my case it would also be ok if I would be able to render a particular portlet to a particular language.
thumbnail
Neil Griffin, geändert vor 11 Jahren.

RE: Ability to Switch/Change language/locale in JSF 2.0 Backing Bean portle

Liferay Legend Beiträge: 2655 Beitrittsdatum: 27.07.05 Neueste Beiträge
I would try adding a Hook to your portlet that provides a custom ServicePreAction. The out-of-the-box ServicePreAction has a method named initThemeDisplay() that initializes the ThemeDisplay for the request. I recommend that you study that for a coding example of how to set the language in the ThemeDisplay. Your custom ServicePreAction could check the name (or perhaps a custom attribute) of the current Layout (portal page) and only set the language in the ThemeDisplay accordingly. The custom attribute approach might work well, because you could simply set "en_US" as the attribute value on the pages that you want to always be rendered in US English.

The Portal Hook wiki article is a little out-of-date. Rather than creating an <event> element, you'll need to create a <portal-properties> element that points to a portal.properties file. Inside that file, you would need to add a servlet.service.events.pre entry that points to the FQCN of your custom ServicePreAction class. Liferay IDE makes this very easy. You can also refer to liferay-hook_6_0_0.dtd for more information.
thumbnail
Johann Kneringer, geändert vor 11 Jahren.

RE: Ability to Switch/Change language/locale in JSF 2.0 Backing Bean portle

Junior Member Beiträge: 42 Beitrittsdatum: 10.11.11 Neueste Beiträge
I will try this, thank you.

But I guess that you mean that I should use a ServicePostAction instead of a ServicePreAction, because the language/local should be (re)setted in a backing bean in a actionlistener.

regards
thumbnail
Neil Griffin, geändert vor 11 Jahren.

RE: Ability to Switch/Change language/locale in JSF 2.0 Backing Bean portle

Liferay Legend Beiträge: 2655 Beitrittsdatum: 27.07.05 Neueste Beiträge
Actually I would try ServicePreAction because the out-of-the-box portal's ServicePreAction class sets up the ThemeDisplay/Locale/Language and I think you would need to override that.
thumbnail
Johann Kneringer, geändert vor 11 Jahren.

RE: Ability to Switch/Change language/locale in JSF 2.0 Backing Bean portle

Junior Member Beiträge: 42 Beitrittsdatum: 10.11.11 Neueste Beiträge
Got it, thanks Neil.

Actually its pretty easy.

Here is my solution:

Create a liferay hook with a ServicePreAction event with LiferayIDE.

Your custom class has to extend com.liferay.portal.kernel.events.Action, by defaulte LiferayIDE extends a SimpleAction which leads to a classcast error.

in your custom class you override the run method and you check a custom attribute, if the language should get changed like this:

@Override
public void run(HttpServletRequest request, HttpServletResponse arg1)throws ActionException
{

if(request.getAttribute("myCustomLanguage")!=null)
{
Locale locale = (Locale)request.getAttribute("myCustomLanguage");
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
themeDisplay.setLocale(locale);
request.setAttribute(WebKeys.THEME_DISPLAY, themeDisplay);
}
}


Now what I did in my backing bean:



String sprache = "";
Locale locale = null;
if(spracheset!=false)
{
//Getting the language from a model
sprache = syllabus.getLvsprache();
}

if(sprache.equals("Englisch") || sprache.equals("English"))
{
locale = Locale.US;
}
else
{
locale = Locale.GERMANY;
}

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
HttpServletRequest request = PortalUtil.getHttpServletRequest(portletRequest);

PortletResponse portletResponse = (PortletResponse)externalContext.getResponse();
HttpServletResponse response = PortalUtil.getHttpServletResponse(portletResponse);

//Setting the custom Attribute to the request
request.setAttribute("myCustomLanguage", locale);

//Load ServicePreAction to renew the ThemeDisplay
ServicePreAction servicePreAction = new ServicePreAction();
try {
servicePreAction.run(request, response);
} catch (ActionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
thumbnail
Neil Griffin, geändert vor 11 Jahren.

RE: Ability to Switch/Change language/locale in JSF 2.0 Backing Bean portle

Liferay Legend Beiträge: 2655 Beitrittsdatum: 27.07.05 Neueste Beiträge
Rather than having a backing-bean, I would think that this could be done entirely with a ServicePreAction.

The instantiation of the ServicePreAction is done automatically by the portal -- in other words, you shouldn't have to do this:

new ServicePreAction();


I recommend that you check the portal.properties file that Liferay IDE created for you. It should have automatically registered your ServicePreAction class there.

The ServicePreAction is supposed to execute before the portal page is processed and before the portlet lifecycle is executed or any of the portlets invoked. So the backing-bean action would seem to execute too late, since the JSF lifecycle of a portlet happens very late in the game.
thumbnail
Johann Kneringer, geändert vor 11 Jahren.

RE: Ability to Switch/Change language/locale in JSF 2.0 Backing Bean portle

Junior Member Beiträge: 42 Beitrittsdatum: 10.11.11 Neueste Beiträge
Neil Griffin:

I recommend that you check the portal.properties file that Liferay IDE created for you. It should have automatically registered your ServicePreAction class there.


This is working correctly...

I have a method (void methodname(ActionEvent event) which gets triggered from a commandLinks actionListener.

The page is already loaded when the language has to be changed.

So maybe it should be a ServicePostAction instead of a ServicePreAction. I've changed that, but unfortunatly I never get my custom attribute in the HttpServletRequest, if I dont make an instance manually in my backing bean.


All in all, i found another (easier) solution.

You can update the themeDisplay via the PortletRequest,..


        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
        portletRequest.setAttribute(WebKeys.THEME_DISPLAY, themeDisplay);


I've tried this before, but it never worked, because there was an error with my locale...

The way that did not work was:


Locale locale = new Locale("de_DE");


The way it worked for me:

locale = Locale.GERMANY;