Package org.internna.iwebmvc.spring.i18n

Source Code of org.internna.iwebmvc.spring.i18n.ContextLocaleResolver

/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.spring.i18n;

import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.internna.iwebmvc.core.security.UserManager;
import org.internna.iwebmvc.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

/**
* The application {@link org.springframework.web.servlet.LocaleResolver}.
*
* @author Jose Noheda
* @since 1.0
*/
@Component("localeResolver")
public class ContextLocaleResolver extends SessionLocaleResolver {

    @Autowired protected UserManager userManager;

    public Locale setLocale(final HttpServletRequest request, final User user) {
        Locale locale = null;
        if (user != null && user.getLocale() != null) locale = new Locale(user.getLocale().getLocale());
        if (locale != null) setLocale(request, null, locale);
        return locale;
    }

    protected Locale getUserLocale(final HttpServletRequest request) {
        User user = userManager.getActiveUser();
        return setLocale(request, user);
    }

    @Override
    protected Locale determineDefaultLocale(final HttpServletRequest request) {
        Locale locale = getUserLocale(request);
        return locale == null ? super.determineDefaultLocale(request) : locale;
    }

}
TOP

Related Classes of org.internna.iwebmvc.spring.i18n.ContextLocaleResolver

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.