Uses default browser locale when user has no language in settings

FEA: ItEr75S07UserSettings
This commit is contained in:
Ignacio Diaz Teijido 2011-07-19 09:26:26 +02:00 committed by Manuel Rego Casasnovas
parent e015096471
commit 7b97bea3b8
4 changed files with 93 additions and 6 deletions

View file

@ -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;
}
}

View file

@ -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<Locale, I18n> localesCache = new HashMap<Locale, I18n>();
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) {

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <cristina.alvarino@comtecsf.es>
* @author Ignacio Diaz Teijido <ignacio.diaz@comtecsf.es>
*/
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;
}
}

View file

@ -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() {