2

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 ?

Kiesa
  • 407
  • 1
  • 19
  • 42

3 Answers3

1

Another possibility is:

VaadinSession.getCurrent().getLocale()

This does not come from the users browser but reflects the user setting in liferay.

daniel-sc
  • 1,139
  • 11
  • 23
1

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);
Martin Gamulin
  • 3,855
  • 21
  • 25
  • call me noob , but i have no idea about requests, i know that i cant put it in the init method but where then ? in the public void onRequestStart() method ? and how do i print out the language id i get (if it works). – Kiesa Nov 17 '11 at 07:25
  • i have tryed it but the onRequestStart method is not being called at all only the init method. Any ideas ? maybe i should use HTTPRequest ? – Kiesa Nov 17 '11 at 11:31
  • tryed it with HTTP Request it works, i get the current users Language id but i cant see the content of my portlet i get the following error : Internal error Please notify the administrator. Take note of any unsaved data, and click here to continue. – Kiesa Nov 17 '11 at 12:07
  • it works , my problem was in portlet.xml as you said. I have created an LiferayProject with vaadin plugin for eclipse. I got an example vaadin portlet but as i looked in to the portlet.xml i saw com.liferay.util.bridges.mvc.MVCPortlet .... not com.vaadin.terminal.gwt.server.ApplicationPortlet2 as it should have been ;/ thx for your help +1 + accept – Kiesa Nov 17 '11 at 13:21
  • hey, do you know how to read out the Language.properties files with vaadin ? im having some problems there too. – Kiesa Nov 18 '11 at 07:25
  • From where do you try to read properties? Java code? Try with com.liferay.portal.kernel.language.LanguageUtil.get(locale, key); – Martin Gamulin Nov 18 '11 at 08:36
  • You can get locale from user. so user.getLocale(); – Martin Gamulin Nov 18 '11 at 08:37
  • i've added an Edit at the bottom of my question – Kiesa Nov 18 '11 at 09:03
  • post the EDIT 3 to my other question Link : [link](http://stackoverflow.com/questions/8121638/vaadin-multilanguage-portlet-component-captions) so i can accept it :) – Kiesa Nov 18 '11 at 10:06
0

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:

  1. handleActionRequest
  2. handleEventRequest
  3. handleRenderRequest
  4. handleResourceRequest

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.

portletApplicationContext2

eMber
  • 33
  • 4