Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Paul Schmidt
Changing user language instantly
November 2, 2012 12:19 AM
Answer

Paul Schmidt

Rank: Junior Member

Posts: 34

Join Date: July 28, 2010

Recent Posts

Hello Community,

there are two ways to change users display language.
Control Panel --> My Account --> Display Settings
If I change the language there, it's instantly changed and everything has the selected language.

Programmatically with:
1
2user = UserServiceUtil.getUserById(Long.parseLong(request.getUserPrincipal().getName()));
3user.setLanguageId(selectedLanguage);
4UserLocalServiceUtil.updateUser(user);


This method works fine and persists the new user language. You can check it, when you have a look at control panel

The problem is: The portal is displayed with the selected language only after relogin! How can I achieve a portal reload with the new language settings?

LR 6.1.0 GA1 bundled with TomCat

BR Paul
Gaurav Jain
RE: Changing user language instantly
November 2, 2012 3:21 AM
Answer

Gaurav Jain

Rank: Regular Member

Posts: 146

Join Date: January 31, 2011

Recent Posts

Have you also tried method:
updateUser(User user, boolean merge)

http://docs.liferay.com/portal/6.1/javadocs/com/liferay/portal/service/UserLocalServiceUtil.html#updateUser(com.liferay.portal.model.User, boolean)
Parameters:
user - the user
merge - whether to merge the user with the current session. See BatchSession.update(com.liferay.portal.kernel.dao.orm.Session, com.liferay.portal.model.BaseModel, boolean) for an explanation.
Hitoshi Ozawa
RE: Changing user language instantly
November 2, 2012 5:58 AM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 7949

Join Date: March 23, 2010

Recent Posts

That's because you're changing the language saved to the user and not changing the current language variable in the themeDisplay.setLanguageId(String languageId)
Paul Schmidt
RE: Changing user language instantly
November 5, 2012 8:33 AM
Answer

Paul Schmidt

Rank: Junior Member

Posts: 34

Join Date: July 28, 2010

Recent Posts

Thank you for the answers,

unfortunately both solutions didn't made the trick.

I debugged into my sources.
On first request
themeDisplay.getLanguageId() is en_US

I change the Language:
themeDisplay.setLanguageId(de_DE);

but on next click in portal
themeDisplay.getLanguageId() is en_US again

Why?
Vitaliy Koshelenko
RE: Changing user language instantly
November 5, 2012 1:57 PM
Answer

Vitaliy Koshelenko

Rank: Expert

Posts: 284

Join Date: March 25, 2011

Recent Posts

Hi!
You can also change the current language programmatically by changing the locale:

1String languageId = "en_US";
2Locale locale = LocaleUtil.fromLanguageId(languageId);
3session.setAttribute(org.apache.struts.Globals.LOCALE_KEY, locale);   


This should change the current language immediately.

Vitaliy