001/* 002 * Copyright 2002-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.servlet.view.tiles3; 018 019import java.util.Locale; 020 021import javax.servlet.http.HttpServletRequest; 022 023import org.apache.tiles.locale.impl.DefaultLocaleResolver; 024import org.apache.tiles.request.Request; 025import org.apache.tiles.request.servlet.NotAServletEnvironmentException; 026import org.apache.tiles.request.servlet.ServletUtil; 027 028import org.springframework.web.servlet.support.RequestContextUtils; 029 030/** 031 * Tiles LocaleResolver adapter that delegates to a Spring 032 * {@link org.springframework.web.servlet.LocaleResolver}, exposing the 033 * DispatcherServlet-managed locale. 034 * 035 * <p>This adapter gets automatically registered by {@link TilesConfigurer}. 036 * 037 * @author Nicolas Le Bas 038 * @since 3.2 039 * @see org.apache.tiles.definition.UrlDefinitionsFactory#LOCALE_RESOLVER_IMPL_PROPERTY 040 */ 041public class SpringLocaleResolver extends DefaultLocaleResolver { 042 043 @Override 044 public Locale resolveLocale(Request request) { 045 try { 046 HttpServletRequest servletRequest = ServletUtil.getServletRequest(request).getRequest(); 047 if (servletRequest != null) { 048 return RequestContextUtils.getLocale(servletRequest); 049 } 050 } 051 catch (NotAServletEnvironmentException ex) { 052 // ignore 053 } 054 return super.resolveLocale(request); 055 } 056 057}