View Javadoc

1   package es.inteco.eni.commons;
2   
3   import java.util.Locale;
4   import java.util.MissingResourceException;
5   import java.util.ResourceBundle;
6   
7   import org.apache.log4j.Logger;
8   
9   import es.inteco.eni.utils.ExceptionUtils;
10  
11  /**
12   * Clase de ayuda para recuperar mensajes de un fichero
13   * 
14   * 
15   *
16   */
17  public final class MessagesHelper {
18  
19  	/**
20  	 * Log
21  	 */
22  	private static Logger log = Logger.getLogger(MessagesHelper.class);
23  
24  	/**
25  	 * Nombre del fichero de mensajes
26  	 */
27  	private static final String MESSAGES_BASE_NAME = "Messages";
28  
29  	/**
30  	 * ResourceBundle
31  	 */
32  	private static ResourceBundle resourceBundle;
33  	static{
34  		String language = EniConstants.ENI_WINDOS_LOCALE_LANGUAGE; //ApplicationPropertiesManager.getProperty("eni.windows.locale.language");
35  		String country = EniConstants.ENI_WINDOS_LOCALE_COUNTRY; //ApplicationPropertiesManager.getProperty("eni.windows.locale.country");
36  		if(language==null || country==null){
37  			resourceBundle = ResourceBundle.getBundle(MESSAGES_BASE_NAME);
38  		}else{
39  			Locale locale = new Locale(language, country);
40  			resourceBundle = ResourceBundle.getBundle(MESSAGES_BASE_NAME, locale);
41  		}
42  
43  	}
44  	/**
45  	 * Constructor privado
46  	 */
47  	private MessagesHelper(){
48  
49  	}
50  
51  	/**
52  	 * Devuelve el texto a partir de la clave
53  	 * @param key Clave
54  	 * @return El texto
55  	 */
56  	public static String getText(String key){
57  		//Comprueba si la clave es nula
58  		if(key==null){
59  			return null;
60  		}
61  
62  		//Busca el texto por la clave
63  		String result = null;
64  		try{
65  			result = resourceBundle.getString(key);
66  		}catch (MissingResourceException mre) {
67  			log.error(ExceptionUtils.stackTraceToString(mre));
68  		}
69  		return result;
70  	}
71  
72  }