【发布时间】:2011-11-16 08:45:23
【问题描述】:
我需要在我的 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”的翻译设置为第一个名称:
first_name=1st Name
Language.properties 文件位于我添加的内容文件夹中,并且资源包也包含在我的 portlet.xml 中:
<resource-bundle>content/Language</resource-bundle>
按钮的标题设置为“名字”而不是名字,如果我将键更改为名字,我会从 language.properties 文件中获得默认翻译,而不是我的翻译,我是否遗漏了什么?
【问题讨论】:
-
LanguageUtil 将从 liferay 属性文件中获取属性。我会编辑答案。