Preserving the Language preference after user logout

Context : In projects which support multiple language end user faces common issue related to language switch based on the browser locale once user is logged out from his/her account which leads to bad or broken user experience for the end user.

Problem Details : We created portal for France and our portal supports 2 languages – English and French. Default site language is French. When I logged into this website, I selected my language as English but after logout I will be landing on the page with content in French.

Solution : In Liferay, we can update the page content’s language by passing the language id in URL as well. For example, I have 2 languages in my portal – English and French. If I pass “en” in URL, page content will be displayed in English language and if I pass “fr” in URL, page content will be displayed in French language.

To solve our problem, we have to write PostLogout Event. In PostLogout event, we will check the user’s selected language and based on user’s selected language, we will append the language id of user’s selected language in Logout Page URL and will call the sendRedirect function with that URL to redirect to updated logout URL.

If user’s portal’s default language is English and after login user has selected French language then after logout, user will be able to see the page content in French language only not in English because we have added user’s selected language(“fr”) in logout URL. We are not using any cookies concept here. So, if you close the browser and open the site again, page content will display in portal’s default language.

Please find below the code of PostLogoutEvent-

import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.events.LifecycleAction;
import com.liferay.portal.kernel.events.LifecycleEvent;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import com.liferay.portal.kernel.service.PortalPreferencesLocalServiceUtil;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.kernel.util.PortletKeys;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;

import java.util.Locale;

import javax.portlet.PortletPreferences;
import javax.servlet.http.HttpServletRequest;

import org.osgi.service.component.annotations.Component;

@Component(immediate = true, property = { "key=logout.events.post" }, service = LifecycleAction.class)
public class PostLogoutEvent extends MVCPortlet implements LifecycleAction {

    private static final Log _log = LogFactoryUtil.getLog(PostLogutEvent.class);
    private static final String PAGE_LOGOUT_INSTANCE_SETTING = "default.logout.page.path";

    @Override
    public void processLifecycleEvent(LifecycleEvent lifecycleEvent) throws ActionException {
        String logoutURL = "";
        try {
            HttpServletRequest request = lifecycleEvent.getRequest();
            User user = PortalUtil.getUser(request);
            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);

            // If logout page URL is entered in instance settings section then only we have to use this code.
            PortletPreferences portalPreferences = PortalPreferencesLocalServiceUtil.getPreferences(themeDisplay.getCompanyId(), PortletKeys.PREFS_OWNER_TYPE_COMPANY);
            if(portalPreferences.getMap().containsKey(PAGE_LOGOUT_INSTANCE_SETTING)) {
                if(portalPreferences.getMap().get(PAGE_LOGOUT_INSTANCE_SETTING).length > 0) {
                    logoutURL = portalPreferences.getMap().get(PAGE_LOGOUT_INSTANCE_SETTING)[0];
                }
            }
            
            if (Validator.isNotNull(user)
                    && LocaleUtil.toLanguageId(Locale.FRANCE).equalsIgnoreCase(user.getLanguageId())) {
                lifecycleEvent.getResponse().sendRedirect("/" + LocaleUtil.FRENCH.getLanguage() + logoutURL);
            }
        } catch (Exception ex) {
            _log.error("Error Occurred in PostLogout Event:", ex);
        }

    }
}

 

Blogs

Thanks for posting this blog. It helps me to achieve the project requirement.