/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rjconsultores.ventaboletos.web.utilerias;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;

/**
 * 
 * @author gleimar
 */
public class MyLabelLocatorGeneral implements org.zkoss.util.resource.LabelLocator {

	private static Logger log = Logger.getLogger(MyLabelLocatorGeneral.class);
	private ServletContext _svlctx;
	private static final Locale localeEspanhol = new Locale("es");

	public MyLabelLocatorGeneral(ServletContext svlctx) {
		if (svlctx == null) {
			throw new IllegalArgumentException("null");
		}

		_svlctx = svlctx;
	}

	public URL locate(Locale locale) {
		URL resource = null;
		try {
			resource = _svlctx.getResource("/WEB-INF/" + getI3LabelName(locale));
			if (resource == null) {
				resource = _svlctx.getResource("/WEB-INF/" + getI3LabelDefault());
			}
		} catch (MalformedURLException ex) {
			log.error("Error al cargar locale:" + locale, ex);
			try {
				resource = _svlctx.getResource("/WEB-INF/" + getI3LabelDefault());
			} catch (MalformedURLException ex1) {
				log.error("Error al cargar la traduccion estandard.", ex1);
				throw new RuntimeException(ex1);
			}
		}
		return resource;

	}

	private static final String getI3LabelName(Locale locale) {
		
		if ((locale != null) && (locale.getLanguage().equals(localeEspanhol.getLanguage()))) {
			return "i3-label_es_MX.label";
		}
		
		return "i3-label_" + locale + ".label";
	}

	private static final String getI3LabelDefault() {
		return "i3-label_pt_BR.label";
	}

}
