diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/settings/entities/Language.java b/navalplanner-business/src/main/java/org/navalplanner/business/settings/entities/Language.java index 924589000..8730921f4 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/settings/entities/Language.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/settings/entities/Language.java @@ -21,6 +21,8 @@ package org.navalplanner.business.settings.entities; import static org.navalplanner.business.i18n.I18nHelper._; +import java.util.Locale; + /** * Available languages. * @@ -29,19 +31,26 @@ import static org.navalplanner.business.i18n.I18nHelper._; */ public enum Language { - BROWSER_LANGUAGE(_("Use browser language configuration")), - GALICIAN_LANGUAGE(_("Galician")), - SPANISH_LANGUAGE(_("Spanish")), - ENGLISH_LANGUAGE(_("English")); + BROWSER_LANGUAGE(_("Use browser language configuration"), null), + GALICIAN_LANGUAGE(_("Galician"), new Locale("gl")), + SPANISH_LANGUAGE(_("Spanish"), new Locale("es")), + ENGLISH_LANGUAGE(_("English"), Locale.ENGLISH); private final String displayName; - private Language(String displayName) { + private Locale locale; + + private Language(String displayName, Locale locale) { this.displayName = displayName; + this.locale = locale; } public String getDisplayName() { return displayName; } + public Locale getLocale() { + return locale; + } + } diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/I18nHelper.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/I18nHelper.java index e5f5be8c6..b9b2b7157 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/I18nHelper.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/I18nHelper.java @@ -4,6 +4,7 @@ * Copyright (C) 2009-2010 Fundación para o Fomento da Calidade Industrial e * Desenvolvemento Tecnolóxico de Galicia * Copyright (C) 2010-2011 Igalia, S.L. + * Copyright (C) 2011 ComtecSF, S.L. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -24,9 +25,14 @@ package org.navalplanner.web; import java.util.HashMap; import java.util.Locale; +import javax.servlet.http.HttpSession; + +import org.navalplanner.business.users.entities.User; import org.xnap.commons.i18n.I18n; import org.xnap.commons.i18n.I18nFactory; import org.zkoss.util.Locales; +import org.zkoss.web.servlet.Charsets; +import org.zkoss.zk.ui.Executions; public class I18nHelper { @@ -36,8 +42,11 @@ public class I18nHelper { private static HashMap localesCache = new HashMap(); public static I18n getI18n() { + Charsets.setPreferredLocale((HttpSession) Executions.getCurrent() + .getSession().getNativeSession(), getUserLocale()); Locale locale = Locales.getCurrent(); + if (localesCache.keySet().contains(locale)) { return localesCache.get(locale); } @@ -56,6 +65,14 @@ public class I18nHelper { return i18n; } + private static Locale getUserLocale() { + User user = UserUtil.getUserFromSession(); + if (user != null) { + return user.getApplicationLanguage().getLocale(); + } + return null; + } + private static I18n getDefaultI18n() { I18n i18n = localesCache.get(defaultLang); if (i18n == null) { diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/UserUtil.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/UserUtil.java new file mode 100644 index 000000000..c4b5818f2 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/UserUtil.java @@ -0,0 +1,57 @@ +/* + * This file is part of NavalPlan + * + * Copyright (C) 2011 ComtecSF, S.L. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.navalplanner.web; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.navalplanner.business.common.Registry; +import org.navalplanner.business.common.exceptions.InstanceNotFoundException; +import org.navalplanner.business.users.entities.User; +import org.navalplanner.web.security.SecurityUtils; +import org.navalplanner.web.users.services.CustomUser; + +/** + * Class used to implement some utilities about users which could be useful in + * different web layer classes + * + * @author Cristina Alavarino Perez + * @author Ignacio Diaz Teijido + */ +public class UserUtil { + + private UserUtil() { + } + + private static final Log LOG = LogFactory.getLog(UserUtil.class); + + public static User getUserFromSession() { + CustomUser loggedUser = SecurityUtils.getLoggedUser(); + if (loggedUser != null) { + String username = loggedUser.getUsername(); + try { + return Registry.getUserDAO().findByLoginName(username); + } catch (InstanceNotFoundException e) { + LOG.info("User " + username + " not found in database."); + return null; + } + } + return null; + } + +} diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/web/security/SecurityUtils.java b/navalplanner-webapp/src/main/java/org/navalplanner/web/security/SecurityUtils.java index cebe81017..f95fcfd68 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/web/security/SecurityUtils.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/web/security/SecurityUtils.java @@ -63,9 +63,13 @@ public final class SecurityUtils { public final static CustomUser getLoggedUser() { Authentication authentication = getAuthentication(); if (authentication == null) { + // This happens before processing first element of login page return null; } - return (CustomUser) authentication.getPrincipal(); + if (authentication.getPrincipal() instanceof CustomUser) { + return (CustomUser) authentication.getPrincipal(); + } + return null; } private static Authentication getAuthentication() {