1 57 58 package org.enhydra.apache.xerces.msg; 59 60 import java.util.ListResourceBundle ; 61 import java.util.Locale ; 62 import java.util.ResourceBundle ; 63 import java.util.StringTokenizer ; 64 65 66 public class ExceptionMessagesLocalizer { 67 68 static final String FMT001 = "FMT001"; 69 70 71 74 75 public static String localizeMessage(String string, Locale locale) { 76 77 ResourceBundle fResourceBundle = null; 78 if (locale != null) 79 fResourceBundle = ListResourceBundle.getBundle("org.enhydra.apache.xerces.msg.ExceptionMessages", locale); 80 if (fResourceBundle == null || locale == null) 81 fResourceBundle = ListResourceBundle.getBundle("org.enhydra.apache.xerces.msg.ExceptionMessages"); 82 83 int keyIndex = string.indexOf(' '); 84 String key = string.substring(0, keyIndex); 85 int nlIndex = string.indexOf('\n'); 87 if (nlIndex == 0 || key == null || key.equals("")) return null; 89 String msg = fResourceBundle.getString(key); 90 if (nlIndex < 0) { 91 return msg; 92 } 93 94 StringTokenizer st = new StringTokenizer (string.substring(nlIndex+1), "\t"); 95 int count = st.countTokens(); 96 String [] args = new String [count]; 97 for(int i=0; i < count; i++) { 98 args[i] = st.nextToken(); 99 } 100 101 if (args != null) { 102 try { 103 msg = java.text.MessageFormat.format(msg, (Object [])args); 104 } catch (Exception e) { 105 msg = fResourceBundle.getString(FMT001)+ ": " + msg; 106 } 107 } 108 return msg; 109 } 110 111 } | Popular Tags |