001: /*
002: *
003: * Copyright 2007 Florin T.PATRASCU
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package ca.flop.jpublish.i18n;
020:
021: import ca.flop.jpublish.SpringActionSupport;
022: import com.anthonyeden.lib.config.Configuration;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.jpublish.JPublishContext;
026: import org.jpublish.Page;
027: import org.springframework.context.ApplicationContext;
028: import org.springframework.context.MessageSource;
029: import org.springframework.context.support.MessageSourceAccessor;
030: import org.springframework.web.servlet.DispatcherServlet;
031: import org.springframework.web.servlet.HandlerInterceptor;
032: import org.springframework.web.servlet.LocaleResolver;
033:
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletResponse;
036: import java.util.Locale;
037:
038: /**
039: * @author <a href="mailto:florin.patrascu@gmail.com">Florin T.PATRASCU</a>
040: * @since $Revision$ (created: May 29, 2007 12:15:15 AM)
041: */
042: public class SpringI18nSupport extends SpringActionSupport {
043: public static final String I18N = "i18n";
044: protected static final Log log = LogFactory
045: .getLog(SpringI18nSupport.class);
046:
047: /**
048: * Execute the action using the given context.
049: *
050: * @param context The current context
051: * @param configuration The configuration
052: * @throws Exception Any error
053: */
054: public void execute(JPublishContext context,
055: Configuration configuration) throws Exception {
056: HttpServletRequest request = (HttpServletRequest) context
057: .get("request");
058: HttpServletResponse response = (HttpServletResponse) context
059: .get("response");
060: Page page = ((Page) context.get("page"));
061:
062: if (page != null) {
063: Locale locale = page.getLocale();
064:
065: ApplicationContext applicationContext = getApplicationContext(context);
066: context.put(SPRING, applicationContext);
067: HandlerInterceptor localeChangeInterceptor = (HandlerInterceptor) applicationContext
068: .getBean("localeChangeInterceptor");
069:
070: if (localeChangeInterceptor != null) {
071: try {
072: LocaleResolver localeResolver = (LocaleResolver) applicationContext
073: .getBean("localeResolver");
074: if (localeResolver != null) {
075: request
076: .setAttribute(
077: DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
078: localeResolver);
079: localeChangeInterceptor.preHandle(request,
080: response, null);
081: locale = localeResolver.resolveLocale(request);
082: MessageSource messageSource = (MessageSource) applicationContext
083: .getBean("messageSource");
084: if (messageSource != null) {
085: context.put(I18N,
086: new MessageSourceAccessor(
087: messageSource, locale));
088: } else {
089: log
090: .error("There is no 'messageSource' defined in your application context."
091: + " Please define one.");
092: }
093: } else {
094: log
095: .error("please define a 'localeResolver' bean in your Application context");
096: }
097: } catch (Exception e) {
098: e.printStackTrace();
099: log.error("Cannot handle the locale change event");
100: }
101: }
102: page.setLocale(locale);
103: }
104: }
105: }
|