Reputation: 407
I need to check in my portlet wich language does an user have selected as his "main" language, to do that, I have to get the UserID (name) first . i have been looking for it for two days (Liferay forums , vaadin forums , stackoverflow etc.) but nothing found that would work so far.
I have found an nice example but it doesnt seem to work (It always returns "null").
package com.example.translation_portlet;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.util.PortalUtil;
import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.PortletRequestListener;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;
public class Translation_portletApplication extends Application implements
PortletRequestListener {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void init() {
Window mainWindow = new Window("LoginApplication");
Label label = new Label("Hello anonymous Vaadin user");
if (getUser() != null) {
// user has logged in
label = new Label("Hello " + ((User) getUser()).getFullName());
}
mainWindow.addComponent(label);
setMainWindow(mainWindow);
}
@Override
public void onRequestStart(PortletRequest request, PortletResponse response) {
if (getUser() == null) {
try {
User user = PortalUtil.getUser(request);
setUser(user);
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}
}
}
@Override
public void onRequestEnd(PortletRequest request, PortletResponse response) {
// Nothing to do here currently, exists only to implement the
// PortletRequestListener interface.
}
}
EDIT :
this is what i have tryed so far :
locale = user.getLocale();
button.setCaption(LanguageUtil.get(locale, "first_name"));
and in my Language.properties i have the translation for "first_name" set to 1st Name:
first_name=1st Name
the Language.properties file is located in my content folder i have added and resource-bundle to my portlet.xml too :
<resource-bundle>content/Language</resource-bundle>
The caption of the button is set to "first_name" not 1st name , if i change the key to first-name i get an default translation no my tranlsation from the language.properties file , am i missing something ?
Upvotes: 2
Views: 5488
Reputation: 1307
Another possibility is:
VaadinSession.getCurrent().getLocale()
This does not come from the users browser but reflects the user setting in liferay.
Upvotes: 1
Reputation: 33
If u run a vaadin application in portletContext in liferay, u need to listening a portletlisteners here's an link to example.
When implemented the listener u need implemened the listener:
When started the application, so run the MainApplication init() method, and u listening to portlet listenert, after the init() method automatic run the listeners methods. The handleRenderRequest method u can call the request.getRemoteUser(). It return the digital number, thats a remote user ID, or null when no singed in anybodey.
Upvotes: 0
Reputation: 3865
Did you try with
final ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKey.THEME_DISPLAY);
themeDisplay.getUser().getLanguageId();
Imports needed are
import javax.portlet.PortletRequest;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
EDIT:
Try with this
@Override
public void onRequestStart(PortletRequest request, PortletResponse response) {
final ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKey.THEME_DISPLAY);
final User user = themeDisplay.getUser();
if (user != null) {
// will be printed to log/console
System.out.println("User's language id = " + user.getLanguageId());
} else {
System.out.println("Guest user.");
}
setUser(user);
}
You can also try
@Override
public void init() {
Window mainWindow = new Window("LoginApplication");
Label label = new Label("Hello anonymous Vaadin user");
if (getUser() != null) {
// user has logged in
label = new Label("Hello " + ((User) getUser()).getFullName() + ", language id is '" + user.getLanguageId() + "'");
}
mainWindow.addComponent(label);
setMainWindow(mainWindow);
}
EDIT2:
This is complete example that works for me. Try it, if it does not work for you please show your portlet.xml, web.xml and liferay-portlet.xml
package com.test;
import java.util.Iterator;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.portal.theme.ThemeDisplay;
import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.PortletRequestListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;
public class Translation_portletApplication extends Application implements PortletRequestListener {
private User m_user;
@Override
public void init() {
Window window = new Window("Vaadin Portlet Application");
setMainWindow(window);
String caption = "Hello Vaadin user!";
if (m_user != null) {
caption = caption + " with language id '" + m_user.getLanguageId() + "'";
}
window.addComponent(new Label(caption));
}
@Override
public void onRequestEnd(PortletRequest p_request, PortletResponse p_response) {
System.out.println("onRequestEnd");
}
@Override
public void onRequestStart(PortletRequest p_request, PortletResponse p_response) {
final ThemeDisplay themeDisplay = (ThemeDisplay) p_request.getAttribute(WebKeys.THEME_DISPLAY);
m_user = themeDisplay.getUser();
}
}
EDIT3:
For getting caption from your property files you can try with (assuming above class)
Button button = new Button() {
@Override
public void attach() {
ResourceBundle bundle = ResourceBundle.getBundle(Translation_portletApplication.class.getName(), user.getLocale());
setCaption(bundle.getString("first_name"));
}
};
window.addComponent(button);
Upvotes: 1