Package de.odysseus.calyxo.struts.base

Source Code of de.odysseus.calyxo.struts.base.StrutsI18nSupport

/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* 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 de.odysseus.calyxo.struts.base;

import java.util.Locale;
import java.util.MissingResourceException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.Globals;
import org.apache.struts.util.MessageResources;

/**
*
* @author Christoph Beck
*/
public class StrutsI18nSupport extends de.odysseus.calyxo.base.I18nSupport {
  private StrutsModuleContext module;
 
  public StrutsI18nSupport(StrutsModuleContext module) {
    this.module = module;
  }
 
  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.I18nSupport#getLocale(javax.servlet.http.HttpServletRequest)
   */
  public Locale getLocale(HttpServletRequest request) {
    Locale locale =
       (Locale)request.getSession().getAttribute(Globals.LOCALE_KEY);
    return locale == null ? request.getLocale() : locale;
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.I18nSupport#setLocale(javax.servlet.http.HttpServletRequest, java.util.Locale)
   */
  public void setLocale(HttpServletRequest request, Locale locale) {
    request.getSession().setAttribute(Globals.LOCALE_KEY, locale);   
  }

  /**
   * Lookup message resources in module and application scope.
   */
  private MessageResources getResources(String bundle) {
    if (bundle == null)
      bundle = Globals.MESSAGES_KEY;

    MessageResources resources = (MessageResources)module.getAttribute(bundle);
   
    if (resources == null) {
      resources = (MessageResources)module.getServletContext().getAttribute(bundle);
    }
    return resources;
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.I18nSupport#lookupResource(java.util.Locale, java.lang.String, java.lang.String)
   */
  protected String lookupResource(Locale locale, String bundle, String key) {
    MessageResources resources = getResources(bundle);
    if (resources == null) {
      throw new MissingResourceException("No such bundle: '" + bundle + "'", bundle, null);
    }
    String result = resources.getMessage(locale, key);
    if (resources == null) {
      throw new MissingResourceException("No such resource: '" + bundle + ":" + key + "'", bundle, key);
    }
    return result;
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.I18nSupport#lookupMessage(java.util.Locale, java.lang.String, java.lang.String, java.lang.Object[])
   */
  protected String lookupMessage(Locale locale, String bundle, String key, Object[] values) {
    MessageResources resources = getResources(bundle);
    if (resources == null) {
      throw new MissingResourceException("No such bundle: '" + bundle + "'", bundle, null);
    }
    String result = resources.getMessage(locale, key, values);
    if (resources == null) {
      throw new MissingResourceException("No such resource: '" + bundle + ":" + key + "'", bundle, key);
    }
    return result;
  }
}
TOP

Related Classes of de.odysseus.calyxo.struts.base.StrutsI18nSupport

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.