1 20 21 26 27 package edu.umd.cs.findbugs; 28 29 import java.awt.event.KeyEvent ; 30 import java.io.FileWriter ; 31 import java.io.IOException ; 32 import java.io.PrintWriter ; 33 import java.util.Enumeration ; 34 import java.util.HashMap ; 35 import java.util.Locale ; 36 import java.util.Map ; 37 import java.util.MissingResourceException ; 38 import java.util.ResourceBundle ; 39 40 import javax.swing.AbstractButton ; 41 42 import edu.umd.cs.findbugs.gui.AnnotatedString; 43 44 public class L10N { 45 private static final boolean DEBUG = SystemProperties.getBoolean("i18n.debug"); 46 private static final boolean GENERATE_MISSING_KEYS = SystemProperties.getBoolean("i18n.generateMissingKeys"); 47 48 private static ResourceBundle bundle; 49 private static ResourceBundle bundle_en; 50 51 private static PrintWriter extraProperties; 52 static { 53 try { 54 if (GENERATE_MISSING_KEYS) try { 55 extraProperties = new PrintWriter (new FileWriter ("/tmp/extra.properties")); 56 } catch (IOException e) { 57 e.printStackTrace(); 58 } 59 60 bundle = ResourceBundle.getBundle("edu.umd.cs.findbugs.gui.bundle.findbugs"); 61 bundle_en = ResourceBundle.getBundle("edu.umd.cs.findbugs.gui.bundle.findbugs", Locale.ENGLISH); 62 63 64 } catch (Exception mre) { 65 } 66 } 67 68 69 private L10N() { 70 } 71 72 private static String lookup(ResourceBundle b, String key) { 73 if (b == null || key == null ) throw new MissingResourceException (null,null,null); 74 75 return b.getString(key); 76 } 77 public static String getLocalString(String key, String defaultString) { 78 if (key == null) return "TRANSLATE("+defaultString+")"; 79 try { 80 return lookup(bundle, key); 81 } catch (MissingResourceException mre) { 82 try { 83 String en = lookup(bundle_en, key); 84 if (DEBUG) return "TRANSLATE("+en+")"; 85 else return en; 86 } catch (MissingResourceException mre2) { 87 if (extraProperties != null) { 88 extraProperties.println(key+"="+defaultString); 89 extraProperties.flush(); 90 } 91 String en = "Default("+defaultString+")"; 92 if (DEBUG) return "TRANSLATE("+en+")"; 93 else return en; 94 } 95 } 96 } 97 98 108 public static void localiseButton(AbstractButton button, String key, String defaultString, 109 boolean setMnemonic) { 110 AnnotatedString as = new AnnotatedString(getLocalString(key, defaultString)); 111 button.setText(as.toString()); 112 int mnemonic; 113 if (setMnemonic && 114 (mnemonic = as.getMnemonic()) != KeyEvent.VK_UNDEFINED) { 115 button.setMnemonic(mnemonic); 116 button.setDisplayedMnemonicIndex(as.getMnemonicIndex()); 117 } 118 } 119 } 120 | Popular Tags |