com.seajas.search.utilities.spring.i18n.AcceptHeaderSessionLocaleResolver.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.utilities.spring.i18n.AcceptHeaderSessionLocaleResolver.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.utilities.spring.i18n;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.i18n.SessionLocaleResolver;

/**
 * Locale resolver which combines the accept header and session locale resolvers.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
public class AcceptHeaderSessionLocaleResolver extends SessionLocaleResolver {
    /**
     * The available languages.
     */
    public List<String> availableLanguages;

    /**
     * Default constructor.
     * 
     * @param availableLanguages
     */
    public AcceptHeaderSessionLocaleResolver(String availableLanguages) {
        this.availableLanguages = Arrays.asList(availableLanguages.replaceAll(" ", "").trim().split(","));
    }

    /**
     * Determine the default locale from the HTTP request header.
     * 
     * @param request
     * @return Locale
     */
    @Override
    protected Locale determineDefaultLocale(HttpServletRequest request) {
        Locale requestLocale = request.getLocale();

        if (availableLanguages.contains(requestLocale.getLanguage()))
            return requestLocale;
        else
            return getDefaultLocale();
    }
}