1
votes

I wrote a portlet for Liferay in Java, Jquery and Javascript.

I am trying to get the language of the userprofil that is using my portlet.

I already got the uuid and can use it.

If i try to get the language the same way i receive null.

My JavaClass:

public class UserId extends MVCPortlet {
    public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
        ThemeDisplay td  =(ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
        User user = td.getUser();
        /*String userLanguage = td.getLocale().toString();*/
        /*String userLanguage = user.getLanguageId();*/
        String userLanguage = LanguageUtil.getLanguageId(renderRequest); 
        String userId = user.getUuid();
        renderRequest.setAttribute("myUser", userId);   
        renderRequest.setAttribute("myUserLang", userLanguage); 
        super.doView(renderRequest, renderResponse);
    }
}

As you can see i tried to get userLanguage with three different methods.

My View.jsp:

<portlet:defineObjects />
<%
    String userIdIntern = renderRequest.getRemoteUser();
    String userIdExtern = (String) request.getAttribute("myUser");
    String userLang = (String) request.getAttribute("myUserLang");
%>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

<script>
    $(document).ready(function(){
        userId = "<%=userIdExtern%>";
        debug(userId);
        var testLang = "<%=userLang%>";
        debug("testLang obj: "+testLang); // returns null
    });
/* more code below */

If i use td.getLocale() i at least get an object instead of null. Using .toString() returns null again.

How can i get the currently used language for an userprofile?

1
did you try printing user.getLocale() ?Ankit P
oh wow it just worked now after i completely removed it from server and readded it. I now receive "en_GB" as expected. i will test this further and close my question then. thanks for your help though! PS: i printed getlocale to a textarea (this happens in my debug() method)Wandang

1 Answers

0
votes

The code was correct. I just needed to remove the portlet from the server and readd it. This is probably because Java wont execute correctly otherwise. (Therefore my languagevar returned null)

Now everything works as expected.