1 22 23 package org.xquark.extractor.common; 24 25 26 import java.text.MessageFormat ; 27 import java.util.Locale ; 28 import java.util.ResourceBundle ; 29 30 31 35 public class MessageLibrary 36 { 37 38 private static final String RCSRevision = "$Revision: 1.6 $"; 39 private static final String RCSName = "$Name: $"; 40 41 private static MessageFormat _formatter = null ; 42 private static ResourceBundle _bundle; 43 44 public static void setup (String messageFileName,Locale locale ) 45 { 46 try 47 { 48 _bundle = ResourceBundle.getBundle(messageFileName,locale); 49 } 50 catch(Exception ex) 51 { 52 throw new SqlWrapperException ( "Can not load message library file:"+messageFileName, ex); 53 } 54 _formatter = new MessageFormat (""); 55 _formatter.setLocale(locale); 56 57 } 58 59 public static String getMessage(String key) 60 { 61 if(_bundle==null) 62 return "No message library available."; 63 try 64 { 65 return key + ": " + _bundle.getString(key); 66 } 67 catch(Exception ex) 68 { 69 return key + ": " + "Message [" + key + "] not found."; 70 } 71 } 72 73 public static String getMessage(String key, Object [] arguments) 74 { 75 String retVal = getMessage ( key ); 76 77 _formatter.applyPattern(retVal); 78 retVal = _formatter.format(arguments); 79 80 return retVal ; 81 } 82 83 public static String getMessage(String key, Object argument ) 84 { 85 Object [] args = {argument}; 86 String retVal = getMessage (key, args); 87 return retVal ; 88 } 89 90 public static String getMessage(String key, Object argument1, Object argument2) 91 { 92 Object [] args = {argument1, argument2}; 93 String retVal = getMessage (key, args); 94 return retVal ; 95 } 96 97 public static void main ( String [] args) { 98 MessageLibrary.setup( "org.xquark.extractor.common.resources.MessageLibrary" , new Locale ("en","US")); 99 String message ; 100 Object [] as = {"=="} ; 101 message = MessageLibrary.getMessage( "T_N_SUP_OPTR",as); 102 System.out.println(message); 103 104 message = MessageLibrary.getMessage( "T_N_SUP_FUN","contains"); 105 System.out.println(message); 106 107 message = MessageLibrary.getMessage( "T_N_SUP_FUN","contains","=="); 108 System.out.println(message); 109 } 110 111 } 112 | Popular Tags |