我需要检查我的portlet,用户选择哪种语言作为他的“主要”语言,为此,我必须首先获得UserID(名称)。我一直在寻找它两天(Liferay论坛,vaadin论坛,stackoverflow等),但到目前为止没有找到任何工作。
我找到了一个很好的例子,但它似乎无法工作(它总是返回“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.
}
}
编辑:
这是我到目前为止所尝试的:
locale = user.getLocale();
button.setCaption(LanguageUtil.get(locale, "first_name"));
在我的Language.properties中,我将“first_name”的翻译设置为1st Name:
first_name=1st Name
Language.properties文件位于我添加的内容文件夹中,资源包也添加到我的portlet.xml中:
<resource-bundle>content/Language</resource-bundle>
按钮的标题设置为“first_name”而不是第一个名称,如果我将密钥更改为名字,我将从language.properties文件中获取默认翻译而不是我的翻译,我错过了什么?
答案 0 :(得分:1)
你试过
吗?final ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKey.THEME_DISPLAY);
themeDisplay.getUser().getLanguageId();
需要进口
import javax.portlet.PortletRequest;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
编辑:
试试这个
@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);
}
您也可以尝试
@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:
这是适合我的完整示例。尝试一下,如果它不起作用,请显示你的portlet.xml,web.xml和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:
要从您的属性文件中获取标题,您可以尝试(假设上面的类)
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);
答案 1 :(得分:1)
另一种可能性是:
VaadinSession.getCurrent().getLocale()
不来自用户浏览器,但反映了liferay中的用户设置。
答案 2 :(得分:0)
如果您在liferay中的portletContext中运行vaadin应用程序,则需要在此处监听portletlisteners,这是指向example的链接。
实现监听器时,你需要实现监听器:
当启动应用程序时,所以运行MainApplication init()方法,并在u(init)监听方法后自动运行侦听器方法。 handleRenderRequest方法你可以调用request.getRemoteUser()。它返回数字编号,即远程用户ID,或者在任何编码中没有复制时返回null。