All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.caelum.vraptor.i18n.I18nLocalization Maven / Gradle / Ivy

There is a newer version: 4.2.0-RC4
Show newest version
package br.com.caelum.vraptor.i18n;

import java.util.Locale;
import java.util.ResourceBundle;

import javax.annotation.Priority;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Alternative;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.interceptor.Interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.jstl.core.Config;

import br.com.caelum.vraptor.core.JstlLocalization;

/**
 * ResourceBundle producer to locale configuration 
 * @author Denilson Telaroli
 */
@RequestScoped
@Alternative
@Priority(Interceptor.Priority.LIBRARY_BEFORE)
public class I18nLocalization extends JstlLocalization {

	private final HttpServletRequest request;

	/** 
	 * @deprecated CDI eyes only
	 */
	protected I18nLocalization() {
		this(null);
	}

	@Inject
	public I18nLocalization(HttpServletRequest request) {
		super(request);
		this.request = request;
	}
	
	@Override
	@Produces
	public ResourceBundle getBundle(Locale locale) {
		setLocaleFromRequestParameter();
		return super.getBundle(locale);
	}
	
	@Override
	@Produces
	public Locale getLocale() {
		return super.getLocale();
	}
	
	private void setLocaleFromRequestParameter() {
		String locale = request.getParameter("_locale");
		
		if(locale != null) {
			Config.set(request.getSession(), Config.FMT_LOCALE, locale);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy